Docker containers run on and made changes to images. Generally, we use existing images which is like a template to create a new container. A lot of Linux distribution and software provide serve Docker images. But in some cases, we may need to create or enhance the existing image. Dockerfile can be used to create new images by providing instructions about the new images.
Dockerfile is a simple text file that contains configuration for the Docker Image which will be created. Dockerfile contains Dockerfile Commands to change the properties of the image. We can also call a Dockerfile as a recipe for an image.
Here is a simple and lean Dockerfile. This file simple derive Base Image from current and latest Ubuntu Docker image and then updates the existing packages. After update is completed
apache2 package is install. Generally Dockerfile’s are more complicated then this example as we will see below.
#This is a sample Image FROM ubuntu MAINTAINER [email protected] RUN apt-get update RUN apt-get install –y apache2
Build Dockerfile and Create New Image
We will start with simple steps where we will build previously given Dockerfile example. This build will create an image with the
-t option of
docker build command
$ mkdir poftut1 $ cd poftut1/ $ vim Dockerfile
After the DockerFile is created with the example content we can build our image with
docker build command. We will require sudo because the current user does not have enough rights to create Docker image and we get
$ sudo docker build -t poftut1 .
We can see from image given Dockerfile instructions are executed line by line.
- Step 1 is getting base
- Step 2 is setting Maintainer information
- Step 3 is running
- Step 4 is installing
Run and Use Docker Image
We can use this Docker image named
poftut1 like a regular image for example
ubuntu . We will create a new container from
poftut1 image with
docker run command.
$ sudo docker run -t -i poftut1 bash
Upto now we have learned simple steps to build an image with
Dockerfile . there are a lot of different Dockerfile commands which can be used to run command, create file, create metadata, copy file etc.
ADD – Copy File From Local System Into Image
Add is very useful command which is used to copy files and folders from local system where docker runs into the image file. This can be used to copy special configuration, copy applications or data. We can also use URL’s for [SOURCE DIRECTORY] like https://www.poftut.com/data
ADD [SOURCE DIRECTORY] [DESTINATION DIRECTORY]
In this example we will copy the
httpd.conf file into the images
ADD httpd.conf /etc/httpd/httpd.conf
ARG – Provide Build Time Arguments
If we want to provide some argument in key value format in the build time we can use
ARG directive. This argument provided with the
build command. If we want to use default value we can set with
ARG [NAME] [=DEFAULT VALUE]
In this example we will set build time argument
VERSION with the default value
CMD – Run Command At Container Start
CMD is another useful command which is used to run commands at the start of the container.
CMD will not run during image build. When new container is created this command will be run.
CMD [COMMAND] [ARGUMENT1] [ARGUMENT2]
In this example, we want to print Hello Poftut to the terminal when a new container is created from this image. The command will be
echo and argument1 will be
CMD "echo" "Hello Poftut"
ENTRYPOINT – Run Single Command On Foreground
ENTRYPOINT is used to set single command which will run on foreground when a container starts. For example if we want to run a web server as container we can set the web server daemon as ENTRYPOINT.
In this example we will start the
httpd command when a container start.
ENV – Set Environment Variables In Key Value
ENV command can be used in a Dockerfile in order to set related image environment variables like key value pairs. This can be used like a database or communication channel with the image.
ENV [KEY] [VALUE]
In this example we will set the thread count which will be used by the daemon service as environment variable.
ENV THREADS 20
EXPOSE – Redirect Host Port To The Container Port
EXPOSE is used to redirect container port to the local system port which it runs. For example if run web server in a container TCP port 80 and want to use local system port 80 we can use EXPOSE.
In this example we will redirect port 8080 to the local system port.
FROM – Use Given Base Image
FROM is used to set base image for the created image. This is very important command because it will create base image from an existing image like
centos etc. Base image will be searched in the local system and Docker Hub.
FROM [IMAGE NAME]
In this example we will use CentOS image from Docker Hub.
If we want we can also specify the version of the image like
MAINTAINER – Set Author Information or Image Metadata
MAINTAINER is used to set information like author name, email etc. Simply we can set some informational data for the container. This information is like comment in programming languages.
In this example we will set the author email which is
MAINTAINER [email protected]
RUN – Run Command During Image Build
During the build of the image we need to take some actions. These actions can be taken with the
RUN command. For example we can install a package with the
apt command for a
In this example we will install
apache2 package and then remove cache files.
RUN apt update RUN apt install apache2 RUN apt autoremove
USER – Set Username
USER directive can be used to set UID or username which is to run the image.
USER [UID or USERNAME]
In this example we will set the user name as
VOLUME – Mount Local System Directory In to Container
In some cases we may need to mount local system path or directory into the running container. We can use
VOLUME directive in order to mount local system path like
/home/ismail into the running container given path like
VOLUME [LOCAL SYSTEM PATH] [CONTAINER PATH]
In this example we will mount local path
/home/ismail into container path
/myhome which will be created automatically.
VOLUME /home/ismail /myhome
WORKDIR – Change or Set Current Working Directory
As we can run commands during the build of image we may need also change working directory which can affect command execution. We will use
WORKDIR to change current working directory.
In this example we will set
/tmp as the current working directory