DOCKER CONCEPT FOR DBA – PART 8 How to build docker image from docker file

1.You need to create docker file first.Below is example of small test docker file.

root@deb-VirtualBox:~# cat test_docker.file
 FROM centos:latest
 RUN yum update -y

Here please note that FROM clause denotes first we need to pull centos image and RUN clause will run yum update on top of it.

2.Now I will build the docker image .

-t option is used to provide tag name

-f option is used to provide docker file name.

You need to run the following command from same path where your docker file resides.

root@deb-VirtualBox:~# docker build -t customimage:v1 -f test_docker.file .
 Sending build context to Docker daemon 8.704kB
 Step 1/2 : FROM centos:latest
 latest: Pulling from library/centos
 469cfcc7a4b3: Pull complete
 Digest: sha256:989b936d56b1ace20ddf855a301741e52abca38286382cba7f44443210e96d16
 Status: Downloaded newer image for centos:latest
 ---> e934aafc2206
 Step 2/2 : RUN yum update -y
 ---> Running in 21686d3260e4
 Loaded plugins: fastestmirror, ovl
 http://ftp.iitm.ac.in/centos/7.4.1708/updates/x86_64/repodata/repomd.xml: [Errno 12] Timeout on http://ftp.iitm.ac.in/centos/7.4.1708/updates/x86_64/repodata/repomd.xml: (28, 'Connection timed out after 30001 milliseconds')
 Trying other mirror.
 Determining fastest mirrors
 * base: mirror.nbrc.ac.in
 * extras: mirror.nbrc.ac.in
 * updates: mirror.nbrc.ac.in
 No packages marked for update
 Removing intermediate container 21686d3260e4
 ---> 9528f83769a3
 Successfully built 9528f83769a3
 Successfully tagged customimage:v1

3.Now let me run the docker image command to check details of our latest build

root@deb-VirtualBox:~# docker images

Please note centos image has been pulled and on top of it our customimage has been built.

4.Now you can use no-cache during pull the image

root@deb-VirtualBox:~# docker build --pull --no-cache -t optimized:v1 -f test_docker.file .
 Sending build context to Docker daemon 8.704kB
 Step 1/2 : FROM centos:latest
 latest: Pulling from library/centos
 Digest: sha256:989b936d56b1ace20ddf855a301741e52abca38286382cba7f44443210e96d16
 Status: Image is up to date for centos:latest
 ---> e934aafc2206
 Step 2/2 : RUN yum update -y
 ---> Running in b2319a7d1d89
 Loaded plugins: fastestmirror, ovl
 http://ftp.iitm.ac.in/centos/7.4.1708/updates/x86_64/repodata/repomd.xml: [Errno 12] Timeout on http://ftp.iitm.ac.in/centos/7.4.1708/updates/x86_64/repodata/repomd.xml: (28, 'Connection timed out after 30001 milliseconds')
 Trying other mirror.
 Determining fastest mirrors
 * base: del-mirrors.extreme-ix.org
 * extras: del-mirrors.extreme-ix.org
 * updates: del-mirrors.extreme-ix.org
 No packages marked for update
 Removing intermediate container b2319a7d1d89
 ---> dd055168eb91
 Successfully built dd055168eb91
 Successfully tagged optimized:v1

5.Now let me write complex docker file

root@deb-VirtualBox:~# cat mycomplexdockerfile.file
FROM centos:6
LABEL maintainer="debasis.tcs@gmail.com"
RUN yum update -y && yum install -y httpd net-tools
RUN mkdir -p /run/httpd
RUN rm -rf /run/http/* /tmp/httpd*
CMD echo "Remember to check your container IP address"
ENV ENVIRONMENT="production"
EXPOSE 80
ENTRYPOINT apachectl "-DFOREGROUND"

6.Let me explain the syntex which we used above

 

FROM centos:6 (This will pull centos image as base)
LABEL maintainer=”debasis.tcs@gmail.com” (This is deprecated)
RUN yum update -y && yum install -y httpd net-tools(It will run all yum package install command)
RUN mkdir -p /run/httpd (Create directory)
RUN rm -rf /run/http/* /tmp/httpd* (Remove directory)
CMD echo “Remember to check your container IP address” (Echo something important)
ENV ENVIRONMENT=”production” (Setting env variable)
EXPOSE 80 (The apache service will listener to port 80)
ENTRYPOINT apachectl “-DFOREGROUND”

 

7.Building docker image

docker build -t mywebserver:v1 -f mycomplexdockerfile.file .

8.Please check the image again

root@deb-VirtualBox:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mywebserver v1 a23dab42b0ee 7 minutes ago 321MB
optimized v1 dd055168eb91 11 hours ago 301MB
customimage v1 9528f83769a3 11 hours ago 301MB
hello-world latest e38bc07ac18e 2 weeks ago 1.85kB
centos 6 70b5d81549ec 3 weeks ago 195MB
centos latest e934aafc2206 3 weeks ago 199MB
root@deb-VirtualBox:~# docker run -d --name mytestweb --rm mywebserver:v1
c0d3e30d1ab22523e7be4958f15cad6dca122a368306a23dd1573a958adabebd

9.Please check that docker is running now

root@deb-VirtualBox:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c0d3e30d1ab2 mywebserver:v1 "/bin/sh -c 'apachec…" 45 seconds ago Up 44 seconds 80/tcp mytestweb

10.Let me check IP address of the docker

root@deb-VirtualBox:~# docker inspect mytestweb | grep -i ipaddress
 "SecondaryIPAddresses": null,
 "IPAddress": "172.17.0.2",
 "IPAddress": "172.17.0.2",

11.Let me check using elinks

root@deb-VirtualBox:~# elinks http://172.17.0.2

12.Now we can use argument in docker file

root@deb-VirtualBox:~# cat mycomplexdockerfile_arg.file
ARG TARGETVERSION=6
FROM centos:${TARGETVERSION}
LABEL maintainer="debasis.tcs@gmail.com"
RUN yum update -y && yum install -y httpd net-tools
RUN mkdir -p /run/httpd
RUN rm -rf /run/http/* /tmp/httpd*
CMD echo "Remember to check your container IP address"
ENV ENVIRONMENT="production"
EXPOSE 80
ENTRYPOINT apachectl "-DFOREGROUND"

13.I will again re-build the docker

root@deb-VirtualBox:~# docker build -t mywebserver1:v1 -f mycomplexdockerfile_arg.file .

14.We can check history of command executed in docker build

root@deb-VirtualBox:~# docker image history mywebserver1:v1

15.You may use no-trunc option to see full command line used in docker run

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>