Thursday, 5 July 2018

003- Daily Operational Containers Management

Creating and removing "wordpress" container:

[ab@fedora28 ~]$ docker search wordpress
NAME                                                DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
wordpress                                           The WordPress rich content management system…   2367                [OK]                
[...]
[ab@fedora28 ~]$ docker pull wordpress
[ab@fedora28 ~]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
wordpress           latest              1d3cc82944da        7 days ago          408MB
fedora              latest              cc510acfcd70        2 months ago        253MB
[ab@fedora28 ~]$ docker ps 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[ab@fedora28 ~]$ docker pull wordpress
[ab@fedora28 ~]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
wordpress           latest              1d3cc82944da        7 days ago          408MB
fedora              latest              cc510acfcd70        2 months ago        253MB
[ab@fedora28 ~]$ docker ps 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[ab@fedora28 ~]$ docker run -it wordpress /bin/bash

root@db9ed3a7383e:/var/www/html# hostname 
db9ed3a7383e

root@db9ed3a7383e:/var/www/html# lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 9.4 (stretch)
Release: 9.4
Codename: stretch

root@db9ed3a7383e:/var/www/html# +
[ab@fedora28 ~]$ docker ps 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
db9ed3a7383e        wordpress           "docker-entrypoint.s…"   3 minutes ago       Up 3 minutes        80/tcp              condescending_bartik

[ab@fedora28 ~]$ docker stop db9ed3a7383e
db9ed3a7383e
[ab@fedora28 ~]$ docker rm db9ed3a7383e
db9ed3a7383e
[ab@fedora28 ~]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
wordpress           latest              1d3cc82944da        7 days ago          408MB
fedora              latest              cc510acfcd70        2 months ago        253MB
[ab@fedora28 ~]$ docker rmi wordpress
[ab@fedora28 ~]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
fedora              latest              cc510acfcd70        2 months ago        253MB


Create new CentOS container with name "centos" and hostname "centos":

[ab@fedora28 ~]$ docker search centos
NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
centos                             The official build of CentOS.                   4419                [OK]                
[...]
[ab@fedora28 ~]$ docker pull centos
[ab@fedora28 ~]$ docker help run | grep -e "\-d," -e "--name" -e "--hostname"
  -d, --detach                         Run container in background and print container ID
  -h, --hostname string                Container host name
      --name string                    Assign a name to the container
[ab@fedora28 ~]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              49f7960eb7e4        4 weeks ago         200MB
fedora              latest              cc510acfcd70        2 months ago        253MB
[ab@fedora28 ~]$ docker run -dit --name centos --hostname=centos centos /bin/bash
5d3c83663ab9cf814ebece52285a83f18f769ba5f0f3745bb16f9e2d4abe77e9
[ab@fedora28 ~]$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
5d3c83663ab9        centos              "/bin/bash"         11 seconds ago      Up 10 seconds                           centos
[ab@fedora28 ~]$ docker attach centos
[root@centos /]# hostname
centos
[root@centos /]# top
top - 14:00:56 up  2:17,  0 users,  load average: 0.02, 0.04, 0.03
Tasks:   2 total,   1 running,   1 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  4038808 total,  2695736 free,   214452 used,  1128620 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.  3500692 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                                                                       
    1 root      20   0   11832   3020   2620 S  0.0  0.1   0:00.03 bash                                                                                          
   15 root      20   0   56172   3896   3364 R  0.0  0.1   0:00.00 top   
[root@centos /]# exit
exit
[ab@fedora28 ~]$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[ab@fedora28 ~]$ docker ps -a
CONTAINER ID        IMAGE               COMMAND              CREATED             STATUS                      PORTS               NAMES
5d3c83663ab9        centos              "/bin/bash"          3 minutes ago       Exited (0) 5 seconds ago                        centos
3ed4e70d4c73        fedora              "/bin/bash"          About an hour ago   Exited (0) 25 minutes ago                       competent_villani
901f42ca4f10        fedora              "echo hello world"   2 hours ago         Exited (0) 2 hours ago                          eloquent_elbakyan


How docker-engine deals with 'docker run' command? It will create new container:


Three Containers:
[ab@fedora28 ~]$ docker ps -a
CONTAINER ID        IMAGE               COMMAND              CREATED             STATUS                      PORTS               NAMES
5d3c83663ab9        centos              "/bin/bash"          3 minutes ago       Exited (0) 5 seconds ago                        centos
3ed4e70d4c73        fedora              "/bin/bash"          About an hour ago   Exited (0) 25 minutes ago                       competent_villani
901f42ca4f10        fedora              "echo hello world"   2 hours ago         Exited (0) 2 hours ago                          eloquent_elbakyan

