Monday, 21 May 2018

03- Docker Beginner - Port Forwarding/Mapping

Summary:

I have CentOS container named "centos1" that have HTTP server.
I want to forward incoming request to port 8081 on the docker physcial host to port 80 on the 'centos' container.

Install 'httpd' on 'centos1' container:

[root@docker1 ~]# hostname
docker1.ab.lab
[root@docker1 ~]# docker ps -s
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES               SIZE
766f948a89d6        centos              "/bin/bash"         2 hours ago         Up 41 minutes                           centos1             217 MB (virtual 416 MB)
[root@docker1 ~]# docker attach centos1
[root@766f948a89d6 /]# ip a
1: lo:  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
26: eth0@if27:  mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 172.17.0.2/16 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:acff:fe11:2/64 scope link 
       valid_lft forever preferred_lft forever
[root@766f948a89d6 /]# yum install httpd -y 

[root@766f948a89d6 /]# systemctl enable httpd      

[root@766f948a89d6 /]# httpd &
[1] 59
[root@766f948a89d6 /]# ps -ef | grep httpd
root        60     1  0 12:18 ?        00:00:00 httpd
apache      61    60  0 12:18 ?        00:00:00 httpd
apache      62    60  0 12:18 ?        00:00:00 httpd
apache      63    60  0 12:18 ?        00:00:00 httpd
apache      64    60  0 12:18 ?        00:00:00 httpd
apache      65    60  0 12:18 ?        00:00:00 httpd
root        67     1  0 12:18 ?        00:00:00 grep --color=auto httpd
[root@766f948a89d6 /]# netstat -tupenl          
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       User       Inode      PID/Program name    
tcp6       0      0 :::80                   :::*                    LISTEN      0          117163     60/httpd 
[root@766f948a89d6 /]# echo "httpd enabled on centos1 container" >> /var/www/html/index.html
[root@766f948a89d6 /]# lynx --dump 172.17.0.2
   httpd enabled on centos1 container


Redirect any incoming request to "docker1.ab.lab:8081" (physical docker host) to "766f948a89d6:80" (centos1 container):

[root@docker1 ~]# docker stop centos1 centos1 [root@docker1 ~]# docker run -it -p 8081:80 centos /bin/bash [root@docker1 ~]# docker ps -s CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE 43a3e1940071 centos "/bin/bash" 31 seconds ago Up 29 seconds 0.0.0.0:8081->80/tcp agitated_liskov 0 B (virtual 199 MB)

02- Docker Beginner - docker commands

Search for image in Docker Hub:

[root@docker1 ~]# docker search centos | head -n2 
INDEX       NAME                                         DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/centos                             The official build of CentOS.                   4297      [OK]      



Pull image from Docker Hup:

[root@docker1 ~]# docker pull centos


List available docker images:

[root@docker1 ~]# docker image ls
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
docker.io/ubuntu        latest              452a96d81c30        3 weeks ago         79.6 MB
docker.io/hello-world   latest              e38bc07ac18e        5 weeks ago         1.85 kB
docker.io/centos        latest              e934aafc2206        6 weeks ago         199 MB


List how many containers are running now:

[root@docker1 ~]# docker ps 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES


Show ALL containers even the stopped ones:

[root@docker1 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                       PORTS               NAMES
6ce4aa30164d        ubuntu              "bash"              17 minutes ago      Exited (0) 16 minutes ago                        kickass_payne
33f58ebbb82e        hello-world         "/hello"            17 minutes ago      Exited (0) 17 minutes ago                        clever_beaver


Run docker container and attach to its console, then terminate it once you logged out:

root@docker1 ~]# docker image ls
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
docker.io/ubuntu        latest              452a96d81c30        3 weeks ago         79.6 MB
docker.io/hello-world   latest              e38bc07ac18e        5 weeks ago         1.85 kB
docker.io/centos        latest              e934aafc2206        6 weeks ago         199 MB
[root@docker1 ~]# docker help run | grep -E -e "[[:space:]]-i" -e "[[:space:]]-t" -e "--name" 
  -i, --interactive                           Keep STDIN open even if not attached
      --name string                           Assign a name to the container
  -t, --tty                                   Allocate a pseudo-TTY
