Building custom Docker image using Dockerfile

We can build the docker images using Dockerfile

Dockerfile is a text file containing command-line instructions.

syntax for ‘docker build’:

# docker build <PATH/URL>

PATH -> directory path on your server
URL -> Git repository URL.

PATH/URL -> defines the context area of the docker image, it contains Dockerfile and files which are required to build the docker image.

Given PATH/URL will be recursively traversed by Docker daemon, so we should not use ‘/’ directory as a PATH.

In context area, we may include ‘.dockerignore‘ file to exclude files and directories during build process.

Types of instructions in Dockerfile

In Dockerfile, each instruction will have arguments

# -> it refers the comment.

Instruction commands:

FROM -> It refers the base image from which we are going to build custom image. This instruction should be the first line in Dockerfile
MAINTAINER -> It refers author of the custom image
RUN -> it refers the set of commands which needs to be executed on image and commit it to image.
LABEL -> it refers the naming to image
CMD -> it refers the set of commands which needs to executed on container while bringing up contianer. There should be only one CMD entry in Dockerfile
ENTRYPOINT -> it refers the default command set of container during startup
EXPOSE -> It refers the container’s run-time port
ENV -> it refers the environmental variables and it is key value pair
ADD and COPY -> It is used to copy the files from context area to inside the container.
VOLUME -> it refers the mount point
USER -> it refers the username (user id) by which the above said RUN instruction should run
WORKDIR -> it refers the working directory location path where the above said RUN instructions should run.

A sample Dockerfile to install HTTPD and start web-server to display the hello world via index.html,

[root@docker-host-1 ~]# docker --version
Docker version 17.12.0-ce, build c97c6d6
[root@docker-host-1 ~]#

[root@docker-host-1 ~]# mkdir dockerenv
[root@docker-host-1 ~]# cd dockerenv
[root@docker-host-1 dockerenv]# pwd
/root/dockerenv
[root@docker-host-1 dockerenv]#
[root@docker-host-1 dockerenv]# ls -lrt
total 8
-rw-r–r–. 1 root root 91 Feb 24 08:35 index.html
-rw-r–r–. 1 root root 325 Feb 24 08:37 Dockerfile
[root@docker-host-1 dockerenv]#

Dockerfile:

[root@docker-host-1 dockerenv]# cat Dockerfile
# we are using 'centos' base image
FROM centos:latest

#Author
MAINTAINER Thirumalai

###Installing httpd web server
RUN yum clean all

RUN yum -y update

RUN yum install -y httpd

### Copying index.html
COPY ./index.html /var/www/html/

# Expose the default port
EXPOSE 80

# Start the service
CMD [“-D”, “FOREGROUND”]
ENTRYPOINT [“/usr/sbin/httpd”]

[root@docker-host-1 dockerenv]#

index.html:

[root@docker-host-1 dockerenv]# cat index.html
<html>
<header><title>Hello Dockerfile</title></header>
<body>
Hello world
</body>
</html>
[root@docker-host-1 dockerenv]#

[root@docker-host-1 dockerenv]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@docker-host-1 dockerenv]#

Executing docker build:

#docker build -t centos/helloweb .

[root@docker-host-1 dockerenv]# docker build -t centos/helloweb .
Sending build context to Docker daemon 3.072kB
Step 1/8 : FROM centos:latest
latest: Pulling from library/centos
af4b0a2388c6: Pull complete
Digest: sha256:2671f7a3eea36ce43609e9fe7435ade83094291055f1c96d9d1d1d7c0b986a5d
Status: Downloaded newer image for centos:latest
---> ff426288ea90
Step 2/8 : MAINTAINER Thirumalai
---> Running in a91539d781a1
Removing intermediate container a91539d781a1
---> 5788f4608826
Step 3/8 : RUN yum clean all
---> Running in 1ecac9fe53d1
Loaded plugins: fastestmirror, ovl
Cleaning repos: base extras updates
Cleaning up everything
Maybe you want: rm -rf /var/cache/yum, to also free up space taken by orphaned data from disabled or removed repos
Removing intermediate container 1ecac9fe53d1
---> 8c7ac7597e92
Step 4/8 : RUN yum -y update
---> Running in 134c38a298f1
Loaded plugins: fastestmirror, ovl
.
.
Resolving Dependencies
--> Running transaction check
---> Package bind-license.noarch 32:9.9.4-51.el7_4.1 will be updated
---> Package bind-license.noarch 32:9.9.4-51.el7_4.2 will be an update
.
.
---> Package yum.noarch 0:3.4.3-154.el7.centos.1 will be an update
--> Finished Dependency Resolution
.
.
Upgrade 11 Packages

Total download size: 14 M
Downloading packages:

