Create Custom Docker Images

0
848

To containerize we need a Dockerfile which is a text document that contains all the commands a user could call on the command line to assemble an image.

My Dockerfile looks like

vi Dockerfile
# we are using ‘centos’ base image
FROM centos:latest
#Author
MAINTAINER Emre
###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”]

And I have got a index.html file.Its looks like:

 <html>
 <header><title>Hello Sysaix Fans</title></header>
 <body>
 Hello man!
 </body>
 </html>

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 maps the container port and docker host 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.

[root@instance-4 dockerfolder]# docker build -t centos/new1 .
Sending build context to Docker daemon 3.072 kB
Step 1/9 : FROM centos:latest
Trying to pull repository registry.access.redhat.com/centos ... 
Trying to pull repository docker.io/library/centos ... 
latest: Pulling from docker.io/library/centos
256b176beaff: Pull complete 
Digest: sha256:6f6d986d425aeabdc3a02cb61c02abb2e78e57357e92417d6d58332856024faf
Status: Downloaded newer image for docker.io/centos:latest
 ---> 5182e96772bf
Step 2/9 : MAINTAINER Emre
 ---> Running in c515e39bc20e
 ---> 6705887093bf
Removing intermediate container c515e39bc20e
Step 3/9 : RUN yum clean all
 ---> Running in b034450135b5

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
 ---> 8e80aaa07823
Removing intermediate container b034450135b5
Step 4/9 : RUN yum -y update
 ---> Running in 4740c9af6522

Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
 * base: mirror.vcu.edu
 * extras: mirror.mojohost.com
 * updates: mirror.mojohost.com
Resolving Dependencies
--> Running transaction check
---> Package audit-libs.x86_64 0:2.8.1-3.el7 will be updated
---> Package audit-libs.x86_64 0:2.8.1-3.el7_5.1 will be an update
---> Package centos-release.x86_64 0:7-5.1804.1.el7.centos will be updated
---> Package centos-release.x86_64 0:7-5.1804.4.el7.centos will be an update
---> Package dracut.x86_64 0:033-535.el7 will be updated
---> Package dracut.x86_64 0:033-535.el7_5.1 will be an update
---> Package kpartx.x86_64 0:0.4.9-119.el7 will be updated
---> Package kpartx.x86_64 0:0.4.9-119.el7_5.1 will be an update
---> Package libblkid.x86_64 0:2.23.2-52.el7 will be updated
---> Package libblkid.x86_64 0:2.23.2-52.el7_5.1 will be an update
---> Package libmount.x86_64 0:2.23.2-52.el7 will be updated
---> Package libmount.x86_64 0:2.23.2-52.el7_5.1 will be an update
---> Package libuuid.x86_64 0:2.23.2-52.el7 will be updated
---> Package libuuid.x86_64 0:2.23.2-52.el7_5.1 will be an update
---> Package systemd.x86_64 0:219-57.el7 will be updated
---> Package systemd.x86_64 0:219-57.el7_5.1 will be an update
---> Package systemd-libs.x86_64 0:219-57.el7 will be updated
---> Package systemd-libs.x86_64 0:219-57.el7_5.1 will be an update
---> Package util-linux.x86_64 0:2.23.2-52.el7 will be updated
---> Package util-linux.x86_64 0:2.23.2-52.el7_5.1 will be an update
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package             Arch        Version                     Repository    Size
================================================================================
Updating:
 audit-libs          x86_64      2.8.1-3.el7_5.1             updates       99 k
 centos-release      x86_64      7-5.1804.4.el7.centos       updates       25 k
 dracut              x86_64      033-535.el7_5.1             updates      325 k
 kpartx              x86_64      0.4.9-119.el7_5.1           updates       76 k
 libblkid            x86_64      2.23.2-52.el7_5.1           updates      178 k
 libmount            x86_64      2.23.2-52.el7_5.1           updates      180 k
 libuuid             x86_64      2.23.2-52.el7_5.1           updates       81 k
 systemd             x86_64      219-57.el7_5.1              updates      5.0 M
 systemd-libs        x86_64      219-57.el7_5.1              updates      402 k
 util-linux          x86_64      2.23.2-52.el7_5.1           updates      2.0 M

Transaction Summary
================================================================================
Upgrade  10 Packages