[root@docker1 ~]# docker run --name centos1 -i -t centos /bin/bash

[root@766f948a89d6 /]# hostname
766f948a89d6

[root@766f948a89d6 /]# exit
exit
[root@docker1 ~]# docker ps -s
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES               SIZE
[root@docker1 ~]# docker ps -a -s
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES               SIZE
766f948a89d6        centos              "/bin/bash"         54 seconds ago      Exited (0) 41 seconds ago                       centos1             14 B (virtual 199 MB)
6ce4aa30164d        ubuntu              "bash"              23 minutes ago      Exited (0) 23 minutes ago                       kickass_payne       0 B (virtual 79.6 MB)
33f58ebbb82e        hello-world         "/hello"            23 minutes ago      Exited (0) 23 minutes ago                       clever_beaver       0 B (virtual 1.85 kB)


Run docker container and de-attach form its console without powering it off:

[root@docker1 ~]# docker image ls
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
docker.io/ubuntu        latest              452a96d81c30        3 weeks ago         79.6 MB
docker.io/hello-world   latest              e38bc07ac18e        5 weeks ago         1.85 kB
docker.io/centos        latest              e934aafc2206        6 weeks ago         199 MB
[root@docker1 ~]# docker run --name centos2 -i -t centos /bin/bash

[root@e9675771f1f2 /]# hostname
e9675771f1f2

[root@e9675771f1f2 /]# [CTRL+p}[CTRL+1] [root@docker1 ~]# hostname
docker1.ab.lab
[root@docker1 ~]# docker ps -s
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES               SIZE
e9675771f1f2        centos              "/bin/bash"         46 seconds ago      Up 45 seconds                           centos2             0 B (virtual 199 MB)
[root@docker1 ~]# docker ps -a -s
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES               SIZE
e9675771f1f2        centos              "/bin/bash"         53 seconds ago      Up 52 seconds                                   centos2             0 B (virtual 199 MB)
766f948a89d6        centos              "/bin/bash"         9 minutes ago       Exited (0) 3 minutes ago                        centos1             14 B (virtual 199 MB)
6ce4aa30164d        ubuntu              "bash"              32 minutes ago      Exited (0) 31 minutes ago                       kickass_payne       0 B (virtual 79.6 MB)
33f58ebbb82e        hello-world         "/hello"            32 minutes ago      Exited (0) 32 minutes ago                       clever_beaver       0 B (virtual 1.85 kB)


Attach to current running container:

[root@docker1 ~]# docker ps -s
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES               SIZE
e9675771f1f2        centos              "/bin/bash"         5 minutes ago       Up 5 minutes                            centos2             0 B (virtual 199 MB)
[root@docker1 ~]# docker attach e9675771f1f2
[root@e9675771f1f2 /]# hostname
e9675771f1f2


Start non-working container:

[root@docker1 ~]# docker ps -a -s 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES               SIZE
e9675771f1f2        centos              "/bin/bash"         14 minutes ago      Up 2 minutes                                    centos2             217 MB (virtual 416 MB)
766f948a89d6        centos              "/bin/bash"         23 minutes ago      Exited (0) 17 minutes ago                       centos1             14 B (virtual 199 MB)
6ce4aa30164d        ubuntu              "bash"              46 minutes ago      Exited (0) 45 minutes ago                       kickass_payne       0 B (virtual 79.6 MB)
33f58ebbb82e        hello-world         "/hello"            46 minutes ago      Exited (0) 46 minutes ago                       clever_beaver       0 B (virtual 1.85 kB)
[root@docker1 ~]# docker start 766f948a89d6
766f948a89d6
[root@docker1 ~]# docker attach  766f948a89d6
[root@766f948a89d6 /]# hostname
766f948a89d6


Stop current working container:

[root@docker1 ~]# docker ps -s
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES               SIZE
e9675771f1f2        centos              "/bin/bash"         17 minutes ago      Up 5 minutes                            centos2             217 MB (virtual 416 MB)
766f948a89d6        centos              "/bin/bash"         26 minutes ago      Up 2 minutes                            centos1             14 B (virtual 199 MB)
[root@docker1 ~]# docker stop 766f948a89d6
766f948a89d6
[root@docker1 ~]# docker ps -s
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES               SIZE
e9675771f1f2        centos              "/bin/bash"         18 minutes ago      Up 5 minutes                            centos2             217 MB (virtual 416 MB)


