Skip to main content

Dockerfile, Create your own container image

Dockerfle is a very important concept to create you own container image. However there are lots of images available on docker hub but these are standard images and creators of these images are not aware about our requirement and environment. In this situation we need our own images. There are two way to create your own container images.

  • docker commit - We can create our own images using 'docker commit' command but only limited feature available in this approach. In below command centos is my existing container name and webserver:v1 is my new image name and version.
[root@server109 docker-ws]# docker pull centos
[root@server109 docker-ws]# docker container commit centos webserver:v1
  • docker build - A powerful approach to create a container image, We can customize our image as more as customization required. This approach is also known as Dockerfile approach.
As of now I have below images available in my environment but I need an image which has net-tools, httpd, python36 packages installed and when we launch it python prompt is available to any python code.
[root@server109 docker-ws]# docker image ls
REPOSITORY    TAG                   IMAGE ID            CREATED             SIZE
httpd                    latest                b2c2ab6dcf2e         2 weeks ago         166MB
mysql                  5.7                   413be204e9c3        5 weeks ago         456MB
ubuntu                14.04               6e4f1fe62ff1          4 months ago        197MB
centos                 7                     5e35e350aded         5 months ago        203MB
wordpress           5.1.1-php7.3-apache   a69f6702fdda    12 months ago       422MB

So let's start to create our own image-
Step 1:- Pull a standard image from docker hub
[root@server109 docker-ws]# docker pull centos

Step 2:- Prepare Dockerfile, Dockerfile is not name of any technology. It is just name of a text file in which we put our requirement and pass this file as input in docker image build / docker build command.
[root@server109 docker-ws]# mkdir /root/docker-ws
[root@server109 docker-ws]# cd /root/docker-ws/
[root@server109 docker-ws]# pwd
/root/docker-ws
[root@server109 docker-ws]# cat > Dockerfile
FROM centos
RUN yum install net-tools httpd python36 -y
CMD python3

Step 3:- Create/Build the image
[root@server109 docker-ws]# docker image ls |grep customized_centos
[root@server109 docker-ws]# docker image build -t customized_centos:v1 /root/docker-ws/
Sending build context to Docker daemon  2.048kB
Step 1/3 : FROM centos
latest: Pulling from library/centos
8a29a15cefae: Pull complete
Digest: sha256:fe8d824220415eed5477b63addf40fb06c3b049404242b31982106ac204f6700
Status: Downloaded newer image for centos:latest
 ---> 470671670cac
Step 2/3 : RUN yum install net-tools httpd python36 -y
 ---> Running in 48bf98d90276
CentOS-8 - AppStream                            2.8 MB/s | 7.0 MB     00:02
CentOS-8 - Base                                 1.9 MB/s | 2.2 MB     00:01
CentOS-8 - Extras                                11 kB/s | 5.9 kB     00:00
Dependencies resolved.
====================================================================
 Package           Arch   Version                               Repo       Size
====================================================================
Installing:
 httpd             x86_64 2.4.37-16.module_el8.1.0+256+ae790463 AppStream 1.7 M
 python36          x86_64 3.6.8-2.module_el8.1.0+245+c39af44f   AppStream  19 k
 net-tools         x86_64 2.0-0.51.20160912git.el8              BaseOS    323 k
Installing dependencies:
 apr               x86_64 1.6.3-9.el8                           AppStream 125 k
 apr-util          x86_64 1.6.1-6.el8                           AppStream 105 k
 centos-logos-httpd
                   noarch 80.5-2.el8                            AppStream  24 k
 httpd-filesystem  noarch 2.4.37-16.module_el8.1.0+256+ae790463 AppStream  35 k
 httpd-tools       x86_64 2.4.37-16.module_el8.1.0+256+ae790463 AppStream 103 k
 mod_http2         x86_64 1.11.3-3.module_el8.1.0+213+acce2796  AppStream 158 k
 python3-pip       noarch 9.0.3-15.el8                          AppStream  19 k
 brotli            x86_64 1.0.6-1.el8                           BaseOS    323 k
 mailcap           noarch 2.1.48-3.el8                          BaseOS     39 k
 python3-setuptools
                   noarch 39.2.0-5.el8                          BaseOS    162 k
Installing weak dependencies:
 apr-util-bdb      x86_64 1.6.1-6.el8                           AppStream  25 k
 apr-util-openssl  x86_64 1.6.1-6.el8                           AppStream  27 k
Enabling module streams:
 httpd                    2.4
 python36                 3.6

Transaction Summary
=====================================================================
Install  15 Packages

