How To Use DockerFile To Build Images For Docker Containers? – POFTUT

How To Use DockerFile To Build Images For Docker Containers?


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

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 poftut@gmail.com 

RUN apt-get update 
RUN apt-get install –y apache2
Dockerfile
Dockerfile

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 poftut1 .

$ 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 root privileges.

$ sudo docker build -t poftut1 .
Build Dockerfile and Create New Image
Build Dockerfile and Create New Image

We can see from image given Dockerfile instructions are executed line by line.

  • Step 1 is getting base ubuntu image
  • Step 2 is setting Maintainer information
  • Step 3 is running apt-get update command
  • Step 4 is installing apache2 with apt package manager.

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
Use Docker Image
Use Docker Image

Dockerfile Commands

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

LEARN MORE  How To Get Information About Running Containers, Images In Docker?

Syntax

ADD [SOURCE DIRECTORY] [DESTINATION DIRECTORY]

In this example we will copy the httpd.conf file into the images /etc/httpd/httpd.conf .

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 directive.

Syntax

ARG [NAME] [=DEFAULT VALUE]

In this example we will set build time argument VERSION with the default value 18.10

ARG VERSION="18.01"

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.

Syntax

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 "Hello Poftut"

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.

Syntax

ENTRYPOINT [COMMAND]

In this example we will start the httpd command when a container start.

ENTRYPOINT /bin/httpd

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.

LEARN MORE  How To Create, Start, Stop, Manage Docker Containers In Linux?

Syntax

 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.

Syntax

EXPOSE [PORT]

In this example we will redirect port 8080 to the local system port.

EXPOSE 8080

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 ubuntu , centos etc. Base image will be searched in the local system and Docker Hub.

Syntax

FROM [IMAGE NAME]

In this example we will use CentOS image from Docker Hub.

FROM centos

If we want we can also specify the version of the image like ubuntu18.10 etc.

FROM ubuntu18.10

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.

Syntax

MAINTAINER [NAME]

In this example we will set the author email which is poftut@gmail.com

MAINTAINER poftut@gmail.com

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 ubuntu image.

LEARN MORE  How to Setup Kubernetes 1.4 on Ubuntu

Syntax

RUN [COMMAND]

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

The USER directive can be used to set UID or username which is to run the image.

Syntax

USER [UID or USERNAME]

In this example we will set the user name as ismail

USER ismail

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 /myhome etc.

Syntax

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.

Syntax

WORKDIR [PATH]

In this example we will set /tmp as the current working directory

WORKDIR /tmp

Leave a Comment