Total download size: 8.4 M
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
Public key for centos-release-7-5.1804.4.el7.centos.x86_64.rpm is not installed
warning: /var/cache/yum/x86_64/7/updates/packages/centos-release-7-5.1804.4.el7.centos.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
--------------------------------------------------------------------------------
Total                                              8.3 MB/s | 8.4 MB  00:01     
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-5.1804.1.el7.centos.x86_64 (@Updates)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Updating   : libuuid-2.23.2-52.el7_5.1.x86_64                            1/20 
  Updating   : libblkid-2.23.2-52.el7_5.1.x86_64                           2/20 
  Updating   : libmount-2.23.2-52.el7_5.1.x86_64                           3/20 
  Updating   : systemd-libs-219-57.el7_5.1.x86_64                          4/20 
  Updating   : audit-libs-2.8.1-3.el7_5.1.x86_64                           5/20 
  Updating   : util-linux-2.23.2-52.el7_5.1.x86_64                         6/20 
  Updating   : centos-release-7-5.1804.4.el7.centos.x86_64                 7/20 
  Updating   : systemd-219-57.el7_5.1.x86_64                               8/20 
  Updating   : kpartx-0.4.9-119.el7_5.1.x86_64                             9/20 
  Updating   : dracut-033-535.el7_5.1.x86_64                              10/20 
  Cleanup    : dracut-033-535.el7.x86_64                                  11/20 
  Cleanup    : systemd-219-57.el7.x86_64                                  12/20 
  Cleanup    : util-linux-2.23.2-52.el7.x86_64                            13/20 
  Cleanup    : centos-release-7-5.1804.1.el7.centos.x86_64                14/20 
  Cleanup    : libmount-2.23.2-52.el7.x86_64                              15/20 
  Cleanup    : libblkid-2.23.2-52.el7.x86_64                              16/20 
  Cleanup    : libuuid-2.23.2-52.el7.x86_64                               17/20 
  Cleanup    : audit-libs-2.8.1-3.el7.x86_64                              18/20 
  Cleanup    : systemd-libs-219-57.el7.x86_64                             19/20 
  Cleanup    : kpartx-0.4.9-119.el7.x86_64                                20/20 
  Verifying  : systemd-219-57.el7_5.1.x86_64                               1/20 
  Verifying  : kpartx-0.4.9-119.el7_5.1.x86_64                             2/20 
  Verifying  : libuuid-2.23.2-52.el7_5.1.x86_64                            3/20 
  Verifying  : libblkid-2.23.2-52.el7_5.1.x86_64                           4/20 
  Verifying  : audit-libs-2.8.1-3.el7_5.1.x86_64                           5/20 
  Verifying  : util-linux-2.23.2-52.el7_5.1.x86_64                         6/20 
  Verifying  : systemd-libs-219-57.el7_5.1.x86_64                          7/20 
  Verifying  : libmount-2.23.2-52.el7_5.1.x86_64                           8/20 
  Verifying  : centos-release-7-5.1804.4.el7.centos.x86_64                 9/20 
  Verifying  : dracut-033-535.el7_5.1.x86_64                              10/20 
  Verifying  : kpartx-0.4.9-119.el7.x86_64                                11/20 
  Verifying  : util-linux-2.23.2-52.el7.x86_64                            12/20 
  Verifying  : systemd-libs-219-57.el7.x86_64                             13/20 
  Verifying  : libmount-2.23.2-52.el7.x86_64                              14/20 
  Verifying  : audit-libs-2.8.1-3.el7.x86_64                              15/20 
  Verifying  : libblkid-2.23.2-52.el7.x86_64                              16/20 
  Verifying  : centos-release-7-5.1804.1.el7.centos.x86_64                17/20 
  Verifying  : libuuid-2.23.2-52.el7.x86_64                               18/20 
  Verifying  : systemd-219-57.el7.x86_64                                  19/20 
  Verifying  : dracut-033-535.el7.x86_64                                  20/20 