Total download size: 3.1 M
Installed size: 9.6 M
Downloading Packages:
(1/15): apr-util-bdb-1.6.1-6.el8.x86_64.rpm     276 kB/s |  25 kB     00:00
(2/15): apr-util-1.6.1-6.el8.x86_64.rpm         1.1 MB/s | 105 kB     00:00
(3/15): apr-1.6.3-9.el8.x86_64.rpm              1.1 MB/s | 125 kB     00:00
(4/15): centos-logos-httpd-80.5-2.el8.noarch.rp 603 kB/s |  24 kB     00:00
(5/15): apr-util-openssl-1.6.1-6.el8.x86_64.rpm 477 kB/s |  27 kB     00:00
(6/15): httpd-2.4.37-16.module_el8.1.0+256+ae79 3.7 MB/s | 1.7 MB     00:00
(7/15): mod_http2-1.11.3-3.module_el8.1.0+213+a 711 kB/s | 158 kB     00:00
(8/15): python3-pip-9.0.3-15.el8.noarch.rpm     954 kB/s |  19 kB     00:00
(9/15): python36-3.6.8-2.module_el8.1.0+245+c39 258 kB/s |  19 kB     00:00
(10/15): httpd-filesystem-2.4.37-16.module_el8.  32 kB/s |  35 kB     00:01
(11/15): mailcap-2.1.48-3.el8.noarch.rpm        333 kB/s |  39 kB     00:00
(12/15): brotli-1.0.6-1.el8.x86_64.rpm          647 kB/s | 323 kB     00:00
(13/15): python3-setuptools-39.2.0-5.el8.noarch 1.2 MB/s | 162 kB     00:00
(14/15): net-tools-2.0-0.51.20160912git.el8.x86 1.3 MB/s | 323 kB     00:00
(15/15): httpd-tools-2.4.37-16.module_el8.1.0+2  32 kB/s | 103 kB     00:03
--------------------------------------------------------------------------------
Total                                           769 kB/s | 3.1 MB     00:04
warning: /var/cache/dnf/AppStream-02e86d1c976ab532/packages/apr-1.6.3-9.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY
CentOS-8 - AppStream                             32 kB/s | 1.6 kB     00:00
Importing GPG key 0x8483C65D:
 Userid     : "CentOS (CentOS Official Signing Key) <security@centos.org>"
 Fingerprint: 99DB 70FA E1D7 CE22 7FB6 4882 05B5 55B3 8483 C65D
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                        1/1
  Installing       : apr-1.6.3-9.el8.x86_64                                1/15
  Running scriptlet: apr-1.6.3-9.el8.x86_64                                1/15
  Installing       : apr-util-bdb-1.6.1-6.el8.x86_64                       2/15
  Installing       : apr-util-openssl-1.6.1-6.el8.x86_64                   3/15
  Installing       : apr-util-1.6.1-6.el8.x86_64                           4/15
  Running scriptlet: apr-util-1.6.1-6.el8.x86_64                           4/15
  Installing       : httpd-tools-2.4.37-16.module_el8.1.0+256+ae790463.    5/15
  Installing       : python3-setuptools-39.2.0-5.el8.noarch                6/15
  Installing       : python36-3.6.8-2.module_el8.1.0+245+c39af44f.x86_6    7/15
  Running scriptlet: python36-3.6.8-2.module_el8.1.0+245+c39af44f.x86_6    7/15
  Installing       : python3-pip-9.0.3-15.el8.noarch                       8/15
  Installing       : mailcap-2.1.48-3.el8.noarch                           9/15
  Installing       : brotli-1.0.6-1.el8.x86_64                            10/15
  Running scriptlet: httpd-filesystem-2.4.37-16.module_el8.1.0+256+ae79   11/15
  Installing       : httpd-filesystem-2.4.37-16.module_el8.1.0+256+ae79   11/15
  Installing       : centos-logos-httpd-80.5-2.el8.noarch                 12/15
  Installing       : mod_http2-1.11.3-3.module_el8.1.0+213+acce2796.x86   13/15
  Installing       : httpd-2.4.37-16.module_el8.1.0+256+ae790463.x86_64   14/15
  Running scriptlet: httpd-2.4.37-16.module_el8.1.0+256+ae790463.x86_64   14/15
  Installing       : net-tools-2.0-0.51.20160912git.el8.x86_64            15/15
  Running scriptlet: net-tools-2.0-0.51.20160912git.el8.x86_64            15/15
  Running scriptlet: httpd-2.4.37-16.module_el8.1.0+256+ae790463.x86_64   15/15
  Running scriptlet: net-tools-2.0-0.51.20160912git.el8.x86_64            15/15
  Verifying        : apr-1.6.3-9.el8.x86_64                                1/15
  Verifying        : apr-util-1.6.1-6.el8.x86_64                           2/15
  Verifying        : apr-util-bdb-1.6.1-6.el8.x86_64                       3/15
  Verifying        : apr-util-openssl-1.6.1-6.el8.x86_64                   4/15
  Verifying        : centos-logos-httpd-80.5-2.el8.noarch                  5/15
  Verifying        : httpd-2.4.37-16.module_el8.1.0+256+ae790463.x86_64    6/15
  Verifying        : httpd-filesystem-2.4.37-16.module_el8.1.0+256+ae79    7/15
  Verifying        : httpd-tools-2.4.37-16.module_el8.1.0+256+ae790463.    8/15
  Verifying        : mod_http2-1.11.3-3.module_el8.1.0+213+acce2796.x86    9/15
  Verifying        : python3-pip-9.0.3-15.el8.noarch                      10/15
  Verifying        : python36-3.6.8-2.module_el8.1.0+245+c39af44f.x86_6   11/15
  Verifying        : brotli-1.0.6-1.el8.x86_64                            12/15
  Verifying        : mailcap-2.1.48-3.el8.noarch                          13/15
  Verifying        : net-tools-2.0-0.51.20160912git.el8.x86_64            14/15
  Verifying        : python3-setuptools-39.2.0-5.el8.noarch               15/15

