top of page

Assemble Docker Image Using Dockerfile




What is dockerfile ?


A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Using docker build users can create an automated build that executes several command-line instructions in succession.


Keywords Of Dockerfile :-

There are certain keywords of dockerfile which can be used in to build a specific image.


LABEL : Allows you to add a label to your docker image.


WORKDIR : Set the path from where you want to execute the command defined with CMD.


USER : Set the UID which is to run the container.


RUN : Central executing directive for Dockerfiles.


MAINTAINER : It defines an email address and name of image creator.


FROM : It defines the base image used to start the build process.


CMD : This can be used to execute a command within a container.


VOLUME : The VOLUME command is used to enable access from your container to a directory on the host machine.


ADD : The ADD command gets two arguments: a source and a destination. It basically copies the files from the source on the host into the container’s own filesystem at the set destination.


Lets create a Dockerfile to build a Centos Image.


Create a new directory called dockercentos and :


[root@siddhesh ~]# mkdir dockercentos
[root@siddhesh ~]# cd dockercentos
[root@siddhesh ~]# cat Dockerfile
FROM centos:7
MAINTAINER Siddhesh Kadam siddhesh@tecgeek.info
RUN yum makecache
RUN yum install -y httpd
RUN mkdir -p /etc/mail
RUN mkdir -p /tmp/siddhesh
[root@siddhesh ~]#

Save and close the file. Build the image with the command:


 [root@siddhesh ~]# docker build -t "apache_centos:DockerFile" .
Sending build context to Docker daemon  2.048kB
Step 1/6 : FROM centos:7
7: Pulling from library/centos
ab5ef0e58194: Pull complete
Digest: sha256:4a701376d03f6b39b8c2a8f4a8e499441b0d567f9ab9d58e4991de4472fb813c
Status: Downloaded newer image for centos:7
 ---> 5e35e350aded
Step 2/6 : MAINTAINER Siddhesh Kadam siddhesh@tecgeek.info
 ---> Running in fd4a5a2ccf3d
Removing intermediate container fd4a5a2ccf3d
 ---> f9ed312db9f8
Step 3/6 : RUN yum makecache
 ---> Running in 95b113334406
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
 * base: mirror.ash.fastserv.com
 * extras: mirror.rackspace.com
 * updates: mirror.umd.edu
Metadata Cache Created
Removing intermediate container 95b113334406
 ---> 6ea4e4f5188b
Step 4/6 : RUN yum install -y httpd
 ---> Running in 9ef4c5080415
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
 * base: mirror.ash.fastserv.com
 * extras: mirror.rackspace.com
 * updates: mirror.umd.edu
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-90.el7.centos will be installed
--> Processing Dependency: httpd-tools = 2.4.6-90.el7.centos for package: httpd-2.4.6-90.el7.centos.x86_64
--> Processing Dependency: system-logos >= 7.92.1-1 for package: httpd-2.4.6-90.el7.centos.x86_64
--> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-90.el7.centos.x86_64
--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-90.el7.centos.x86_64
--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-90.el7.centos.x86_64
--> Running transaction check
---> Package apr.x86_64 0:1.4.8-5.el7 will be installed
---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed
---> Package centos-logos.noarch 0:70.0.6-3.el7.centos will be installed
---> Package httpd-tools.x86_64 0:2.4.6-90.el7.centos will be installed
---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package             Arch          Version                    Repository   Size
================================================================================
Installing:
 httpd               x86_64        2.4.6-90.el7.centos        base        2.7 M
Installing for dependencies:
 apr                 x86_64        1.4.8-5.el7                base        103 k
 apr-util            x86_64        1.5.2-6.el7                base         92 k
 centos-logos        noarch        70.0.6-3.el7.centos        base         21 M
 httpd-tools         x86_64        2.4.6-90.el7.centos        base         91 k
 mailcap             noarch        2.1.41-2.el7               base         31 k

Transaction Summary
================================================================================
Install  1 Package (+5 Dependent packages)

Total download size: 24 M
Installed size: 31 M
Downloading packages:
warning: /var/cache/yum/x86_64/7/base/packages/apr-util-1.5.2-6.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Public key for apr-util-1.5.2-6.el7.x86_64.rpm is not installed
--------------------------------------------------------------------------------
Total                                               40 MB/s |  24 MB  00:00
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
 Userid     : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>"
 Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
 Package    : centos-release-7-7.1908.0.el7.centos.x86_64 (@CentOS)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : apr-1.4.8-5.el7.x86_64                                       1/6
  Installing : apr-util-1.5.2-6.el7.x86_64                                  2/6
  Installing : httpd-tools-2.4.6-90.el7.centos.x86_64                       3/6
  Installing : centos-logos-70.0.6-3.el7.centos.noarch                      4/6
  Installing : mailcap-2.1.41-2.el7.noarch                                  5/6
  Installing : httpd-2.4.6-90.el7.centos.x86_64                             6/6
  Verifying  : mailcap-2.1.41-2.el7.noarch                                  1/6
  Verifying  : httpd-tools-2.4.6-90.el7.centos.x86_64                       2/6
  Verifying  : apr-util-1.5.2-6.el7.x86_64                                  3/6
  Verifying  : httpd-2.4.6-90.el7.centos.x86_64                             4/6
  Verifying  : apr-1.4.8-5.el7.x86_64                                       5/6
  Verifying  : centos-logos-70.0.6-3.el7.centos.noarch                      6/6

