How To Use DockerFile To Build Images For Docker Containers?

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/how-to-use-dockerfile-to-build-images-for-docker-containers/
PINTEREST
PINTEREST
INSTAGRAM

Docker containers run on and made changes on images. Generally we use existing images which is like a template to create new container. A lot of Linux distribution and software provides 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 new image.

Dockerfile

Dockerfile is a simple text file which contains configuration for the Docker Image which will be created. Dockerfile contains Dockerfile Commands to change properties of image. We can also call a Dockerfile as a recepie for 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.

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 imagewith the -t option of docker build command poftut1 .

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 do not have enough rights to create Docker image and we get root privileges.

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.

Use Docker Image

We can use this Docker image named poftut1 like a regular image for example ubuntu . We will create new container from poftut1 image with docker run command.

LEARN MORE  What is Docker Registry and How Use It?

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

Syntax

In this example we will copy the httpd.conf file into the images /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

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

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

In this example we want to print Hello Poftut to the terminal when new container is created from this image. The command will be echo and argument1 will be "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.

LEARN MORE  How To Install Kali Docker Image To The Linux?

Syntax

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.

Syntax

In this example we will set the thread count which will be used by the daemon service as environment variable.

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

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

Syntax

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

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

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.

LEARN MORE  How To Remove Docker Images

Syntax

In this example we will set the author email which is 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.

Syntax

In this example we will install apache2 package and then remove cache files.

USER – Set Username

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

Syntax

In this example we will set the user name as 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

In this example we will mount local path /home/ismail into container path /myhome which will be created automatically.

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

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

 

 

 

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/how-to-use-dockerfile-to-build-images-for-docker-containers/
PINTEREST
PINTEREST
INSTAGRAM