Docker_Image_Build_Docker-file

Docker_Image_Build_Docker-file

Docker File

Docker builds images automatically by reading the instructions from a Dockerfile — a text file that contains all commands, in order, needed to build a given image

In this example, I am going to setup the WordPress site(Only Application) with the Ubuntu image. these are required packages.

  1. Apache
  2. PHP
  3. WordPress file

First, create "dockerfile" with all requirement.


[root@rhel7 docker]# cat dockerfile
FROM ubuntu:latest
LABEL maintainer="bibhutimail@gmail.com"
RUN export https_proxy="http://XXX.XX.XX.XX:8080"
RUN export http_proxy="http://XXX.XX.XX.XX:8080"
RUN apt-get update && apt install -y apache2 && apt install -y php && apt install -y libapache2-mod-php && apt install -y php-mysql && apt install -y php-pear && apt install -y php-fpm

Now build new docker image with help of "dockerfile.

[root@rhel7 docker]# docker build -t feenixdv/feenixdv_web:1.0 .
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM ubuntu:latest
 —> 93fd78260bd1
Step 2 : LABEL maintainer "bibhutimail@gmail.com"
 —> Using cache
 —> 97d02e86f7e0
.
.
.
.
debconf: falling back to frontend: Readline
Configuring tzdata
——————

Please select the geographic area in which you live. Subsequent configuration
questions will narrow this down by presenting a list of cities, representing
the time zones in which they are located.

  1. Africa      4. Australia  7. Atlantic  10. Pacific  13. Etc
  2. America     5. Arctic     8. Europe    11. SystemV
  3. Antarctica  6. Asia       9. Indian    12. US
Geographic area: 9

Terminal stop/hang on this section "please select the geographic area in which you live docker"
Update Dockerfile.

[root@rhel7 docker]# cat dockerfile
FROM ubuntu:latest
LABEL maintainer="bibhutimail@gmail.com"
RUN export https_proxy="http://XXX.XX.XX.XX:8080"
RUN export http_proxy="http://XXX.XX.XX.XX:8080"
RUN apt-get update
RUN apt-get install -y tzdata
#ENV TZ Asia/india
RUN apt install -y apache2 && apt install -y php && apt install -y libapache2-mod-php && apt install -y php-mysql && apt install -y php-pear && apt install -y php-fpm

[root@rhel7 docker]# docker build -t feenixdv/feenixdv_web:1.0 .
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM ubuntu:latest
 —> 93fd78260bd1
Step 2 : LABEL maintainer "bibhutimail@gmail.com"
 —> Using cache
 —> 97d02e86f7e0
.
.
.
php_invoke: Enabled module pdo_mysql for fpm sapi
php_invoke: Enabled module dom for fpm sapi
php_invoke: Enabled module simplexml for fpm sapi
php_invoke: Enabled module wddx for fpm sapi
php_invoke: Enabled module xml for fpm sapi
php_invoke: Enabled module xmlreader for fpm sapi
php_invoke: Enabled module xmlwriter for fpm sapi
php_invoke: Enabled module xsl for fpm sapi
NOTICE: Not enabling PHP 7.2 FPM by default.
NOTICE: To enable PHP 7.2 FPM in Apache2 do:
NOTICE: a2enmod proxy_fcgi setenvif
NOTICE: a2enconf php7.2-fpm
NOTICE: You are seeing this message because you have apache2 package installed.
invoke-rc.d: could not determine current run level
invoke-rc.d: policy-rc.d denied execution of start.
Setting up php-fpm (1:7.2+60ubuntu1) …
Processing triggers for libc-bin (2.27-3ubuntu1) …
 —> 05b5fd5f944a
Removing intermediate container e5ed499128f0
Successfully built 05b5fd5f944a

Now we can see our Docker image created with all prerequisites(Apache, PHP..).


[root@rhel7 yum.repos.d]# docker images
REPOSITORY                              TAG                 IMAGE ID            CREATED             SIZE
feenixdv/feenixdv_web                   1.0                 633bd89f2cb6        5 hours ago         300 MB

 

Docker_Import_Export

Docker_Import_Export

Import and Export Docker image

If we have any configured container and we want to save all changes(commit) in Docker image we use theses process.
Also, we can export these image and run container with help of these image, in a different machine.  

 

First check “CONTAINER ID” which you want to import using below command.

[root@rhel7 ~]# docker ps -a
CONTAINER ID        IMAGE                                  COMMAND                  CREATED             STATUS                   PORTS                    NAMES
0b834a2e1484        docker.io/ubuntu                       "/bin/bash"              7 hours ago         Up 7 hours               0.0.0.0:3306->3306/tcp   ubuntu_mysql
c92a3e450ca1        docker.io/ubuntu                       "/bin/bash"              13 days ago         Up 8 hours               0.0.0.0:82->80/tcp       ubuntu_apache

Commit all changes of a container which make Docker image.

Here “feenix_web” is name of Docker image and “c92a3e450ca1” is container ID.

[root@rhel7 ~]# docker commit c92a3e450ca1 feenix_web
sha256:b91f1f0109001b6e94db3d0c439136232736d88732db301b93184196bacac725

After commit we have all changes save in new Docker image(Apache + PHP + WordPress setting.. etc)

 

Check new Docker image created or not.

[root@rhel7 ~]# docker images
REPOSITORY                              TAG                 IMAGE ID            CREATED             SIZE
feenix_web                              latest              b91f1f010900        3 minutes ago       338.8 MB

 

Save Docker image as TAR or export to other Docker VM.

In this example, I’m going to save docker image in /tmp

[root@rhel7 ~]# docker save -o /tmp/feenix_web.tar feenix_web
[root@rhel7 ~]# ll /tmp/
total 340732
-rw——-. 1 root root 348898304 Oct 30 19:16 feenix_web.tar
 

Now copy this Docker image TAR file to other machine and start the container.

 

For example, you copy these image in /tmp location on another machine.

Note:- Docker must be installed in a machine(VM).

[root@rhel7 ~]# docker load < /tmp/ feenix_web.tar

Now list Docker image
[root@rhel7 ~]# docker images
Docker image listed with ”feenix_web

Now start container with ”feenix_web” docker image.

[root@rhel7 ~]# docker run -it –name ubuntu_apache -p 82:80 feenix_web