Installed:
  httpd.x86_64 0:2.4.6-90.el7.centos

Dependency Installed:
  apr.x86_64 0:1.4.8-5.el7
  apr-util.x86_64 0:1.5.2-6.el7
  centos-logos.noarch 0:70.0.6-3.el7.centos
  httpd-tools.x86_64 0:2.4.6-90.el7.centos
  mailcap.noarch 0:2.1.41-2.el7

Complete!
Removing intermediate container 9ef4c5080415
 ---> 114e1e9151fe
Step 5/6 : RUN mkdir -p /etc/mail
 ---> Running in 7e0a435fc46d
Removing intermediate container 7e0a435fc46d
 ---> 6608a26865b5
Step 6/6 : RUN mkdir -p /tmp/siddhesh
 ---> Running in dc5b6585f487
Removing intermediate container dc5b6585f487
 ---> 8bc3d4f2d628
Successfully built 8bc3d4f2d628
Successfully tagged apache_centos:DockerFile
[root@siddhesh ~]#

Once the build process completes, run following command to see the newly created image using Dockerfile.


[root@siddhesh ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
apache_centos       DockerFile          8bc3d4f2d628        2 minutes ago       599MB
centos              7                   5e35e350aded        4 months ago        203MB
[root@siddhesh ~]#


You can verify its author and more details using docker inspect command.


[root@siddhesh ~]# docker inspect 
docker inspect 8bc3d4f2d628
[
    {
        "Id": "sha256:8bc3d4f2d628503320df7b4f42caa3f2225139103643dda218ebbf6b37aee1ec",
        "RepoTags": [
            "apache_centos:DockerFile"
        ],
        "RepoDigests": [],
        "Parent": "sha256:6608a26865b5aa4ed9486d885e0ee4ae101fac879ad394cc8264d2a501365f08",
        "Comment": "",
        "Created": "2020-04-07T17:41:06.030180259Z",
        "Container": "dc5b6585f487cd6f299afd671427c578a26c7fc3ba225131371341b7d0178f32",
        "ContainerConfig": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "mkdir -p /tmp/siddhesh"
            ],
            "Image": "sha256:6608a26865b5aa4ed9486d885e0ee4ae101fac879ad394cc8264d2a501365f08",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "org.label-schema.build-date": "20191001",
                "org.label-schema.license": "GPLv2",
                "org.label-schema.name": "CentOS Base Image",
                "org.label-schema.schema-version": "1.0",
                "org.label-schema.vendor": "CentOS"
            }
        },
        "DockerVersion": "19.03.4",
        "Author": "Siddhesh Kadam siddhesh@tecgeek.info",
        "Config": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/bash"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:6608a26865b5aa4ed9486d885e0ee4ae101fac879ad394cc8264d2a501365f08",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "org.label-schema.build-date": "20191001",
                "org.label-schema.license": "GPLv2",
                "org.label-schema.name": "CentOS Base Image",
                "org.label-schema.schema-version": "1.0",
                "org.label-schema.vendor": "CentOS"
            }
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 598771646,
        "VirtualSize": 598771646,
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/66d706262f30f87f7cc56a2b4d3ef8a5b4bdf9e768aa3e2c95c88bc95568e30a/diff:/var/lib/docker/overlay2/85b9608cd1ad3c839a492e663c9c7eefa4491f11f148286ced6ab3cb46393914/diff:/var/lib/docker/overlay2/9713e2399502959db40eda79c5a754c9bd2e990331b609ce354c170edf20e159/diff:/var/lib/docker/overlay2/124eb368a97a5dec6e0b01ed7576d040e29076e8d5a58bc7008b43a8ff63ac93/diff",
                "MergedDir": "/var/lib/docker/overlay2/7297a1b073fcd04dc8603b1fbd87b70d86be3659d1751d9bce23d92acfe9f988/merged",
                "UpperDir": "/var/lib/docker/overlay2/7297a1b073fcd04dc8603b1fbd87b70d86be3659d1751d9bce23d92acfe9f988/diff",
                "WorkDir": "/var/lib/docker/overlay2/7297a1b073fcd04dc8603b1fbd87b70d86be3659d1751d9bce23d92acfe9f988/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:77b174a6a187b610e4699546bd973a8d1e77663796e3724318a2a4b24cb07ea0",
                "sha256:19b25d87d66b2fedce1264d3022c6e68da83850488ae473e3ac672ed8cdfb3fa",
                "sha256:cdf91c18ff3b17f4d3a96537739a8bf195eeb817191d591675449715ffb8c234",
                "sha256:bf93daff45ebfb71fab99087949c3aa38bb03367cd35488ac3e8348a75146bf2",
                "sha256:2ff5e40a310c29260cc276d09f590fb513145f068b5d6450f03d76c3b1f29728"
            ]
        },
        "Metadata": {
            "LastTagTime": "2020-04-07T17:41:06.10870629Z"
        }
    }
]
[root@siddhesh ~]#


bottom of page