What is Docker

Docker is one of container implementation. Others are Rocket, Drawbridge, LXC.
Docker Inc. is the company that sells the commercial version of Docker. Docker is also available as open source.
Docker Daemon / dockerd? Manages Docker objects(Eg: Images, containers, networks, and volumes). dockerd also communicate with other daemons to manage Docker services.
Docker client / docker? Users interact with docker client.

            User 
            $ docker run -----> [Docker Client] -----> [Docker Daemon/dockerd]
        

Docker vs Kubernets

Docker Kubernetes(Container Orchestration system)
What Platform for building containers Platform for managing multiple containers
Use case Run containers on 1 host Run containers on cluster of machines, providing scaling, self-healing, and rollback
Commands docker run helm

Docker Terms

Term Description
Docker Host Machine on which the Docker containers run. It can be a virtual machine or a physical machine
Container Image/Docker Image This is used to start the container.
Docker File Shell script for creating Docker image/Container image.

# vim application.Dockerfile 
FROM ubuntu:20.04                             //FROM Command: Installation is based image this OS, Since docker is not full OS.
RUN apt update \                              //RUN Command: Run some command
    apt-get install -y --no-install-recommends openssl \
    apt-get install -y --no-install-recommends iputils-ping \

COPY ./application.out ./a.txt /app/

CMD /app/application.out "--config" a.txt     //CMD Command: Start the application
                    
Docker Registry Stores docker images. Same as github,gitlab are for source code. Eg: dockerhub, elastic container registry(ecr)

Docker Volumes

This is a persistent storage location managed by Docker, outside the container’s filesystem.
Helpful when you want to store data present in database across container reboots/deletion/recreation.
Volumes are defined in docker-compose.yml and mapped to a path inside the container.
volumes, bind mounts, tmpfs all are seen as mounted directories to container.

Name of docker volume:
Docker Compose auto-generates the volume name as project-name_volume

// Check volumes
> docker volume ls
go-server_db_data

// Delete a volume
> docker volume rm go-server_db_data
go-server_db_data
        

Docker Mounts, tmpfs

Does not persists data across reboots

Docker Networking

Connecting Docker Containers with each other and with outside world.

Types of Docker Networking

Network Description
Bridge Networking Used when multiple containers want to communicate on the same Docker host.

Types of Bridges:
User Defined Bridge Default Bridge
Connect using IP Address or hostname Only IP Address

Run 2 different Alpine containers on same docker host and commmunicate between them

////Overall Pictutre
    Container-1(172.17.0.2)
    Container-2(172.17.0.3)
                    Bridge(172.17.0.1)                    
    Docker host


1. Start Docker desktop. Start Windows Powershell and list the network.

ps> docker network ls

NETWORK ID      NAME       DRIVER     SCOPE
17e324f45964    bridge     bridge     local   //Default bridge network
6ed54d316334    host       host       local
7092879f2cc8    none       null       local

$ docker run -dit --name alpine1 alpine ash   //2. Start 2 alpine containers, running ash(This is default alpine shell rather than bash).
$ docker run -dit --name alpine2 alpine ash

$ docker container ls                        //3. Check containers have started or not?
CONTAINER ID        IMAGE      COMMAND     CREATED          STATUS             PORTS        NAMES
602dbf1edc81        alpine     "ash"       4 seconds ago    Up 3 seconds                   alpine2
da33b7aa74b0        alpine     "ash"       17 seconds ago   Up 16 seconds                  alpine1

$ docker network inspect bridge              //4. Check Brigde network to see to what containers are connected to?

[
    {
        "Name": "bridge",
        "Id": "17e324f459648a9baaea32b248d3884da102dde19396c25b30ec800068ce6b10",
        "Created": "2017-06-22T20:27:43.826654485Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.17.0.0/16",
                    "Gateway": "172.17.0.1"         //Gateway between docker host and Bridge N/W
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Containers": {
            "602dbf1edc81813304b6cf0a647e65333dc6fe6ee6ed572dc0f686a3307c6a2c": {
                "Name": "alpine2",
                "EndpointID": "03b6aafb7ca4d7e531e292901b43719c0e34cc7eef565b38a6bf84acf50f38cd",
                "MacAddress": "02:42:ac:11:00:03",
                "IPv4Address": "172.17.0.3/16",
                "IPv6Address": ""
            },
            "da33b7aa74b0bf3bda3ebd502d404320ca112a268aafe05b4851d1e3312ed168": {
                "Name": "alpine1",
                "EndpointID": "46c044a645d6afc42ddd7857d19e9dcfb89ad790afb5c239a35ac0af5e8a5bc5",
                "MacAddress": "02:42:ac:11:00:02",
                "IPv4Address": "172.17.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {
            "com.docker.network.bridge.default_bridge": "true",
            "com.docker.network.bridge.enable_icc": "true",
            "com.docker.network.bridge.enable_ip_masquerade": "true",
            "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
            "com.docker.network.bridge.name": "docker0",
            "com.docker.network.driver.mtu": "1500"
        },
        "Labels": {}
    }
]

$ docker attach alpine1                         //5. Since containers are running in background. Run attach command to connect to container.

/ # ip addr show      //list IP Address
# ping google.com     //Connected to internet
PING google.com (172.217.3.174): 56 data bytes
64 bytes from 172.217.3.174: seq=0 ttl=41 time=9.841 ms
64 bytes from 172.217.3.174: seq=1 ttl=41 time=9.897 ms

# ping 172.17.0.3     //Ping another container
PING 172.17.0.3 (172.17.0.3): 56 data bytes
64 bytes from 172.17.0.3: seq=0 ttl=64 time=0.086 ms
64 bytes from 172.17.0.3: seq=1 ttl=64 time=0.094 ms

//7. Detach from alpine1 without stopping it by using the detach sequence, CTRL + p CTRL + q (hold down CTRL and type p followed by q). 
//8. Stop and remove both containers

$ docker container stop alpine1 alpine2
$ docker container rm alpine1 alpine2
                    
None Networking This completely disables the networking stack on the container. Within the container, only the loopback device is created

Example:

$ docker run --rm -dit  --network none  --name no-net-alpine   alpine:latest   ash

//Check the container’s network stack... no eth0 created
$ docker exec no-net-alpine ip link show
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: tunl0@NONE:  mtu 1480 qdisc noop state DOWN qlen 1
    link/ipip 0.0.0.0 brd 0.0.0.0
3: ip6tnl0@NONE:  mtu 1452 qdisc noop state DOWN qlen 1
    link/tunnel6 00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00 brd 00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00
    
//Stop the container. It is removed automatically because it was created with the --rm flag
$ docker stop no-net-alpine
                    
Host Networking Container's network stack is same as host network stack. IP Address of container is same as host OS. Useful in situtation where container needs to listen/read/write on large number of ports.
Advantages 1. Expose 1 port and IP access the service. Example: 50 Game servers are running in Docker containers on same host. Requests are routed using kubernets to free game server.
Disadvantages: Host networking driver only work on linux not on Windows and MAC.

Example: Nginx container binds directly to port 80 on the Docker host:
Start ngnix in container with host networking, ngnix listens on port 80 which is same as docker host.

# docker run --rm -d --network host --name my_nginx nginx         //Create and start container as detached process
# ip addr show 
# sudo netstat -tulpn | grep :80                                  //Check which process is binded to port 80.
# docker container stop my_nginx                                  //Stop the container, it will be removed automatically since start with -rm switch