Now, four containers, the new one name is "modest_montalcini"
[ab@fedora28 ~]$ docker run centos /usr/bin/free -m 
              total        used        free      shared  buff/cache   available
Mem:           3944         197        2643           0        1102        3430
Swap:          2047           0        2047
[ab@fedora28 ~]$ docker ps -as
CONTAINER ID        IMAGE               COMMAND              CREATED             STATUS                      PORTS               NAMES               SIZE
e7ec9ef2443f        centos              "/usr/bin/free -m"   5 minutes ago       Exited (0) 5 minutes ago                        modest_montalcini   0B (virtual 200MB)       <----
5d3c83663ab9        centos              "/bin/bash"          15 minutes ago      Exited (0) 11 minutes ago                       centos              23B (virtual 200MB)
3ed4e70d4c73        fedora              "/bin/bash"          2 hours ago         Exited (0) 37 minutes ago                       competent_villani   76B (virtual 253MB)
901f42ca4f10        fedora              "echo hello world"   2 hours ago         Exited (0) 2 hours ago                          eloquent_elbakyan   0B (virtual 253MB)


Relation between docker "host" PID and docker "container"


Start "centos" container again:
[ab@fedora28 ~]$ docker ps -as
CONTAINER ID        IMAGE               COMMAND              CREATED             STATUS                        PORTS               NAMES               SIZE
e7ec9ef2443f        centos              "/usr/bin/free -m"   9 minutes ago       Exited (0) 9 minutes ago                          modest_montalcini   0B (virtual 200MB)
5d3c83663ab9        centos              "/bin/bash"          19 minutes ago      Exited (137) 14 seconds ago                       centos              23B (virtual 200MB)
3ed4e70d4c73        fedora              "/bin/bash"          2 hours ago         Exited (0) 40 minutes ago                         competent_villani   76B (virtual 253MB)
901f42ca4f10        fedora              "echo hello world"   2 hours ago         Exited (0) 2 hours ago                            eloquent_elbakyan   0B (virtual 253MB)
[ab@fedora28 ~]$ docker start centos
centos
[ab@fedora28 ~]$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
5d3c83663ab9        centos              "/bin/bash"         19 minutes ago      Up 6 seconds                            centos

From the docker "host" check the running PIDs in "centos" container:

[ab@fedora28 ~]$ docker help top

Usage: docker top CONTAINER [ps OPTIONS]

Display the running processes of a container

Options:
[ab@fedora28 ~]$ docker top centos
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                5073                5058                0                   16:18               pts/0               00:00:00            /bin/bash
[ab@fedora28 ~]$ hostname
fedora28.ab.lab
[ab@fedora28 ~]$ ps -ef | grep 5058
root      5058  2344  0 16:18 ?        00:00:00 docker-containerd-shim -namespace moby -workdir /var/lib/docker/containerd/daemon/io.containerd.runtime.v1.linux/moby/5d3c83663ab9cf814ebece52285a83f18f769ba5f0f3745bb16f9e2d4abe77e9 -address /var/run/docker/containerd/docker-containerd.sock -containerd-binary /usr/bin/docker-containerd -runtime-root /var/run/docker/runtime-runc
root      5073  5058  0 16:18 pts/0    00:00:00 /bin/bash


Checking containers status:

[ab@fedora28 ~]$ docker ps 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
5d3c83663ab9        centos              "/bin/bash"         27 minutes ago      Up 8 minutes                            centos
[ab@fedora28 ~]$ docker help stats

Usage: docker stats [OPTIONS] [CONTAINER...]

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

Options:
  -a, --all             Show all containers (default shows just running)
      --format string   Pretty-print images using a Go template
      --no-stream       Disable streaming stats and only pull the first result
      --no-trunc        Do not truncate output
[ab@fedora28 ~]$ docker stats --no-stream
CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT   MEM %               NET I/O             BLOCK I/O           PIDS
5d3c83663ab9        centos              0.00%               884KiB / 3.852GiB   0.02%               1.12kB / 0B         0B / 0B             1
[ab@fedora28 ~]$ docker stats --no-stream --all
CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT   MEM %               NET I/O             BLOCK I/O           PIDS
e7ec9ef2443f        modest_montalcini   0.00%               0B / 0B             0.00%               0B / 0B             0B / 0B             0
5d3c83663ab9        centos              0.00%               884KiB / 3.852GiB   0.02%               1.12kB / 0B         0B / 0B             1
3ed4e70d4c73        competent_villani   0.00%               0B / 0B             0.00%               0B / 0B             0B / 0B             0
901f42ca4f10        eloquent_elbakyan   0.00%               0B / 0B             0.00%               0B / 0B             0B / 0B             0


Copy files from docker "host" to specific container and visa vers:

[ab@fedora28 ~]$ hostname
fedora28.ab.lab
[ab@fedora28 ~]$ docker help cp

Usage: docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
 docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH

Copy files/folders between a container and the local filesystem

Options:
  -a, --archive       Archive mode (copy all uid/gid information)
  -L, --follow-link   Always follow symbol link in SRC_PATH
[ab@fedora28 ~]$ docker help cp

Usage: docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
 docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH

Copy files/folders between a container and the local filesystem

Options:
  -a, --archive       Archive mode (copy all uid/gid information)
  -L, --follow-link   Always follow symbol link in SRC_PATH

"centos" container -> docker "host":
[ab@fedora28 ~]$ docker cp centos:/etc/passwd /tmp/centos-container-password
[ab@fedora28 ~]$ head -n1 /tmp/centos-container-password 
root:x:0:0:root:/root:/bin/bash

container "host" -> "centos" container:
[ab@fedora28 ~]$ docker cp /tmp/centos-container-password centos:/root/passwd
[ab@fedora28 ~]$ docker exec centos head -n1 /root/passwd
root:x:0:0:root:/root:/bin/bash


Understanding how Processes are listed inside the container:


Create new container "demo1" and start it by "dd if=/dev/zero of=/dev/null" command. This will be infinite process:
[ab@fedora28 ~]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              49f7960eb7e4        4 weeks ago         200MB
fedora              latest              cc510acfcd70        2 months ago        253MB
[ab@fedora28 ~]$ hostname
fedora28.ab.lab
[ab@fedora28 ~]$ docker run --name demo1 centos dd if=/dev/zero of=/dev/null

Check running PID for 'demo1' container in the 'host':
[ab@fedora28 ~]$ docker top demo1
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                6338                6323                95                  19:56               ?                   00:00:21            dd if=/dev/zero of=/dev/null
[ab@fedora28 ~]$ ps -ef | grep 6338
[ab@fedora28 ~]$ ps -ef | grep 6338 | grep -v grep
root      6338  6323 98 19:56 ?        00:01:08 dd if=/dev/zero of=/dev/null
[ab@fedora28 ~]$ ps -ef | grep 6323 | grep -v grep
root      6323  2344  0 19:56 ?        00:00:00 docker-containerd-shim -namespace moby -workdir /var/lib/docker/containerd/daemon/io.containerd.runtime.v1.linux/moby/e8324cfcdc0ffea78d754a764fcfc7b38e1308e0913196e1e772456d4727d538 -address /var/run/docker/containerd/docker-containerd.sock -containerd-binary /usr/bin/docker-containerd -runtime-root /var/run/docker/runtime-runc
root      6338  6323 99 19:56 ?        00:01:43 dd if=/dev/zero of=/dev/null

Try to execute "/bin/bash" inside "demo1" container to be able to access the container, then check the process. PID 1 is "dd" and PID 9 is "bash":

[ab@fedora28 ~]$ docker exec -it demo1 /bin/bash
[root@e8324cfcdc0f /]# top

top - 17:59:15 up  6:15,  0 users,  load average: 0.99, 0.85, 0.59
Tasks:   3 total,   2 running,   1 sleeping,   0 stopped,   0 zombie
%Cpu(s): 47.8 us, 51.8 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.3 hi,  0.0 si,  0.0 st
KiB Mem :  4038808 total,  2684956 free,   212964 used,  1140888 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.  3497668 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                                                                       
    1 root      20   0    4416    768    696 R 99.3  0.0   3:08.56 dd                                                                                            
    9 root      20   0   11832   3028   2676 S  0.0  0.1   0:00.01 bash                                                                                          
   22 root      20   0   56140   3864   3368 R  0.0  0.1   0:00.00 top    


Inspect Container Parameters:

[ab@fedora28 ~]$ docker help inspect 

Usage: docker inspect [OPTIONS] NAME|ID [NAME|ID...]

Return low-level information on Docker objects

Options:
  -f, --format string   Format the output using the given Go template
  -s, --size            Display total file sizes if the type is container
      --type string     Return JSON for specified type
[ab@fedora28 ~]$ docker inspect centos
[ab@fedora28 ~]$ docker inspect -f '{{ .NetworkSettings.IPAddress }}' centos
172.17.0.2


No comments:

Post a Comment