How to use dockerfiles to create container ?

In this article I am going to show how to create image from the dockerfile. A dockerfile is a text document that contains all the commands you would normally execute manually in order to build a Docker image.

Step 1 : I have created docker file in one a directory test2.

-bash-4.2# pwd
/root/test2

-bash-4.2# cat Dockerfile
## Setting environment and working directory
FROM rhel
MAINTAINER vickygup1
RUN yum update -y
RUN yum install httpd -y
RUN yum clean all
WORKDIR /tmp
ENV Hello version1

You may refer the article to know more about the syntax of Dockerfile.

Step 2 : Bit explanation on my dockerfile. I am going to install the httpd package and change my working directory to /tmp and adding one environment variable. Dockerfile help us to avoid issuing the command everytime while running container.

Step 3 : From the dockerfile I am going to create one image and using that image I will run container.

-bash-4.2# docker build -t local/demo1:tag1 .
Sending build context to Docker daemon  2.56 kB
Sending build context to Docker daemon
Step 0 : FROM rhel
—> 10acc31def5d
Step 1 : MAINTAINER vickygup1
—> Running in fc8ae3eed0e4
—> 586f56caa630
Removing intermediate container fc8ae3eed0e4
Step 2 : RUN yum update -y
—> Running in bafdd145ba46
Loaded plugins: product-id, subscription-manager
https://cdn.redhat.com/content/dist/rhel/server/7/7Server/x86_64/os/repodata/bacb8858a00f81e427cee86b6714f411a78fb4b0-comps-rhel-x86_64-server-7.                       xml.gz: [Errno 14] curl#6 – “Could not resolve host: cdn.redhat.com; Name or service not known”
Trying other mirror.
https://cdn.redhat.com/content/dist/rhel/server/7/7Server/x86_64/os/repodata/94782eadb60f106000b4038fcd69b72026903d2ad8fe64bfb39c63c87f8bd706-upd                       ateinfo.xml.gz: [Errno 14] curl#6 – “Could not resolve host: cdn.redhat.com; Name or service not known”
Trying other mirror.
Resolving Dependencies
–> Running transaction check
—> Package subscription-manager.x86_64 0:1.13.19-1.el7 will be updated
—> Package subscription-manager.x86_64 0:1.13.22-1.el7 will be an update
—> Package tzdata.noarch 0:2014j-1.el7 will be updated
—> Package tzdata.noarch 0:2015a-1.el7 will be an update
–> Finished Dependency Resolution

Dependencies Resolved

================================================================================
Package                 Arch      Version          Repository             Size
================================================================================
Updating:
subscription-manager    x86_64    1.13.22-1.el7    rhel-7-server-rpms    795 k
tzdata                  noarch    2015a-1.el7      rhel-7-server-rpms    432 k

Transaction Summary
================================================================================
Upgrade  2 Packages

Total download size: 1.2 M
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
warning: /var/cache/yum/x86_64/7Server/rhel-7-server-rpms/packages/tzdata-2015a-1.el7.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51                       : NOKEY
Public key for tzdata-2015a-1.el7.noarch.rpm is not installed
——————————————————————————–
Total                                              138 kB/s | 1.2 MB  00:08
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
Importing GPG key 0xFD431D51:
Userid     : “Red Hat, Inc. (release key 2) <security@redhat.com>”
Fingerprint: 567e 347a d004 4ade 55ba 8a5f 199e 2f91 fd43 1d51
Package    : redhat-release-server-7.1-1.el7.x86_64 (@koji-override-0/7.0)
From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
Importing GPG key 0x2FA658E0:
Userid     : “Red Hat, Inc. (auxiliary key) <security@redhat.com>”
Fingerprint: 43a6 e49c 4a38 f4be 9abf 2a53 4568 9c88 2fa6 58e0
Package    : redhat-release-server-7.1-1.el7.x86_64 (@koji-override-0/7.0)
From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Updating   : tzdata-2015a-1.el7.noarch                                    1/4
Updating   : subscription-manager-1.13.22-1.el7.x86_64                    2/4
Cleanup    : tzdata-2014j-1.el7.noarch                                    3/4
Cleanup    : subscription-manager-1.13.19-1.el7.x86_64                    4/4
Verifying  : subscription-manager-1.13.22-1.el7.x86_64                    1/4
Verifying  : tzdata-2015a-1.el7.noarch                                    2/4
Verifying  : tzdata-2014j-1.el7.noarch                                    3/4
Verifying  : subscription-manager-1.13.19-1.el7.x86_64                    4/4

Updated:
subscription-manager.x86_64 0:1.13.22-1.el7    tzdata.noarch 0:2015a-1.el7