Transaction test succeeded
Running transaction
Updating : libdb-5.3.21-21.el7_4.x86_64 1/22
..
Verifying : libdb-5.3.21-20.el7.x86_64 22/22

Updated:
bind-license.noarch 32:9.9.4-51.el7_4.2
..
yum.noarch 0:3.4.3-154.el7.centos.1

Complete!
Removing intermediate container 134c38a298f1
—> fb25937d45cc
Step 5/8 : RUN yum install -y httpd
—> Running in d7ac5ab19d0b
Loaded plugins: fastestmirror, ovl
Resolving Dependencies
.
.
Install 1 Package (+5 Dependent packages)

Total download size: 24 M
Installed size: 32 M
.
.
Installed:
httpd.x86_64 0:2.4.6-67.el7.centos.6
..
Complete!
Removing intermediate container d7ac5ab19d0b
—> 083ec3318310
Step 6/8 : COPY ./index.html /var/www/html/
—> 6aa6a7341f08
Step 7/8 : EXPOSE 80
—> Running in d65f754aecfd
Removing intermediate container d65f754aecfd
—> 1aa14450665d
Step 8/8 : CMD [“systemctl start httpd”]
—> Running in f094d757b517
Removing intermediate container f094d757b517
—> df113dd8482b
Successfully built df113dd8482b
Successfully tagged centos/helloweb:latest
[root@docker-host-1 dockerenv]#

Listing out the images,

[root@docker-host-1 dockerenv]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos/helloweb latest df113dd8482b 4 minutes ago 503MB
centos latest ff426288ea90 6 weeks ago 207MB
[root@docker-host-1 dockerenv]#

Running our newly built custom docker image:

#docker run -d -p 80:80 –name=helloweb centos/helloweb

Now I am getting the error as below,

[root@docker-host-1 dockerenv]# docker run -d -p 80:80 --name=helloweb centos/helloweb
c5ffe8133681009302cd3f5f303604dae2ec45f1eb84ba7570db70f2c372c383
docker: Error response from daemon: OCI runtime create failed: container_linux.go:296: starting container process caused "exec: \"systemctl start httpd\": executable file not found in $PATH": unknown.
[root@docker-host-1 dockerenv]#

So, we need to update the “# Start the service” section in Dockerfile as below and we will re-run the docker build command

# Start the service
CMD [“-D”, “FOREGROUND”]
ENTRYPOINT [“/usr/sbin/httpd”]

[root@docker-host-1 dockerenv]# cat Dockerfile
# we are using ‘centos’ base image
FROM centos:latest

#Author
MAINTAINER Thirumalai

###Installing httpd web server
RUN yum clean all

RUN yum -y update

RUN yum install -y httpd

### Copying index.html
COPY ./index.html /var/www/html/

# Expose the default port
EXPOSE 80

# Start the service
CMD [“-D”, “FOREGROUND”]
ENTRYPOINT [“/usr/sbin/httpd”]
[root@docker-host-1 dockerenv]#

Re-Run the docker build process

[root@docker-host-1 dockerenv]# docker build -t centos/helloweb .
Sending build context to Docker daemon 3.072kB
Step 1/9 : FROM centos:latest
—> ff426288ea90
Step 2/9 : MAINTAINER Thirumalai
—> Using cache
—> 5788f4608826
Step 3/9 : RUN yum clean all
—> Using cache
—> 8c7ac7597e92
Step 4/9 : RUN yum -y update
—> Using cache
—> fb25937d45cc
Step 5/9 : RUN yum install -y httpd
—> Using cache
—> 083ec3318310
Step 6/9 : COPY ./index.html /var/www/html/
—> Using cache
—> 6aa6a7341f08
Step 7/9 : EXPOSE 80
—> Using cache
—> 1aa14450665d
Step 8/9 : CMD [“-D”, “FOREGROUND”]
—> Running in c5f358d5e260
Removing intermediate container c5f358d5e260
—> d483e6d199e2
Step 9/9 : ENTRYPOINT [“/usr/sbin/httpd”]
—> Running in 46f637410233
Removing intermediate container 46f637410233
—> f1a8014162c6
Successfully built f1a8014162c6
Successfully tagged centos/helloweb:latest
[root@docker-host-1 dockerenv]#

Re-running our newly built custom docker image:

[root@docker-host-1 dockerenv]# docker run -d -p 80:80 –name=helloweb centos/helloweb
1a381912d61f408a7ed0834c61504e782d0fe93b7fda303f4f525975fb49421a
[root@docker-host-1 dockerenv]#

Now we can see the docker ps output as below,

[root@docker-host-1 dockerenv]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1a381912d61f centos/helloweb "/usr/sbin/httpd -D …" 10 minutes ago Up 10 minutes 0.0.0.0:80->80/tcp helloweb
[root@docker-host-1 dockerenv]#

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: