Wednesday, August 31, 2022

Docker Network

 Docker Network:







[root@ip-172-31-39-133 ~]# ifconfig
docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        inet6 fe80::42:baff:feb1:8229  prefixlen 64  scopeid 0x20<link>
        ether 02:42:ba:b1:82:29  txqueuelen 0  (Ethernet)
        RX packets 3478  bytes 201970 (197.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 3669  bytes 40278349 (38.4 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 9001
        inet 172.31.39.133  netmask 255.255.240.0  broadcast 172.31.47.255
        inet6 fe80::839:a1ff:feee:a9c4  prefixlen 64  scopeid 0x20<link>
        ether 0a:39:a1:ee:a9:c4  txqueuelen 1000  (Ethernet)
        RX packets 148043  bytes 144043751 (137.3 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 55507  bytes 63376063 (60.4 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 4  bytes 336 (336.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4  bytes 336 (336.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

veth07253cd: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::9898:72ff:fe27:e131  prefixlen 64  scopeid 0x20<link>
        ether 9a:98:72:27:e1:31  txqueuelen 0  (Ethernet)
        RX packets 31  bytes 2696 (2.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 85  bytes 6743 (6.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

veth2476f03: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::b868:acff:fe2b:7dca  prefixlen 64  scopeid 0x20<link>
        ether ba:68:ac:2b:7d:ca  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 67  bytes 4174 (4.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

veth4cb9aff: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::7ce8:34ff:fea3:481c  prefixlen 64  scopeid 0x20<link>
        ether 7e:e8:34:a3:48:1c  txqueuelen 0  (Ethernet)
        RX packets 145  bytes 13579 (13.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 194  bytes 12477 (12.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

veth6d62ecd: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::649b:22ff:fe21:ffa4  prefixlen 64  scopeid 0x20<link>
        ether 66:9b:22:21:ff:a4  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 12  bytes 892 (892.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

vetha672711: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::bc7e:c1ff:fe05:ebbd  prefixlen 64  scopeid 0x20<link>
        ether be:7e:c1:05:eb:bd  txqueuelen 0  (Ethernet)
        RX packets 19  bytes 2577 (2.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 31  bytes 2713 (2.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@ip-172-31-39-133 ~]# docker ps
CONTAINER ID   IMAGE      COMMAND                  CREATED         STATUS         PORTS                                     NAMES
c995d28b99c4   nginx      "/docker-entrypoint.…"   3 minutes ago   Up 3 minutes   0.0.0.0:49153->80/tcp, :::49153->80/tcp   thirsty_dubinsky

4f8882709160   nginx      "/docker-entrypoint.…"   6 minutes ago   Up 6 minutes   80/tcp                                    optimistic_tesla

0171ffaaa59e   copydemo   "nginx -g 'daemon of…"   14 hours ago    Up 14 hours    0.0.0.0:3365->80/tcp, :::3365->80/tcp     goofy_blackwell

b97ae09e1692   nginx      "/docker-entrypoint.…"   15 hours ago    Up 15 hours    0.0.0.0:3306->80/tcp, :::3306->80/tcp     priceless_hermann

5d3b1c485eb1   ubuntu     "bash"                   24 hours ago    Up 24 hours                                              amazing_wilson
[root@ip-172-31-39-133 ~]#


Create new container without passing port number:

[root@ip-172-31-39-133 ~]# docker run -itd -P nginx
c995d28b99c47ef0289ab4c679353931140a7c1eef68d3345a27377beebe1cdb
[root@ip-172-31-39-133 ~]# docker ps
CONTAINER ID   IMAGE      COMMAND                  CREATED         STATUS         PORTS                                     NAMES
c995d28b99c4   nginx      "/docker-entrypoint.…"   4 seconds ago   Up 3 seconds   0.0.0.0:49153->80/tcp, :::49153->80/tcp   thirsty_dubinsky

We can see container is running it took host port 49153 automatically.



[root@ip-172-31-39-133 ~]# docker network --help

Usage:  docker network COMMAND

Manage networks

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


Create new network:

[root@ip-172-31-39-133 ~]# docker network create -d bridge my_network
43f46e2b33a1e49805b4adb8b9f7304d4a4682e7182b094d975bee0110a7edf2
[root@ip-172-31-39-133 ~]# docker network ls
NETWORK ID     NAME         DRIVER    SCOPE
32e70e010db8   bridge       bridge    local
21f7ccbe7a2b   host         host      local
43f46e2b33a1   my_network   bridge    local
a2057ad1b8e7   none         null      local
[root@ip-172-31-39-133 ~]#

On creating new network we can see new interface also created.

[root@ip-172-31-39-133 ~]# ifconfig
br-43f46e2b33a1: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.19.0.1  netmask 255.255.0.0  broadcast 172.19.255.255
        ether 02:42:67:2e:c6:dd  txqueuelen 0  (Ethernet)
        RX packets 19  bytes 2577 (2.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 32  bytes 2783 (2.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0



[root@ip-172-31-39-133 ~]# docker container run -itd --network my_network nginx
7cd1dfca58a85b928bb56c96a3fd9286f20670aaf0e326e890eacdd8b9a8a1ac
[root@ip-172-31-39-133 ~]# docker ps
CONTAINER ID   IMAGE      COMMAND                  CREATED          STATUS          PORTS                                     NAMES
7cd1dfca58a8   nginx      "/docker-entrypoint.…"   5 seconds ago    Up 4 seconds    80/tcp                                    cool_saha




[root@ip-172-31-39-133 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 9001 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 0a:39:a1:ee:a9:c4 brd ff:ff:ff:ff:ff:ff
    inet 172.31.39.133/20 brd 172.31.47.255 scope global dynamic eth0
       valid_lft 3285sec preferred_lft 3285sec
    inet6 fe80::839:a1ff:feee:a9c4/64 scope link
       valid_lft forever preferred_lft forever
3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
    link/ether 02:42:ba:b1:82:29 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:baff:feb1:8229/64 scope link
       valid_lft forever preferred_lft forever
5: veth2476f03@if4: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default
    link/ether ba:68:ac:2b:7d:ca brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet6 fe80::b868:acff:fe2b:7dca/64 scope link
       valid_lft forever preferred_lft forever
13: veth4cb9aff@if12: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default
    link/ether 7e:e8:34:a3:48:1c brd ff:ff:ff:ff:ff:ff link-netnsid 1
    inet6 fe80::7ce8:34ff:fea3:481c/64 scope link
       valid_lft forever preferred_lft forever
35: veth07253cd@if34: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default
    link/ether 9a:98:72:27:e1:31 brd ff:ff:ff:ff:ff:ff link-netnsid 2
    inet6 fe80::9898:72ff:fe27:e131/64 scope link
       valid_lft forever preferred_lft forever
60: veth6d62ecd@if59: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default
    link/ether 66:9b:22:21:ff:a4 brd ff:ff:ff:ff:ff:ff link-netnsid 3
    inet6 fe80::649b:22ff:fe21:ffa4/64 scope link
       valid_lft forever preferred_lft forever
62: vetha672711@if61: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default
    link/ether be:7e:c1:05:eb:bd brd ff:ff:ff:ff:ff:ff link-netnsid 4
    inet6 fe80::bc7e:c1ff:fe05:ebbd/64 scope link
       valid_lft forever preferred_lft forever
63: br-43f46e2b33a1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
    link/ether 02:42:67:2e:c6:dd brd ff:ff:ff:ff:ff:ff
    inet 172.19.0.1/16 brd 172.19.255.255 scope global br-43f46e2b33a1
       valid_lft forever preferred_lft forever
    inet6 fe80::42:67ff:fe2e:c6dd/64 scope link
       valid_lft forever preferred_lft forever
65: veth35fc2fc@if64: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master br-43f46e2b33a1 state UP group default
    link/ether 1e:01:18:82:83:fd brd ff:ff:ff:ff:ff:ff link-netnsid 5
    inet6 fe80::1c01:18ff:fe82:83fd/64 scope link
       valid_lft forever preferred_lft forever
[root@ip-172-31-39-133 ~]#






In above diagram if one bridge container wants to communicate other they should communicate vai host interface.


ex: wget ip:3333 


Host Network:

[root@ip-172-31-39-133 ~]# docker container run -it --network=host nginx

You can see your container and host are same.

Container will be isolated but its use same host network.

You can access nginx service using host ip.

Null Network: Container which is not connected to any network.

[root@ip-172-31-39-133 ~]# docker container run -it --network=none ubuntu
root@fd7fb116f4c3:/# 

Its isolated container.
We can see container created with loopback address.


Connect and Disconnect:

[root@ip-172-31-39-133 ~]# docker network disconnect none <container_id>
[root@ip-172-31-39-133 ~]# docker network connect my_network <container_id>









Tuesday, August 30, 2022

Docker Volume

 Docker volume: Theory

https://muhammadbasirbaig.medium.com/types-of-volumes-in-docker-9ddc84d6f5d9#:~:text=%20Types%20Of%20Volumes%20In%20Docker%20%201,if%20the%20data%20is%20only%20going...%20More%20


[root@ip-172-31-39-133 ~]# docker images

REPOSITORY   TAG       IMAGE ID       CREATED        SIZE


mysql        latest    70325c69f1fe   3 days ago     447MB



----

docker image inspect mysql


            "Image": "sha256:70085967b5e8a570f801bad787ac3551abe82bd06aeeae4acc402c5f8f706948",

            "Volumes": {

                "/var/lib/mysql": {}

=---------------


[root@ip-172-31-39-133 ~]# docker container run -d --name mysql_demo -e  MYSQL_ALLOW_EMPTY_PASSWORD=true mysql

fc9adc908367882debb884010f4093c56071df898924b21c1b187a36f253f731

[root@ip-172-31-39-133 ~]# docker ps

CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                 NAMES

fc9adc908367   mysql     "docker-entrypoint.s…"   6 seconds ago   Up 5 seconds   3306/tcp, 33060/tcp   mysql_demo

2ba149181b54   nginx     "/docker-entrypoint.…"   14 hours ago    Up 14 hours    80/tcp                elegant_cray

[root@ip-172-31-39-133 ~]#


[root@ip-172-31-39-133 ~]# docker volume ls

DRIVER    VOLUME NAME

local     83a13c22bec37287e671d8654b8fb8b19c48cbcc338a7f7ea54772ce1b71dd33

[root@ip-172-31-39-133 ~]#



[root@ip-172-31-39-133 _data]# pwd

/var/lib/docker/volumes/83a13c22bec37287e671d8654b8fb8b19c48cbcc338a7f7ea54772ce1b71dd33/_data



[root@ip-172-31-39-133 dockerdemo]# docker container exec -it fc9adc908367 bash

bash-4.4# mysql

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 8

Server version: 8.0.30 MySQL Community Server - GPL


Copyright (c) 2000, 2022, Oracle and/or its affiliates.


Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.


Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

| sys                |

+--------------------+

4 rows in set (0.01 sec)


mysql> create database kishor

    -> ;

Query OK, 1 row affected (0.01 sec)


mysql> create database satish

    -> ;

Query OK, 1 row affected (0.01 sec)


mysql> create database satish

    -> ;

ERROR 1007 (HY000): Can't create database 'satish'; database exists

mysql> create database ganesh;

Query OK, 1 row affected (0.01 sec)


mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| ganesh             |

| information_schema |

| kishor             |

| mysql              |

| performance_schema |

| satish             |

| sys                |

+--------------------+

7 rows in set (0.00 sec)




-------------

created new container:

[root@ip-172-31-39-133 dockerdemo]# docker container run -d --name mysql_demo -e  MYSQL_ALLOW_EMPTY_PASSWORD=true mysql

54e7eace755057e125576b84098cbcd1eb2b44e878d932b8e8221dc23cbab6d2

[root@ip-172-31-39-133 dockerdemo]# docker exec -it 54e bash

bash-4.4# mysql

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 8

Server version: 8.0.30 MySQL Community Server - GPL


Copyright (c) 2000, 2022, Oracle and/or its affiliates.


Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.


Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

| sys                |

+--------------------+

4 rows in set (0.00 sec)


mysql>



================

[root@ip-172-31-39-133 dockerdemo]# docker run -itd -v 83a13c22bec37287e671d8654b8fb8b19c48cbcc338a7f7ea54772ce1b71dd33:/var/lib/mysql mysql

85901506b9ae2bab09c8a104b49d63db0a083e3dac5674c464d88d526e00ad69

[root@ip-172-31-39-133 dockerdemo]# docker ps

CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                 NAMES

85901506b9ae   mysql     "docker-entrypoint.s…"   5 seconds ago    Up 4 seconds    3306/tcp, 33060/tcp   elated_jemison

54e7eace7550   mysql     "docker-entrypoint.s…"   10 minutes ago   Up 10 minutes   3306/tcp, 33060/tcp   mysql_demo

[root@ip-172-31-39-133 dockerdemo]# docker exec -it 85901506b9ae bash

bash-4.4# mysql

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 8

Server version: 8.0.30 MySQL Community Server - GPL


Copyright (c) 2000, 2022, Oracle and/or its affiliates.


Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.


Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| ganesh             |

| information_schema |

| kishor             |

| mysql              |

| performance_schema |

| satish             |

| sys                |

+--------------------+

7 rows in set (0.15 sec)


mysql>


=============


[root@ip-172-31-39-133 ~]# docker volume create myvol

myvol

[root@ip-172-31-39-133 ~]# cd /var/lib/docker/volumes/

[root@ip-172-31-39-133 volumes]# ls -l

total 24

drwx-----x 3 root root     19 Aug 30 18:30 83a13c22bec37287e671d8654b8fb8b19c48cbcc338a7f7ea54772ce1b71dd33

drwx-----x 3 root root     19 Aug 30 18:36 8db408011f86a213df2975039da6177698f5cfcd9a8451d9b3bf54003fae502b

brw------- 1 root root 202, 1 Aug 30 18:58 backingFsBlockDev

-rw------- 1 root root  32768 Aug 30 18:59 metadata.db

drwx-----x 3 root root     19 Aug 30 18:59 myvol

drwx-----x 3 root root     19 Aug 30 18:50 test

[root@ip-172-31-39-133 volumes]# docker volume create myvol1

myvol1

[root@ip-172-31-39-133 volumes]# docker volume create myvol2

myvol2

[root@ip-172-31-39-133 volumes]# docker volume ls

DRIVER    VOLUME NAME

local     8db408011f86a213df2975039da6177698f5cfcd9a8451d9b3bf54003fae502b

local     83a13c22bec37287e671d8654b8fb8b19c48cbcc338a7f7ea54772ce1b71dd33

local     myvol

local     myvol1

local     myvol2

local     test


-----------

Remove Volume:


[root@ip-172-31-39-133 volumes]# docker volume rm myvol2

myvol2


Prune Volume: Remove un used volume:


[root@ip-172-31-39-133 volumes]# docker volume prune

WARNING! This will remove all local volumes not used by at least one container.

Are you sure you want to continue? [y/N] y

Deleted Volumes:

myvol

myvol1


Total reclaimed space: 0B

[root@ip-172-31-39-133 volumes]#


==================



https://dockerlabs.collabnix.com/beginners/volume/create-a-volume-mount-from-dockerfile.html


==============

Bind Mount:


[root@ip-172-31-39-133 volumes]# cd /dockerdemo/

[root@ip-172-31-39-133 dockerdemo]# ls -l

total 114296

-rw-r--r-- 1 root root        34 Aug 30 04:36 Dockerfile

-rw-r--r-- 1 root root 117034496 Aug 30 03:53 mycon.tar

[root@ip-172-31-39-133 dockerdemo]# pwd

/dockerdemo

[root@ip-172-31-39-133 dockerdemo]# docker run -it -v /dockerdemo:/tmp/test ubuntu bash

root@5d3b1c485eb1:/# ls -l /tmp/test/

total 114296

-rw-r--r-- 1 root root        34 Aug 30 04:36 Dockerfile

-rw-r--r-- 1 root root 117034496 Aug 30 03:53 mycon.tar

root@5d3b1c485eb1:/#



---------------

Docker Network:

https://docs.docker.com/engine/reference/commandline/network/

https://monkelite.com/introduction-to-docker-networking/




Dockerfile Commands

FROM <Base Image>

RUN  <To install software/packages>

ENV  <Pass env variable>

ARG <Its will  used image creation time>

FROM alpine
ARG TARGETPLATFORM
RUN echo "I'm building for $TARGETPLATFORM"
FROM ubuntu
ARG CONT_IMG_VER
ENV CONT_IMG_VER=$CONT_IMG_VER
RUN echo $CONT_IMG_VER

USER <Will switch user>

WORKDIR <Ir set working directory>

COPY  <copy files from host to container>

ADD  <copy remote files and extract it>

VOLUME /myvol  <mount volume>

ENTRYPOINT 

CMD

Dockerfile reference | Docker Documentation


Docker has a default entrypoint which is /bin/sh -c but does not have a default command.

The ENTRYPOINT specifies a command that will always be executed when the container starts.

The CMD specifies arguments that will be fed to the ENTRYPOINT.


If you want to make an image dedicated to a specific command you will use ENTRYPOINT ["/path/dedicated_command"]

Otherwise, if you want to make an image for general purpose, you can leave ENTRYPOINT unspecified and use CMD ["/path/dedicated_command"] as you will be able to override the setting by supplying arguments to docker run.


For example, if your Dockerfile is:

FROM debian:wheezy
ENTRYPOINT ["/bin/ping"]
CMD ["localhost"]

The ENTRYPOINT specifies a command that will always be executed when the container starts. 
The CMD specifies arguments that will be fed to the ENTRYPOINT


FROM debian:wheezy
CMD ["/bin/ping", "localhost"]



The ENTRYPOINT is the program that will be run, and the value passed to the container will be appended to it.


The ENTRYPOINT can be overridden by specifying an --entrypoint flag, followed by the new entry point you want to use.

====================

CMD defines default commands and/or parameters for a container. CMD is an instruction that is best to use if you need a default command which users can easily override. If a Dockerfile has multiple CMDs, it only applies the instructions from the last one.


ENTRYPOINT is preferred when you want to define a container with a specific executable.

You cannot override an ENTRYPOINT when starting a container unless you add the --entrypoint flag.


CMD Docker file


  FROM centos:8.1.1911
  CMD ["echo", "Hello Docker"]

Run result

$ sudo docker run <image-id>
Hello Docker
$ sudo docker run <image-id> hostname   # hostname is exec to override CMD
244be5006f32

ENTRYPOINT

Docker file


  FROM centos:8.1.1911
  ENTRYPOINT ["echo", "Hello Docker"]

Run result

$ sudo docker run <image-id>
Hello Docker
$ sudo docker run <image-id> hostname   # hostname as parameter to exec
Hello Docker hostname

  1. There are many situations in which combining CMD and ENTRYPOINT would be the best solution for your Docker container. In such cases, the executable is defined with ENTRYPOINT, while CMD specifies the default parameter.

Docker file

  FROM centos:8.1.1911

  ENTRYPOINT ["echo", "Hello"]
  CMD ["Docker"]

Run result

$ sudo docker run <image-id>
Hello Docker
$ sudo docker run <image-id> Ben
Hello Ben

Monday, August 29, 2022

Docker Session Commands

 430  vi Dockerfile

  431  docker logs bc59d1bb68dc

  432  docker ps

  433  docker kill bc59d1bb68dc

  434  docker ps

  435  docker

  436  mkdir /dockerdemo

  437  cd /dockerdemo/

  438  vi Dockerfile

  439  docker build -t myubuntuimage:1.0 .

  440  docker images

  441  docker container run -it myubuntuimage:1.0

  442  vi Dockerfile

  443  docker build -t myubuntuimage:2.0 .

  444  docker container run -it myubuntuimage:2.0

  445  docker ps

  446  docker container commit  --author "Kishor Aswar" -m "THis is my Customized image" c8d9f6e1db26 my_new_image

  447  docker images

  448  docker cp /tmp/index.html c8d9f6e1db26:/tmp

  449  docker exec c8d9f6e1db26 ls /tmp

  450  docker exec c8d9f6e1db26 ls -l /tmp

  451  date

  452  docker diff c8d9f6e1db26

  453  docker cp /var/log/messages c8d9f6e1db26:/tmp

  454  docker diff c8d9f6e1db26

  455  docker events c8d9f6e1db26

  456  docker events --help

  457  docker events -since index c8d9f6e1db26

  458  docker events --since index c8d9f6e1db26

  459  docker events --since '2022' c8d9f6e1db26

  460  docker events --since '2022'

  461  docker ps

  462  docker container export  c8d9f6e1db26 >mycon.tar

  463  ls -l

  464  docker image import  mycon.tar my_inported_image

  465  docker images

  466  docker run -d my_inported_image

  467  docker container run -d my_inported_image

  468  docker container run my_inported_image

  469  dcoker container run nginx

  470  docker container run nginx

  471  docker container run -d my_inported_image /bin/bash

  472  docker images

  473  docker ps

  474  docker ps -a

  475  docker ps

  476  docker history c8d9f6e1db26

  477  docker container history c8d9f6e1db26

  478  docker container history

  479  docker history

  480  docker history --help

  481  docker images

  482  docker history my_inported_image

  483  docker history nginx

  484  docker info

  485  docker ps

  486  docker inspect c8d9f6e1db26

  487  docker ps

  488  docker kill c8d9f6e1db26

  489  docker stop 6463aa45b2d0

  490  docker images

  491  docker login

  492  docker pull httpd

  493  docker images

  494  docker pull httpd:2.4.54

  495  docker images

  496  vi Dockerfile

  497  docker build -t kishoraswar/myrepo:v1 .

  498  docker  images

  499  docker push kishoraswar/myrepo:v1

  500  docker login

  501  docker ps

  502  docker run -it ubuntu

  503  docke rps

  504  docker ps

  505  docker container restart ed625cb0b5ba

  506  docker ps

  507  docker stop ed625cb0b5ba

  508  docker container ls -a

  509  docker ps -a

  510  docker rm $(docker ps -a)

  511  docker rm $(docker ps -a -f status=exited -q)

  512  docker ps -a

  513  docker ps

  514  docker rm $(docker ps -a -f status=created -q)

  515  docker images

  516  docker rmi my_inported_image

  517  docker images

  518  docker rmi $(docker images -a -q)

  519  docker images

  520  docker rmi $(docker images -a -q)

  521  docker rmi $(docker images -a -f -q)

  522  docker prune images

  523  docker images

  524  docker rmi kishoraswar/myrepo

  525  docker rmi kishoraswar/myrepo:v1

  526  docker images

  527  docker rmi $(docker images -a -q)

  528  docker rmi myubuntuimage:2.0

  529  docker images

  530  docker rmi myos*

  531  docker rmi myos:2.0

  532  docker images

  533  docker search nginx

  534  docker container run -it ubuntu

  535  docker ps

  536  docker stats

  537  docker images

  538  docker tag

  539  docker tag  myos:3.0 new_image:1.0

  540  docker images

  541  docker ps

  542  top

  543  docker ps

  544  docker top 2906c6761ae7

  545  docker ps

  546  docker pause 2906c6761ae7

  547  docker ps

  548  docker unpause 2906c6761ae7

  549  docker ps

  550  docker images

  551  docker run -d nginx

  552  docker ps

  553  docker nginx

  554  docker ps

  555  rm Dockerfile

  556  vi Dockerfile

  557  ls -l

  558  pwd

  559  cd /opt

  560  docker build -t myimage:1.0 .

  561  docker build -t myimage:1.0 -f /dockerdemo/Dockerfile

  562  docker build -t myimage:1.0 /dockerdemo/Dockerfile -

  563  docker build -t myimage:1.0 /dockerdemo/Dockerfile .

  564  docker build --help

  565  docker build -t myimage:1.0 -f /dockerdemo/Dockerfile

  566  cd /dockerdemo/

  567  ls -l

  568  docker build -t testimage .

  569  docker images

  570  mv Dockerfile myfile

  571  docker build -t testimage1 -f myfile

  572  docker build -t testimage1 -f myfile -

  573  docker images

  574  docker container run -d testimage

  575  docker ps

  576  docker ps -a

  577  ls -l

  578  mv myfile Dockerfile

  579  vi Dockerfile

  580  docker build -t test1 .

  581  vi Dockerfile

  582  history


Docker Commands:

 Management Commands:

  builder     Manage builds

  config      Manage Docker configs

  container   Manage containers

  context     Manage contexts

  image       Manage images

  manifest    Manage Docker image manifests and manifest lists

  network     Manage networks

  node        Manage Swarm nodes

  plugin      Manage plugins

  secret      Manage Docker secrets

  service     Manage services

  stack       Manage Docker stacks

  swarm       Manage Swarm

  system      Manage Docker

  trust       Manage trust on Docker images

  volume      Manage volumes


Commands:

  attach      Attach local standard input, output, and error streams to a running container

  build       Build an image from a Dockerfile

  commit      Create a new image from a container's changes

  cp          Copy files/folders between a container and the local filesystem

  create      Create a new container

  diff        Inspect changes to files or directories on a container's filesystem

  events      Get real time events from the server

  exec        Run a command in a running container

  export      Export a container's filesystem as a tar archive

  history     Show the history of an image

  images      List images

  import      Import the contents from a tarball to create a filesystem image

  info        Display system-wide information

  inspect     Return low-level information on Docker objects

  kill        Kill one or more running containers

  load        Load an image from a tar archive or STDIN

  login       Log in to a Docker registry

  logout      Log out from a Docker registry

  logs        Fetch the logs of a container

  pause       Pause all processes within one or more containers

  port        List port mappings or a specific mapping for the container

  ps          List containers

  pull        Pull an image or a repository from a registry

  push        Push an image or a repository to a registry

  rename      Rename a container

  restart     Restart one or more containers

  rm          Remove one or more containers

  rmi         Remove one or more images

  run         Run a command in a new container

  save        Save one or more images to a tar archive (streamed to STDOUT by default)

  search      Search the Docker Hub for images

  start       Start one or more stopped containers

  stats       Display a live stream of container(s) resource usage statistics

  stop        Stop one or more running containers

  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

  top         Display the running processes of a container

  unpause     Unpause all processes within one or more containers

  update      Update configuration of one or more containers

  version     Show the Docker version information

  wait        Block until one or more containers stop, then print their exit codes


Thursday, August 25, 2022

Docker Training

 1)Docker Introduction and Architecture  - Done

2)Docker Installation and Docker Info  - Done

3)Create First Container, List Container, Remove Container

 Create: docker container run --name=kishor -d nginx  

 # docker container ls

 #docker ps

#docker ps -a

 #docker container ls -a

[root@ip-172-31-39-133 ~]# docker container stop d13851e2b5fb

d13851e2b5fb

[root@ip-172-31-39-133 ~]# docker container rm d13851e2b5fb

d13851e2b5fb

  


4)Create Container in background, stop, start, detach  container.

[root@ip-172-31-39-133 ~]# docker container run -d httpd

d85bfc54941ec18d2ec4380e9098989ff23872843cc39862bd09389e984ff60c

[root@ip-172-31-39-133 ~]# docke rps

-bash: docke: command not found

[root@ip-172-31-39-133 ~]# docker ps

'CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES

d85bfc54941e   httpd     "httpd-foreground"       4 seconds ago    Up 4 seconds    80/tcp    intelligent_satoshi


5)Docker Container Inspect

docker inspect d85bfc54941e|less


6)Whats going on inside the container

#docker logs d85bfc54941e 


7)Docker port mapping, rename container, restart the container, exec container

[root@ip-172-31-39-133 ~]# docker run -d -p 3306:80 nginx

ba313210c37b5a4193d92317a2734087555072e1c614eec80185708fd6fce98b

[root@ip-172-31-39-133 ~]# docker ps

CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS        PORTS                                   NAMES

ba313210c37b   nginx     "/docker-entrypoint.…"   2 seconds ago   Up 1 second   0.0.0.0:3306->80/tcp, :::3306->80/tcp   musing_poitras


8)Attach to a running container, kill, wait, pause, unpause, prune, port



[root@ip-172-31-39-133 ~]# docker rm $(docker ps -a -f status=exited -q)

351171d12678

d6e05ed3325b

ab0bcc80a5f3    

[root@ip-172-31-39-133 ~]# docker ps -a

CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

[root@ip-172-31-39-133 ~]#

[root@ip-172-31-39-133 ~]# docker rmi $(docker images -a -q)

9)Create docker container, diff docker container and copy file into container.


10)Export/Import Docker Container.

 #docker container ls

  #docker container export 351171d12678 > my_ubuntu.tar

  #ls -l

  #docker image import my_ubuntu.tar my_ubunut

  #docker images

  #docker container run -it my_ubunut

  #docker container run -it my_ubunut /bin/bash


11)How to create docker image from a running container.

[root@ip-172-31-39-133 ~]# docker container commit --author "Kishor Aswar" -m "THis is my Customized image"  351171d12678 my_con_image

sha256:e8f2927cc89c7c2de6828369e1f0c3107a6a017814abf075a444f873818c540c

[root@ip-172-31-39-133 ~]# docker images

REPOSITORY     TAG       IMAGE ID       CREATED         SIZE

my_con_image   latest    e8f2927cc89c   7 seconds ago   114MB

[root@ip-172-31-39-133 ~]# docker run -it my_con_image /bin/bash

root@7b5ba6313bee:/#


12)How to push image on docker hub, image tag, image pull, docker login.

https://hub.docker.com/

[root@ip-172-31-39-133 ~]# docker pull nginx:1.23

1.23: Pulling from library/nginx

7a6db449b51b: Pull complete

ca1981974b58: Pull complete

d4019c921e20: Pull complete

7cb804d746d4: Pull complete

e7a561826262: Pull complete

7247f6e5c182: Pull complete

Digest: sha256:b95a99feebf7797479e0c5eb5ec0bdfa5d9f504bc94da550c2f58e839ea6914f

Status: Downloaded newer image for nginx:1.23

docker.io/library/nginx:1.23

[root@ip-172-31-39-133 ~]# docker images

REPOSITORY   TAG       IMAGE ID       CREATED      SIZE

nginx        1.23      2b7d6430f78d   5 days ago   142MB

[root@ip-172-31-39-133 ~]#

[root@ip-172-31-39-133 ~]# docker images

REPOSITORY   TAG       IMAGE ID       CREATED       SIZE

nginx        1.23      2b7d6430f78d   5 days ago    142MB

ubuntu       latest    df5de72bdb3b   3 weeks ago   77.8MB

[root@ip-172-31-39-133 ~]# docker image tag ubuntu kishoraswar/ubuntu_new

[root@ip-172-31-39-133 ~]# docker image push

"docker image push" requires exactly 1 argument.

See 'docker image push --help'.


Usage:  docker image push [OPTIONS] NAME[:TAG]


Push an image or a repository to a registry

[root@ip-172-31-39-133 ~]# docker images

REPOSITORY               TAG       IMAGE ID       CREATED       SIZE

nginx                    1.23      2b7d6430f78d   5 days ago    142MB

kishoraswar/ubuntu_new   latest    df5de72bdb3b   3 weeks ago   77.8MB

ubuntu                   latest    df5de72bdb3b   3 weeks ago   77.8MB

[root@ip-172-31-39-133 ~]# docker image push  kishoraswar/ubuntu_new

Using default tag: latest

The push refers to repository [docker.io/kishoraswar/ubuntu_new]

629d9dbab5ed: Mounted from library/ubuntu

latest: digest: sha256:42ba2dfce475de1113d55602d40af18415897167d47c2045ec7b6d9746ff148f size: 529

[root@ip-172-31-39-133 ~]#



[root@ip-172-31-39-133 ~]# docker login

Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.

Username: kishoraswar

Password:

WARNING! Your password will be stored unencrypted in /root/.docker/config.json.

Configure a credential helper to remove this warning. See

https://docs.docker.com/engine/reference/commandline/login/#credentials-store


Login Succeeded


  # docker images
  # docker tag ubuntu:latest kishoraswar/myrepo:myubuntutag
  # docker push kishoraswar/myrepo:myubuntutag

13)How to inspect, list and history of docker image.

[root@ip-172-31-39-133 ~]# docker image history df5de72bdb3b

IMAGE          CREATED       CREATED BY                                      SIZE      COMMENT

df5de72bdb3b   3 weeks ago   /bin/sh -c #(nop)  CMD ["bash"]                 0B

<missing>      3 weeks ago   /bin/sh -c #(nop) ADD file:396eeb65c8d737180…   77.8MB

[root@ip-172-31-39-133 ~]#

docker image inspect df5de72bdb3b

15)Layered architecture and Dockerfile

[root@ip-172-31-39-133 ~]# cat Dockerfile

FROM ubuntu

MAINTAINER Kishor Aswar

USER root

RUN apt-get update -y

RUN apt-get install tree -y

RUN touch /tmp/file1

RUN touch /tmp/file2


[root@ip-172-31-39-133 ~]# vi Dockerfile

[root@ip-172-31-39-133 ~]# docker build -t myos:3.0 .

Sending build context to Docker daemon  116.4MB

Step 1/7 : FROM ubuntu

 ---> df5de72bdb3b

Step 2/7 : MAINTAINER Kishor Aswar

 ---> Using cache

 ---> c9e9b6a1ef5a

Step 3/7 : USER root

 ---> Using cache

 ---> db9cf470d44a

Step 4/7 : RUN apt-get update -y

 ---> Using cache

 ---> 400b74a03009

Step 5/7 : RUN apt-get install tree -y

 ---> Using cache

 ---> d5f601900dde

Step 6/7 : RUN touch /tmp/file1

 ---> Running in 0e7ead191ce1

Removing intermediate container 0e7ead191ce1

 ---> ba71d11f5172

Step 7/7 : RUN touch /tmp/file2

 ---> Running in 3da57cf7a554

Removing intermediate container 3da57cf7a554

 ---> 33bb176bdbdf

Successfully built 33bb176bdbdf

Successfully tagged myos:3.0

[root@ip-172-31-39-133 ~]# docker images -a

REPOSITORY   TAG       IMAGE ID       CREATED          SIZE

myos         3.0       33bb176bdbdf   9 seconds ago    115MB

<none>       <none>    ba71d11f5172   10 seconds ago   115MB

myos         2.0       d5f601900dde   6 minutes ago    115MB

<none>       <none>    400b74a03009   6 minutes ago    114MB

<none>       <none>    db9cf470d44a   7 minutes ago    77.8MB

myos         1.0       c9e9b6a1ef5a   14 minutes ago   77.8MB

nginx        latest    2b7d6430f78d   6 days ago       142MB

httpd        latest    a981c8992512   6 days ago       145MB

ubuntu       latest    df5de72bdb3b   3 weeks ago      77.8MB

centos       latest    5d0da3dc9764   11 months ago    231MB

[root@ip-172-31-39-133 ~]#


16)Dockerfile(Label , RUN, ENV, Workdir)




17)Dockerfile(ADD, COPY, USER)

18)Dockerfile(CMD)

19)Dockerfile(Expose and create a SSH container using dockerfile)


20)Dockerfile(ENTRYPOINT)

Docker difference between run, cmd, entrypoint commands (til.codes)

  • RUN executes command(s) in a new layer and creates a new image. E.g., it is often used for installing software packages.
  • CMD sets default command and/or parameters, which can be overwritten from command line when docker container runs.
  • ENTRYPOINT configures a container to run as an executable. This is usually the file path to the executable inside the container.

Use RUN instructions to build your image by adding layers on top of initial image.

Prefer ENTRYPOINT to CMD when building executable Docker image and you need a command always to be executed. Additionally use CMD if you need to provide extra default arguments that could be overwritten from command line when docker container runs.

Choose CMD if you need to provide a default command and/or arguments that can be overwritten from command line when docker container runs.

21)Docker Volume(Docker Storage), mysql data persist in docker container

22)Docker Volume Creation

23)Docker volume(Remove, Prune)

24)Docker Bind Mount

25)Docker networking(Bridge, Host, Null)

26)Docker Network(Connect, Disconnect)

27)Docker network(Remove, Prune)

28)Docker Registry/Repository(Insecure)

29)Docker Registry With Basic Authentication)


Docker Compose/swarm


Kubernetes



Sample Game App Deployment on EKS cluster

 https://padmakshi.medium.com/setting-up-an-eks-cluster-and-deploying-a-game-application-a-step-by-step-guide-08790e0be117