Complete!
—> 9ffa51ca1d70
Removing intermediate container bafdd145ba46
Step 3 : RUN yum install httpd -y
—> Running in ea50ad41458f
Loaded plugins: product-id, subscription-manager
Resolving Dependencies
–> Running transaction check
—> Package httpd.x86_64 0:2.4.6-31.el7 will be installed
–> Processing Dependency: httpd-tools = 2.4.6-31.el7 for package: httpd-2.4.6-31.el7.x86_64
–> Processing Dependency: system-logos >= 7.92.1-1 for package: httpd-2.4.6-31.el7.x86_64
–> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-31.el7.x86_64
–> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-31.el7.x86_64
–> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-31.el7.x86_64
–> Running transaction check
—> Package apr.x86_64 0:1.4.8-3.el7 will be installed
—> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed
—> Package httpd-tools.x86_64 0:2.4.6-31.el7 will be installed
—> Package mailcap.noarch 0:2.1.41-2.el7 will be installed
—> Package redhat-logos.noarch 0:70.0.3-4.el7 will be installed
–> Finished Dependency Resolution

Dependencies Resolved

================================================================================
Package           Arch        Version            Repository               Size
================================================================================
Installing:
httpd             x86_64      2.4.6-31.el7       rhel-7-server-rpms      1.2 M
Installing for dependencies:
apr               x86_64      1.4.8-3.el7        rhel-7-server-rpms      103 k
apr-util          x86_64      1.5.2-6.el7        rhel-7-server-rpms       92 k
httpd-tools       x86_64      2.4.6-31.el7       rhel-7-server-rpms       79 k
mailcap           noarch      2.1.41-2.el7       rhel-7-server-rpms       31 k
redhat-logos      noarch      70.0.3-4.el7       rhel-7-server-rpms       13 M

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

Total download size: 14 M
Installed size: 18 M
Downloading packages:
——————————————————————————–
Total                                               68 kB/s |  14 MB  03:35
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : apr-1.4.8-3.el7.x86_64                                       1/6
Installing : apr-util-1.5.2-6.el7.x86_64                                  2/6
Installing : httpd-tools-2.4.6-31.el7.x86_64                              3/6
Installing : mailcap-2.1.41-2.el7.noarch                                  4/6
Installing : redhat-logos-70.0.3-4.el7.noarch                             5/6
Installing : httpd-2.4.6-31.el7.x86_64                                    6/6
Verifying  : redhat-logos-70.0.3-4.el7.noarch                             1/6
Verifying  : apr-1.4.8-3.el7.x86_64                                       2/6
Verifying  : mailcap-2.1.41-2.el7.noarch                                  3/6
Verifying  : httpd-2.4.6-31.el7.x86_64                                    4/6
Verifying  : apr-util-1.5.2-6.el7.x86_64                                  5/6
Verifying  : httpd-tools-2.4.6-31.el7.x86_64                              6/6

Installed:
httpd.x86_64 0:2.4.6-31.el7

Dependency Installed:
apr.x86_64 0:1.4.8-3.el7                  apr-util.x86_64 0:1.5.2-6.el7
httpd-tools.x86_64 0:2.4.6-31.el7         mailcap.noarch 0:2.1.41-2.el7
redhat-logos.noarch 0:70.0.3-4.el7

Complete!
—> 0aab5f0659d3
Removing intermediate container ea50ad41458f
Step 4 : RUN yum clean all
—> Running in 1d8a1b55646c
Loaded plugins: product-id, subscription-manager
Cleaning repos: rhel-7-server-rpms
Cleaning up everything
—> 168a1a10c352
Removing intermediate container 1d8a1b55646c
Step 5 : WORKDIR /tmp
—> Running in 9d84d8cc5a6c
—> 6c871aee9223
Removing intermediate container 9d84d8cc5a6c
Step 6 : ENV Hello version1
—> Running in a7f0817d03a0
—> 4a16ef98360b
Removing intermediate container a7f0817d03a0
Successfully built 4a16ef98360b

Step 4 : Image has been built successfully we can verify the same by issuing below command.

-bash-4.2# docker images
REPOSITORY                        TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
local/demo1                       tag1                4a16ef98360b        2 minutes ago       270.8 MB

Step 5 : Run the container using image created in step 3 and verify that everything is according to the configuration file.

-bash-4.2# docker run -ti –name check1 4a16ef98360b
[root@a85d0a125bf9 tmp]# pwd
/tmp

In configuration file we have given /tmp as default dir.

We have set one environment variable as well we can check that also.

[root@a85d0a125bf9 tmp]# echo $Hello
version1

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s