Updated:
  audit-libs.x86_64 0:2.8.1-3.el7_5.1                                           
  centos-release.x86_64 0:7-5.1804.4.el7.centos                                 
  dracut.x86_64 0:033-535.el7_5.1                                               
  kpartx.x86_64 0:0.4.9-119.el7_5.1                                             
  libblkid.x86_64 0:2.23.2-52.el7_5.1                                           
  libmount.x86_64 0:2.23.2-52.el7_5.1                                           
  libuuid.x86_64 0:2.23.2-52.el7_5.1                                            
  systemd.x86_64 0:219-57.el7_5.1                                               
  systemd-libs.x86_64 0:219-57.el7_5.1                                          
  util-linux.x86_64 0:2.23.2-52.el7_5.1                                         

Complete!
 ---> 09c71ca7b09b
Removing intermediate container 4740c9af6522
Step 5/9 : RUN yum install -y httpd
 ---> Running in cf2b719de843

Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
 * base: mirror.vcu.edu
 * extras: mirror.mojohost.com
 * updates: mirror.mojohost.com
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-80.el7.centos.1 will be installed
--> Processing Dependency: httpd-tools = 2.4.6-80.el7.centos.1 for package: httpd-2.4.6-80.el7.centos.1.x86_64
--> Processing Dependency: system-logos >= 7.92.1-1 for package: httpd-2.4.6-80.el7.centos.1.x86_64
--> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-80.el7.centos.1.x86_64
--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-80.el7.centos.1.x86_64
--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-80.el7.centos.1.x86_64
--> Running transaction check
---> Package apr.x86_64 0:1.4.8-3.el7_4.1 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-80.el7.centos.1 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-80.el7.centos.1         updates      2.7 M
Installing for dependencies:
 apr               x86_64      1.4.8-3.el7_4.1               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-80.el7.centos.1         updates       90 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:
--------------------------------------------------------------------------------
Total                                               29 MB/s |  24 MB  00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : apr-1.4.8-3.el7_4.1.x86_64                                   1/6 
  Installing : apr-util-1.5.2-6.el7.x86_64                                  2/6 
  Installing : httpd-tools-2.4.6-80.el7.centos.1.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-80.el7.centos.1.x86_64                           6/6 
  Verifying  : mailcap-2.1.41-2.el7.noarch                                  1/6 
  Verifying  : httpd-tools-2.4.6-80.el7.centos.1.x86_64                     2/6 
  Verifying  : apr-util-1.5.2-6.el7.x86_64                                  3/6 
  Verifying  : httpd-2.4.6-80.el7.centos.1.x86_64                           4/6 
  Verifying  : apr-1.4.8-3.el7_4.1.x86_64                                   5/6 
  Verifying  : centos-logos-70.0.6-3.el7.centos.noarch                      6/6 
Installed:
  httpd.x86_64 0:2.4.6-80.el7.centos.1                                          
Dependency Installed:
  apr.x86_64 0:1.4.8-3.el7_4.1                                                  
  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-80.el7.centos.1                                    
  mailcap.noarch 0:2.1.41-2.el7                                                 
Complete!
 ---> 8769b9bb9903
Removing intermediate container cf2b719de843
Step 6/9 : COPY ./index.html /var/www/html/
 ---> 6148fbcb6eb4
Removing intermediate container e22f4e40f061
Step 7/9 : EXPOSE 80
 ---> Running in e388c9d5a1ed
 ---> 8fb59f9d8e91
Removing intermediate container e388c9d5a1ed
Step 8/9 : CMD [“-D”, “FOREGROUND”]
 ---> Running in 2fea17f43992
 ---> 26f23f43f3af
Removing intermediate container 2fea17f43992
Step 9/9 : ENTRYPOINT [“/usr/sbin/httpd”]
 ---> Running in 85b08db9b615
 ---> d7489adf5f2f
Removing intermediate container 85b08db9b615
Successfully built d7489adf5f2f

[root@instance-4 dockerfolder]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos/firstweb     latest              d7489adf5f2f        36 seconds ago      476 MB
docker.io/centos    latest              5182e96772bf        2 weeks ago         200 MB
[root@instance-4 dockerfolder]# 
root@instance-4 dockerfolder]# docker run -d -p 80:80 centos/new1
edaf3694003b302cdc8c713d6529f4440a4b7139bef5421c661884dba7301409
root@instance-4 dockerfolder]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1a121102d61f centos/new1 "/usr/sbin/httpd -D …" 10 minutes ago Up 10 minutes 0.0.0.0:80->80/tcp helloweb

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.