technical skills grow

Responsive Ads Here

Saturday, October 31, 2020

Docker

[root@dev ~]#yum install docker

Installed:
  docker.x86_64 2:1.13.1-162.git64e9980.el7.centos              

[root@dev ~]# systemctl start docker
[root@dev ~]# systemctl enable docker
 

We can search available images in docker repository  

[root@dev ~]# docker search mysql
INDEX       NAME                                        DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/mysql                             MySQL is a widely used, open-source relati...   10120     [OK]       
docker.io   docker.io/mariadb                           MariaDB is a community-developed fork of M...   3715      [OK]       
docker.io   docker.io/mysql/mysql-server                Optimized MySQL Server Docker images. Crea...   739                  [OK]
docker.io   docker.io/percona                           Percona Server is a fork of the MySQL rela...   511       [OK]       
docker.io   docker.io/centos/mysql-57-centos7           MySQL 5.7 SQL database server                   84                   
docker.io   docker.io/mysql/mysql-cluster               Experimental MySQL Cluster Docker images. ...   77                   


[root@dev docker]# docker pull mysql

Using default tag: latest
Trying to pull repository docker.io/library/mysql ...
latest: Pulling from docker.io/library/mysql
bb79b6b2107f: Pull complete
49e22f6fb9f7: Pull complete   

[root@dev ~]# docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

docker.io/mysql     latest              db2b37ec6181        8 days ago          545 MB


How to deploy first container 

Type of two mode 

1 . root mode (It is temporary If you logout machine is stop )  

2. detach mode (It is permanent running. it is running at back end as deamon) 

 1 . root mode : create container

[root@dev ~]# docker run -it --name con3 0d120b6ccaa8
[root@d908ff95cd56 /]#

Check it take login in same machine other terminal