Kill unresponsive container:

[root@docker1 ~]# docker ps -s
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES               SIZE
e9675771f1f2        centos              "/bin/bash"         18 minutes ago      Up 5 minutes                            centos2             217 MB (virtual 416 MB)
[root@docker1 ~]# docker kill e9675771f1f2
e9675771f1f2
[root@docker1 ~]# docker ps -s
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES               SIZE


Remove container:

Container must be Powered off
[root@docker1 ~]# docker ps -a -s
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                         PORTS               NAMES               SIZE
e9675771f1f2        centos              "/bin/bash"         28 minutes ago      Exited (137) 12 seconds ago                        centos2             217 MB (virtual 416 MB)
766f948a89d6        centos              "/bin/bash"         37 minutes ago      Exited (137) 26 seconds ago                        centos1             14 B (virtual 199 MB)
6ce4aa30164d        ubuntu              "bash"              59 minutes ago      Exited (0) 59 minutes ago                          kickass_payne       0 B (virtual 79.6 MB)
33f58ebbb82e        hello-world         "/hello"            About an hour ago   Exited (0) About an hour ago                       clever_beaver       0 B (virtual 1.85 kB)
[root@docker1 ~]# docker rm e9675771f1f2
e9675771f1f2
[root@docker1 ~]# docker ps -a -s
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                         PORTS               NAMES               SIZE
766f948a89d6        centos              "/bin/bash"         37 minutes ago      Exited (137) 50 seconds ago                        centos1             14 B (virtual 199 MB)
6ce4aa30164d        ubuntu              "bash"              About an hour ago   Exited (0) 59 minutes ago                          kickass_payne       0 B (virtual 79.6 MB)
33f58ebbb82e        hello-world         "/hello"            About an hour ago   Exited (0) About an hour ago                       clever_beaver       0 B (virtual 1.85 kB)


Remove specific image:

You must remove the containers used it first
 
[root@docker1 ~]# docker image ls
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
docker.io/ubuntu        latest              452a96d81c30        3 weeks ago         79.6 MB
docker.io/hello-world   latest              e38bc07ac18e        5 weeks ago         1.85 kB
docker.io/centos        latest              e934aafc2206        6 weeks ago         199 MB
 
[root@docker1 ~]# docker rmi hello-world
Error response from daemon: conflict: unable to remove repository reference "hello-world" (must force) - container 33f58ebbb82e is using its referenced image e38bc07ac18e
 
[root@docker1 ~]# docker ps -a -s
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                         PORTS               NAMES               SIZE
766f948a89d6        centos              "/bin/bash"         49 minutes ago      Exited (137) 12 minutes ago                        centos1             14 B (virtual 199 MB)
6ce4aa30164d        ubuntu              "bash"              About an hour ago   Exited (0) About an hour ago                       kickass_payne       0 B (virtual 79.6 MB)
33f58ebbb82e        hello-world         "/hello"            About an hour ago   Exited (0) About an hour ago                       clever_beaver       0 B (virtual 1.85 kB)
 
[root@docker1 ~]# docker rm 33f58ebbb82e
33f58ebbb82e
 
[root@docker1 ~]# docker rmi hello-world
Untagged: hello-world:latest
Untagged: docker.io/hello-world@sha256:f5233545e43561214ca4891fd1157e1c3c563316ed8e237750d59bde73361e77
Deleted: sha256:e38bc07ac18ee64e6d59cf2eafcdddf9cec2364dfe129fe0af75f1b0194e0c96
Deleted: sha256:2b8cbd0846c5aeaa7265323e7cf085779eaf244ccbdd982c4931aef9be0d2faf


Execute commands into container without logging:

 
[root@docker1 ~]# docker ps -s
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES               SIZE
 