Installed:
  httpd-2.4.37-16.module_el8.1.0+256+ae790463.x86_64
  python36-3.6.8-2.module_el8.1.0+245+c39af44f.x86_64
  net-tools-2.0-0.51.20160912git.el8.x86_64
  apr-util-bdb-1.6.1-6.el8.x86_64
  apr-util-openssl-1.6.1-6.el8.x86_64
  apr-1.6.3-9.el8.x86_64
  apr-util-1.6.1-6.el8.x86_64
  centos-logos-httpd-80.5-2.el8.noarch
  httpd-filesystem-2.4.37-16.module_el8.1.0+256+ae790463.noarch
  httpd-tools-2.4.37-16.module_el8.1.0+256+ae790463.x86_64
  mod_http2-1.11.3-3.module_el8.1.0+213+acce2796.x86_64
  python3-pip-9.0.3-15.el8.noarch
  brotli-1.0.6-1.el8.x86_64
  mailcap-2.1.48-3.el8.noarch
  python3-setuptools-39.2.0-5.el8.noarch

Complete!
Removing intermediate container 48bf98d90276
 ---> 36ebc72e35c8
Step 3/3 : CMD python3
 ---> Running in e6e00e8d2edb
Removing intermediate container e6e00e8d2edb
 ---> d474dc1f3564
Successfully built d474dc1f3564
Successfully tagged customized_centos:v1

[root@server109 docker-ws]# docker image ls |grep customized_centos
customized_centos          v1                    d474dc1f3564        2 minutes ago       281MB

That's all, have a look on last command output. Your own image has been created. You can start using it. I added very few requirements in Dockerfile to make it easy to understand but you can put as per your requirement.

Comments

Popular posts from this blog

error: db5 error(11) from dbenv->open: Resource temporarily unavailable

If rpm command is not working in your system and it is giving an error message( error: db5 error(11) from dbenv->open: Resource temporarily unavailable ). What is the root cause of this issue? How to fix this issue?   just a single command- [root@localhost rpm]# rpm --rebuilddb Detailed error message- [root@localhost rpm]# rpm -q firefox ^Cerror: db5 error(11) from dbenv->open: Resource temporarily unavailable error: cannot open Packages index using db5 - Resource temporarily unavailable (11) error: cannot open Packages database in /var/lib/rpm ^Cerror: db5 error(11) from dbenv->open: Resource temporarily unavailable error: cannot open Packages database in /var/lib/rpm package firefox is not installed [root@localhost rpm]# RPM manage a database in which it store all information related to packages installed in our system. /var/lib/rpm, this is directory where this information is available. [root@localhost rpm]# cd /var/lib/rpm ...

Failed to get D-Bus connection: Operation not permitted

" Failed to get D-Bus connection: Operation not permitted " - systemctl command is not working in Docker container. If systemctl command is not working in your container and giving subjected error message then simple solution of this error is, create container with -- privileged option and also provide init file full path  /usr/sbin/init [root@server109 ~]# docker container run -dit --privileged --name systemctl_not_working_centos1 centos:7 /usr/sbin/init For detailed explanation and understanding I am writing more about it, please have look below. If we have a daemon based program(httpd, sshd, jenkins, docker etc.) running inside a container and we would like to start/stop or check status of daemon inside docker then it becomes difficult for us to perform such operations , because by default systemctl and service  commands don't work inside docker. Normally we run below commands to check services status in Linux systems. [root@server109 ~]# systemctl status ...

AWS cloud automation using Terraform

In this post I'll create multiple resources in AWS cloud using Terraform . Terraform is an infrastructure as code( IAC ) software which can do lots of things but it is superb in cloud automation. To use Terraform we have write code in a high-level configuration language known as Hashicorp Configuration Language , optionally we can write code in JSON as well. I'll create below service using Terraform- 1. Create the key-pair and security group which allow inbound traffic on port 80 and 22 2. Launch EC2 instance. 3. To create EC2 instance use same key and security group which created in step 1 4. Launch Volume(EBS) and mount this volume into /var/www/html directory 5. Upload index.php file and an image on GitHub repository 6. Clone GitHub repository into /var/www/html 7. Create S3 bucket, copy images from GitHub repo into it and set permission to public readable 8 Create a CloudFront use S3 bucket(which contains images) and use the CloudFront URL to update code in /var/w...