[root@dev ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
d908ff95cd56        0d120b6ccaa8        "/bin/bash"         About a minute ago   Up About a minute                       con3
 

[root@dev ~]# docker top con1  (It is show total running process in container)

UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                12676               12664               0                   14:13               pts/1               00:00:00            /bin/bash
 

[root@dev ~]# docker ps -a  (It is show available container running or not)

 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
d908ff95cd56        0d120b6ccaa8        "/bin/bash"              4 minutes ago       Up 4 minutes                                    con3
0938d29378c5        f83a2938370c        "container-entrypo..."   8 minutes ago       Exited (1) 8 minutes ago                        con2
077c41c17301        db2b37ec6181        "docker-entrypoint..."   23 minutes ago      Exited (1) 22 minutes ago                       con1
a8b8f648899e        db2b37ec6181        "docker-entrypoint..."   28 minutes ago      Exited (1) 25 minutes ago                       mysql1
6789f4dd1306        db2b37ec6181        "docker-entrypoint..."   30 minutes ago      Exited (1) 29 minutes ago                       mysql_ 

[root@dev ~]# docker rm con1 ( We can remove con1)

Note :

1.If you want to logout from container using ctr + p + q ( Container is running)

2. Exit : you should not logout using exit command because (it is stop container )  

Set Hostname :

[root@dev ~]# docker run -itd --name con4 --hostname con4 0d120b6ccaa8
218facfdd9b1902567992d77302495161d217018aab2661b3e7023f7dc80ef58
[root@dev ~]# docker start con4
con4
[root@dev ~]# docker attach con4

Stop All container :

[root@dev ~]# docker stop `docker ps -q -a`
218facfdd9b1
9a338c2d1bb9
d908ff95cd56 

Remove All container :

[root@dev ~]# docker rm `docker ps -q -a`
218facfdd9b1
9a338c2d1bb9
d908ff95cd56

How to save container changes

[root@dev ~]# docker run -it 0d120b6ccaa8
 

[root@e4b3947f6fc2 /]# yum install php -y 

Failed to set locale, defaulting to C.UTF-8
CentOS-8 - AppStream                                                                                           3.8 MB/s | 5.8 MB     00:01    
CentOS-8 - Base                                                                                                1.1 MB/s | 2.2 MB     00:02    
CentOS-8 - Extras                                                                                               10 kB/s | 8.1 kB     00:00    
Dependencies resolved.
===============================================================================================================================================
 Package                           Architecture          Version                                                Repository                Size
===============================================================================================================================================
Installing:
 php                               x86_64                7.2.24-1.module_el8.2.0+313+b04d0a66                   AppStream                1.5 M
Installing dependencies:
 apr                               x86_64                1.6.3-9.el8                                            AppStream                125 k
 apr-util                          x86_64                1.6.1-6.el8                  

Logout from container using ctr + p+ q

[root@dev ~]# docker ps -a  ( check container ID)


CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
e4b3947f6fc2        0d120b6ccaa8        "/bin/bash"         4 minutes ago       Up 4 minutes                            gracious_elion
 

[root@dev ~]# docker stop e4b3947f6fc2   (Stop container)
e4b3947f6fc2
 

Save changes in image :

[root@dev ~]# docker commit e4b3947f6fc2 centos:php5.6  sha256:ec0671bdc5a2abf2560c46fac6caaf6eb423ba5c27bc10711e69ec9707313943

[root@dev ~]# docker images

REPOSITORY                          TAG                 IMAGE ID            CREATED             SIZE

centos                              php5.6              ec0671bdc5a2        12 seconds ago      282 MB
docker.io/mysql                     latest              db2b37ec6181        8 days ago          545 MB
docker.io/centos                    latest              0d120b6ccaa8        2 months ago        215 MB
docker.io/centos/mysql-57-centos7   latest              f83a2938370c        12 months ago       452 MB

How to take docker image backup and recover:

 Take backup image php5.6

[root@dev ~]# docker images
REPOSITORY                          TAG                 IMAGE ID            CREATED             SIZE
centos                              php5.6              ec0671bdc5a2        8 minutes ago       282 MB
docker.io/mysql                     latest              db2b37ec6181        8 days ago          545 MB
docker.io/centos                    latest              0d120b6ccaa8        2 months ago        215 MB
docker.io/centos/mysql-57-centos7   latest              f83a2938370c        12 months ago       452 MB
 

Backup image :

[root@dev ~]# docker save centos:php5.6 > centos-php.5.6.tar.gz
[root@dev ~]# ls
 centos-php.5.6.tar.gz

If you remove image by mistake suppose

[root@dev ~]# docker rmi ec0671bdc5a2
Untagged: centos:php5.6
Deleted: sha256:ec0671bdc5a2abf2560c46fac6caaf6eb423ba5c27bc10711e69ec9707313943
Deleted: sha256:6e397e6f06bd76e013b37021c31a05725d2496b82de16323aec315accffd9411
[root@dev ~]# docker images
REPOSITORY                          TAG                 IMAGE ID            CREATED             SIZE
docker.io/mysql                     latest              db2b37ec6181        8 days ago          545 MB
docker.io/centos                    latest              0d120b6ccaa8        2 months ago        215 MB
docker.io/centos/mysql-57-centos7   latest              f83a2938370c        12 months ago       452 MB

Recovery image (because it docker image show in binary format )

[root@dev ~]# docker load -i centos-php.5.6.tar.gz
d7f354f1f90c: Loading layer [==================================================>] 67.57 MB/67.57 MB
Loaded image: centos:php5.6
[root@dev ~]# docker images
REPOSITORY                          TAG                 IMAGE ID            CREATED             SIZE
centos                              php5.6              ec0671bdc5a2        15 minutes ago      282 MB
docker.io/mysql                     latest              db2b37ec6181        8 days ago          545 MB
docker.io/centos                    latest              0d120b6ccaa8        2 months ago        215 MB
docker.io/centos/mysql-57-centos7   latest              f83a2938370c        12 months ago       452 MB

Docker Socket Binding : Need How to install Service running container 

[root@dev ~]# docker images
REPOSITORY                          TAG                 IMAGE ID            CREATED             SIZE
centos                              apache8.0.2         d8e22f533cbe        3 days ago          347 MB
centos                              php5.6              ec0671bdc5a2        6 days ago          282 MB
docker.io/mysql                     latest              db2b37ec6181        2 weeks ago         545 MB
docker.io/centos                    latest              0d120b6ccaa8        2 months ago        215 MB
docker.io/centos/mysql-57-centos7   latest              f83a2938370c        13 months ago       452 MB 

[root@dev conf.d]# docker run -d -p 192.168.105.62:80:80 --name web1 0d120b6ccaa8
79203e7a352b7018a83c2aba96e0cb4b66050dae6c2b0e1e94e3e4bc2d2ec806
[root@dev conf.d]# cd
[root@dev ~]# docker start web1
web1
[root@dev ~]# docker attach web1

[root@df0a24b1e56b /]# echo "This docker web server 1 " > /var/www/html/index.html
[root@df0a24b1e56b /]# /usr/sbin/httpd
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.3. Set the 'ServerName' directive globally to suppress this message

Check Apache is running or not
[root@dev ~]# docker top web1
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                15522               15511               0                   00:34               pts/1               00:00:00            /bin/bash
root                15664               15522               0                   00:36               ?                   00:00:00            /usr/sbin/httpd
apache              15665               15664               0                   00:36               ?                   00:00:00            /usr/sbin/httpd
apache              15666               15664               0                   00:36               ?                   00:00:00            /usr/sbin/httpd
apache              15667               15664               0                   00:36               ?                   00:00:00            /usr/sbin/httpd
apache              15668               15664               0                   00:36               ?                   00:00:00            /usr/sbin/httpd

[root@dev conf.d]# docker run -d -p 192.168.105.62:80:80 --name web2 0d120b6ccaa8
79203e7a352b7018a83c2aba96e0cb4b66050dae6c2b0e1e94e3e4bc2d2ec806
[root@dev conf.d]# cd
[root@dev ~]# docker start web2
web1
[root@dev ~]# docker attach web2

[root@df0a24b1e56b /]# echo "This docker web server 2 " > /var/www/html/index.html
[root@df0a24b1e56b /]# /usr/sbin/httpd

Check docker image commit history

[root@dev ~]# docker history apache:centos

Docker File:

suppose that we have to create container and install apache ,php and run some script etc . so with the help docker file we can write daily task in docker file and execute .

[root@dev]#mkdir  docker_project

[root@dev docker_project]# vim dockerfile  

FROM 0d120b6ccaa8
MAINTAINER tarun@skymetweather.com
RUN  yum install httpd -y
COPY index.html /var/www/html/index.html
ADD abc.sh /root
run sh /root/abc.sh
EXPOSE  80
CMD  /usr/sbin/httpd -DFOREGROUND

 [root@dev docker_project]# echo "This is docker file based hosting" > index.html  [root@dev docker_project]#vim  abc.sh

 [root@dev docker_project]# docker build -t centos:apache8.2  .

 [root@dev docker_project]# docker images

REPOSITORY                          TAG                 IMAGE ID            CREATED              SIZE

centos                              tarun               f0412fb7615c        About a minute ago   254 MB

centos                              apache8.0.2         d8e22f533cbe        4 days ago           347 MB

centos                              php5.6              ec0671bdc5a2        7 days ago           282 MB

docker.io/mysql                     latest              db2b37ec6181        2 weeks ago          545 MB

docker.io/centos                    latest              0d120b6ccaa8        2 months ago         215 MB

docker.io/centos/mysql-57-centos7   latest              f83a2938370c        13 months ago        452 MB

 

Now we are use this image create container and use all services 

[root@dev docker_project]# docker run -itdP  --name test centos:apache

[root@dev docker_project]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS                   NAMES
30e9a4e4eb7a        centos:tarun        "/bin/sh -c '/usr/..."   5 minutes ago       Up 5 minutes                  0.0.0.0:32768->80/tcp   test
9c9713e8aec1  

Note : 

Capital P : is used it is bind apache at free port 32768

Small p :is used to bind the apache service at particular port

If you want to add new service in same image:

[root@dev docker_project]# vi dockerfile  

FROM 0d120b6ccaa8

MAINTAINER tarun@skymetweather.com

RUN  yum install httpd -y

COPY index.html /var/www/html/index.html

ADD abc.sh /root

RUN sh /root/abc.sh

RUN  yum install vsftpd -y

EXPOSE  80

CMD  /usr/sbin/httpd -DFOREGROUND

 

[root@dev docker_project]# docker build -t centos:tarun .

Sending build context to Docker daemon 4.096 kB

Step 1/9 : FROM 0d120b6ccaa8

 ---> 0d120b6ccaa8

Step 2/9 : MAINTAINER tarun@skymetweather.com

 ---> Using cache

 ---> d0c9acc9cedc

Step 3/9 : RUN yum install httpd -y

 ---> Using cache

 ---> 5a57eb1caddc

Step 4/9 : COPY index.html /var/www/html/index.html

 ---> Using cache

 ---> 783217da0478

Step 5/9 : ADD abc.sh /root

 ---> Using cache

 ---> 6619ce582e74

Step 6/9 : RUN sh /root/abc.sh

 ---> Using cache

 ---> 74098b6bc875

Step 7/9 : RUN yum install vsftpd -y

 ---> Running in 198979541f4f

Docker volume :

[root@dev code]# docker images

 

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

centos              tarun               be53fc4a5de5        23 hours ago        277 MB

centos              apache8.0.2         d8e22f533cbe        5 days ago          347 MB

centos              php5.6              ec0671bdc5a2        8 days ago          282 MB

docker.io/centos    latest              0d120b6ccaa8        2 months ago        215 MB

 

[root@dev]#mkdir /code   (Create directory at base machine for keep data at base machine)

[root@dev]#echo "This code at base machine " > code/index.html   

[root@dev ~]# docker run -itd -p 192.168.105.62:8080:80 -v /code:/var/www/html --name web2 0d120b6ccaa8

430e7fc764c6a0393518106c9a708926f99e9dbbcc76bc124290a5361a950271
[root@dev ~]# docker attach web2
[root@430e7fc764c6 /]# yum install httpd -y
Failed to set locale, defaulting to C.UTF-8
CentOS-8 - AppStream                                                                                           1.2 MB/s | 5.8 MB     00:04    
CentOS-8 - Base                                                                                                1.9 MB/s | 2.2 MB     00:01    
CentOS-8 - Extras                                                                                              9.9 kB/s | 8.1 kB     00:00    
Dependencies resolved.
===============================================================================================================================================
 Package                           Architecture          Version                                                Repository                Size
===============================================================================================================================================
Installing:
 httpd               


 

Expose multiple port with docker container

[root@dev ~]# docker run -itd -p 192.168.105.62:2223:22 -p 192.168.105.62:8080:80 --name web-server 0d120b6ccaa8

 

Docker Network :

[root@dev ~]# docker network --help

Usage:    docker network COMMAND

Manage networks

Options:
      --help   Print usage

Commands:
  connect     Connect a container to a network
  create      Create a network
  disconnect  Disconnect a container from a network
  inspect     Display detailed information on one or more networks
  ls          List networks
  prune       Remove all unused networks
  rm          Remove one or more networks

[root@dev ~]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
bf3d4877576a        bridge              bridge              local
fb8978641bb4        host                host                local
a61a98c05ceb        none                null                local

We can find the container who is used bridge and  host or none driver

[root@dev ~]# docker network inspect  bridge 

[root@dev ~]# docker network inspect  bridge
[
    {
        "Name": "bridge",
        "Id": "bf3d4877576a726effbd41c326f682a9dc054b13affa39f986d3ad145e3d7dbc",
        "Created": "2020-11-09T15:20:32.860701112+05:30",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.17.0.0/16",
                    "Gateway": "172.17.0.1"

How to disconnect & connect driver  

[root@dev ~]# docker network disconnect bridge container-name

Example: 
[root@dev ~]# docker network disconnect bridge ssh
[root@dev ~]# docker network inspect  bridge

Create  docker own driver

[root@dev ~]# docker network create -d bridge my_bridge

224bd8d5b385f86e65b0ab02b5086c80337d721c69e4552ea47e35a1553949cc

[root@dev ~]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
bf3d4877576a        bridge                  bridge                local
fb8978641bb4        host                      host                   local
224bd8d5b385        my_bridge           bridge               local
 

[root@dev ~]# docker network connect my_bridge ssh

Check how many bridge connect with container

[root@dev ~]# docker inspect ssh | grep "bridge"
                "bridge": {
                "my_bridge": {

Remove own bridge

[root@dev ~]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
bf3d4877576a        bridge              bridge              local
fb8978641bb4        host                host                local
224bd8d5b385        my_bridge           bridge              local 

[root@dev ~]# docker network rm my_bridge

SET OWN BRIDGE DRIVER AND SET IP POOL

[root@dev ~]# docker network create prod --driver bridge --subnet 192.168.0.1/24
[root@dev ~]# docker run -itd --name my_container 0d120b6ccaa8
[root@dev ~]# docker network disconnect bridge my_container
[root@dev ~]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
bf3d4877576a        bridge              bridge              local
fb8978641bb4        host                host                local
a61a98c05ceb        none                null                local
4341550fae50        prod                bridge              local 

Connect container with own driver

[root@dev ~]# docker network connect prod my_container


[root@dev ~]# docker inspect my_container | grep -i "IP"
            "IpcMode": "",
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
                    "IPAMConfig": {},
                    "IPAddress": "192.168.0.2",
                    "IPPrefixLen": 24,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,


How to install docker mysql container 

docker pull mysql/mysql-server:latest

[root@dev ~]# docker logs mysql01
[Entrypoint] MySQL Docker Image 8.0.22-1.1.18
[Entrypoint] No password option specified for new database.
[Entrypoint]   A random onetime password will be generated.
[Entrypoint] Initializing database
Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.
[Entrypoint] GENERATED ROOT PASSWORD: W3Jyd0kYBISepnOJokaSis3JIq9u

[root@dev ~]# docker exec -it mysql01 mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 8.0.22

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Dba@123';
Query OK, 0 rows affected (0.04 sec)

How to build own docker registry server :

[root@dev ~]# yum install docker-registry  -y 



No comments:

Post a Comment

Powered by Blogger.

Labels

Contact Form

Name

Email *

Message *

Search This Blog

Blog Archive

Ad Code

Responsive Advertisement

Recent Posts