[root@docker1 ~]# docker ps -a -s
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                         PORTS               NAMES               SIZE
766f948a89d6        centos              "/bin/bash"         About an hour ago   Exited (137) 28 minutes ago                        centos1             14 B (virtual 199 MB)
6ce4aa30164d        ubuntu              "bash"              About an hour ago   Exited (0) About an hour ago                       kickass_payne       0 B (virtual 79.6 MB)
 
[root@docker1 ~]# docker start centos1
centos1
 
[root@docker1 ~]# docker exec centos1 cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core) 
 
[root@docker1 ~]# docker exec centos1 yum update -y 
 
[root@docker1 ~]# docker exec centos1 cat /etc/redhat-release 
CentOS Linux release 7.5.1804 (Core) 



01- Docker Beginner - Install

Based on the following Beginner Course:

https://www.safaribooksonline.com/library/view/docker-for-the/9781788991315/



OS information:

[root@docker1 ~]# lsb_release -a
LSB Version: :core-4.1-amd64:core-4.1-noarch
Distributor ID: CentOS
Description: CentOS Linux release 7.5.1804 (Core) 
Release: 7.5.1804
Codename: Core
[root@docker1 ~]# ip a show eth0
2: eth0:  mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:20:ef:be brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.178/24 brd 192.168.122.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:fe20:efbe/64 scope link 
       valid_lft forever preferred_lft forever
[root@docker1 ~]# cat /etc/resolv.conf 
# Generated by NetworkManager
search ab.lab
nameserver 192.168.122.1
nameserver 8.8.8.8
[root@docker1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.122.1   0.0.0.0         UG    100    0        0 eth0
192.168.122.0   0.0.0.0         255.255.255.0   U     100    0        0 eth0


Install Docker:

[root@docker1 ~]# yum install docker
[root@docker1 ~]# systemctl enable docker --now


Test your installation:

[root@docker1 ~]# docker --version
Docker version 1.13.1, build 94f4240/1.13.1
[root@docker1 ~]# docker -h 
[root@docker1 ~]# docker help image
[root@docker1 ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
[root@docker1 ~]# docker search hello | head -n2 
INDEX       NAME                                                 DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/hello-world                                Hello World! (an example of minimal Docker...   526       [OK]       
[root@docker1 ~]# docker pull hello-world
Using default tag: latest
Trying to pull repository docker.io/library/hello-world ... 
latest: Pulling from docker.io/library/hello-world
9bb5a5d4561a: Pull complete 
Digest: sha256:f5233545e43561214ca4891fd1157e1c3c563316ed8e237750d59bde73361e77
Status: Downloaded newer image for docker.io/hello-world:latest
[root@docker1 ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
Trying to pull repository docker.io/library/hello-world ... 
latest: Pulling from docker.io/library/hello-world
9bb5a5d4561a: Pull complete 
Digest: sha256:f5233545e43561214ca4891fd1157e1c3c563316ed8e237750d59bde73361e77
Status: Downloaded newer image for docker.io/hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/
[root@docker1 ~]# hostname
docker1.ab.lab
[root@docker1 ~]# docker help run | grep -E -e "[[:space:]] -i" -e "[[:space:]]-t"
  -i, --interactive                           Keep STDIN open even if not attached
  -t, --tty                                   Allocate a pseudo-TTY
[root@docker1 ~]# docker run -it ubuntu bash
Unable to find image 'ubuntu:latest' locally
Trying to pull repository docker.io/library/ubuntu ... 
latest: Pulling from docker.io/library/ubuntu
a48c500ed24e: Pull complete 
1e1de00ff7e1: Pull complete 
0330ca45a200: Pull complete 
471db38bcfbf: Pull complete 
0b4aba487617: Pull complete 
Digest: sha256:c8c275751219dadad8fa56b3ac41ca6cb22219ff117ca98fe82b42f24e1ba64e
Status: Downloaded newer image for docker.io/ubuntu:latest
root@18a61e60f7fa:/# hostname 
18a61e60f7fa


root@18a61e60f7fa:/# exit   
[root@docker1 ~]# hostname
docker1.ab.lab
[root@docker1 ~]# docker image ls
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
docker.io/ubuntu        latest              452a96d81c30        3 weeks ago         79.6 MB
docker.io/hello-world   latest              e38bc07ac18e        5 weeks ago         1.85 kB