Learn To Automate The Build And Configure Docker Images: Docker File
Docker Linux Tutorials

Learn To Automate The Build And Configure Docker Images: Docker File

In our previous tutorial, we tried with a simple Demo of a DockerFile to create a custom image with the installation of “dos2unix” package inside the image. Now in this tutorial, we will take a deep dive in DockerFile and learn to automate the building of Docker images with some configuration to create our custom image.

First, we need to know what are DockerFiles?

Docker files are nothing but a step-by-step set of instruction or command used to build a custom Docker image. Docker files are written in text format. The build is run by the Docker daemon, not by the CLI. The first thing a build process does is send the entire context (recursively) to the daemon. The command docker build  builds an image from a Dockerfile and a context. The build’s context is the files at a specified location PATH or URL. The PATH is a directory on your local file system.

Just in case, if you did Miss Previous tutorials regarding Docker, You can read here:

Part 1: What Is Docker And Concept Of Containers With Virtualization?
Part 2: How To Install Docker On CentOS/RHEL 6/7? And Learn Docker HUB Registration
Part 3A: Getting Familiar With Docker Commands Docker Terms: Docker – Part 3 A
Part 3B: Getting Familiar With Docker Commands Docker Terms: Docker – Part 3 B
Part 4: Learn To Install, Run And Delete Applications Inside Docker Containers – Part 4
Part 5: Learn To Push Custom Build Docker Image On Docker HUB (Repository) 
Part 6: What Are Docker File Learns To Use Them? | Simple Demo

Prerequisite:

Installed Docker Deamon: Part 1

Scenario :

Host OS: CentOS 7 x86_64
Host IP: 192.168.1.188
RAM: 4GB memory,
Network Port: 1GB/s
HDD: 1000GB of disk space.


To construct this DockerFile we will use CentOS image. In the same image we will install Apache and  expose port 80 to access the Docker Apache server. Apart from this, we will do much more.

Before starting our Docker file construction let’s get aware and introduced of few instruction keywords and command used in the creation of DockerFile.

  1. Keyword  “FROM”, tells Docker to pull the base image from which we will build our new image? If you looking for a specific image then use Tag e.g. centos:7.3. If you don’t specify tag you will get the latest image. You may use docker #search centos command to get your image tag or image version.
  2. Keyword “MAINTAINER”, tells that who will maintain this image so just mention the name and email ID.
  3. Command “RUN”, is used to run instructions against the image. These instructions can be used in multiple lines and run any commands after Docker image creation.
  4. Command “CMD” is just to print a message to the user like you are done or Successfully Completed. Use this when Docker image is started and it should be used only one time in a DockerFile.
  5. Keyword “ENTRYPOINT” is same as CMD. It is used when you need container to run the same executable every time, then you should consider using ENTRYPOINT in combination with like CMD [“param1″,”param2”] (as default parameters to ENTRYPOINT)
  6. Instruction “EXPOSE” instruction informs Docker that the container listens on the specified network ports at runtime. EXPOSE does not make the ports of the container accessible to the host. To do that, you must use either the -p flag to publish a range of ports or the -P flag to publish all of the exposed ports.
  7. Variable “ENV”, Set container environment variables. The environment variables set using ENV will persist when a container is run from the resulting image.
  8. Instruction “ADD” instruction copies new files, directories or remote file URLs from <src> and adds them to the filesystem of the image at the path <dest>. Like Copy resources (files, directories or files from URLs).

Let’s start

Step 1: Create Directory For DockerFile

From the Previous article, we know that our Docker file should start with capital “D” so we will keep it that way. But before that we should make a directory to keep our DockerFile at a specific path. Use the following command to create a directory and DockerFile.

Step2: Edit DockerFile

Now edit the DockerFile using vim or any editor present on your system and append the DockerFile excerpt given below.

Now add following DockerFile excerpt:

after adding these excerpt save and close the wife using :wq!


Let’s understand what we have just done

  1. The line starting with “#”   is used for comment. You can comment as many as comment you require, all you have to do is to start with a #
  2. Keyword “FROM” tell the name of the base image. In this example, we are using CentOS to create our new image.
  3. Keyword “MAINTAINER”, tells that who will maintain this image so just mention the name and email ID.
  4. Command “RUN” is used to run instructions against the image like in this example we will install Apache and we will put some text inside the default Apache page on our CentOS image.
  5. EXPOSE command will instruct Docker container to listen on port 80

Step 3: Executing DockerFile

Now we have created our Docker file and we will use CentOS image but it is required to give a specific name to the image which will be created locally after the execution of DockerFile.  Let’s say the new name is centos-apache.

Let’s understand what we are doing here:

  1. “-t” is used for Tagging our new image
  2. centos-apache is the Image Name you want to give to your image.
  3. /var/dockerfiles/centos/apache/ is the directory where the Your DockerFile is present.

After completion of the command we can check for our newly created image using the following command:

Step 4: Start Container And Access Apache

As previously told, by default Docker container port are not accessible to the Docker host. So, access Apache we need to run our container continuously (in the background) and access the container exposed port (80) from the host or other remote machines in your network run the flowing commands.

Let’s understand what we are doing here:

  1. “-it” runs the centos-apache container with Interactive mode and with Tagging
  2. “-p” option maps the container port 80 to your localhost port 81. And Docker web server is accessible only on port 81
  3. centos-apache is the Image Name you want to use.

Step 5: Check Our Work

1. Through Port: We can check the status of our running Docker container with ‘docker ps’ command.Now we have a custom image and we created Docker container with the web server running. So to check out work we will use ‘netstat’ command to take an idea about what ports.

2. Through Web Page: You may verify this by accessing the web page on the web browser. Now, Open your web browser on a computer within your network and hit http://DockerHostIP:81/ in my case i will hit http://192.168.1.188:81 and if you can see below image then your newly created Docker image is working fine.

Learn To Automate The Build And Configure Docker Images: Docker File

Step 6: Check Inside Your Docker Container

Now to get a status and idea about what’s happening inside Docker container or you ant to stop it you may use flowing commands

Now you can check different stats inside a docker container using follwoing commands.

You may Also handle these services from the Docker Host machine you need to check Part 8

Part 1: What Is Docker And Concept Of Containers With Virtualization?
Part 2: How To Install Docker On CentOS/RHEL 6/7? And Learn Docker HUB Registration
Part 3A: Getting Familiar With Docker Commands Docker Terms: Docker – Part 3 A
Part 3B: Getting Familiar With Docker Commands Docker Terms: Docker – Part 3 B
Part 4: Learn To Install, Run And Delete Applications Inside Docker Containers – Part 4
Part 5: Learn To Push Custom Build Docker Image On Docker HUB (Repository) 
Part 6: What Are Docker File Learns To Use Them? | Simple Demo

Video Coming Soon:



Kapendra
Love to write technical stuff with personal experience as I am working as a Sr. Linux Admin. and every day is a learning day and Trust me being tech geek is really cool.
http://kapendra.com

Leave a Reply