Day 16: 90DaysOfChallenge

Day 16: 90DaysOfChallenge

Docker for DevOps Engineers

What is Docker?

Docker is an open-source platform that lets you build, test and deploy applications quickly. Dockers packages the application into a loosely isolated environment called containers which contains everything needed to run the application like system libraries, system tools, code, Operating System,etc. Docker is lightweight as it uses the Kernel of the host Operating System, unlike hypervisor-based virtual machines, so you can use more of your server capacity to achieve your business goals. Docker streamlines the development lifecycle by allowing developers to work in standardized environments using local containers. Containers are great for continuous integration and continuous delivery (CI/CD) workflows.

Docker’s portability and lightweight nature make it easy to dynamically manage workloads, scaling up or tearing down applications and services as business needs dictate, in near real time.

Docker Engine

Docker Engine is a service that allows you to run any containers on any host Operating System. It has 3 main components-

  • Docker d- Here, d stands for daemon. Docker daemon is a service that manages Docker objects such as images, containers, networks, and volumes in the background. It helps to start, stop, move, or delete a container using low-level service called container d.

  • Docker CLI- You can access everything like create, start, stop, move, or delete a container using the Command Line Interface.

  • Container d- It manages the container lifecycle, and provides image and filesystem management. It’s a low-level building block, designed to be integrated into other systems, such as Docker and Kubernetes. It is used as the default runtime for Docker.

Images

Docker images are a set of instructions used for creating a 'n' number of Docker containers. Often, an image is based on another image, with some additional customization. You might create your own images or you might only use those created by others and published in a registry like DockerHub. To build your own image, you create a Dockerfile with a simple syntax for defining the steps needed to create the image and run it.

Containers

A container is a runnable instance of an image. You can create, start, stop, move, or delete a container using the Docker API or CLI. You can connect a container to one or more networks, attach storage to it, or even create a new image based on its current state. A container is defined by its image as well as any configuration options you provide to it when you create or start it. When a container is removed, any changes to its state that aren’t stored in persistent storage disappear.

Docker Registries

A Docker registry stores Docker images. Docker Hub is a public registry that anyone can use, and Docker looks for images on Docker Hub by default. You can even run your private registry.

Tasks:

  1. Use the docker run command to start a new container and interact with it through the command line.

    docker run [docker_image] can run containers from locally stored Docker images or if it is not on your system, the software pulls it from the online registry. For example, we have used a Docker image "hello-world" which is pulled from the registry to start and run the container.

ubuntu@ip-172-31-92-1:~$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
719385e32844: Pull complete 
Digest: sha256:dcba6daec718f547568c562956fa47e1b03673dd010fe6ee58ca806767031d1c
Status: Downloaded newer image for 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/get-started/
  1. Use the docker inspect command to view detailed information about a container or image.

    docker inspect <image-name> is used to get low-level configuration information about various Docker objects like images, containers, volumes, networks, nodes, etc, for example, on which node a container is deployed or what ports it listens to.

ubuntu@ip-172-31-92-1:~$ docker inspect hello-world
[
    {
        "Id": "sha256:9c7a54a9a43cca047013b82af109fe963fde787f63f9e016fdc3384500c2823d",
        "RepoTags": [
            "hello-world:latest"
        ],
        "RepoDigests": [
            "hello-world@sha256:dcba6daec718f547568c562956fa47e1b03673dd010fe6ee58ca806767031d1c"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2023-05-04T17:37:03.872958712Z",
        "Container": "347ca68872ee924c4f9394b195dcadaf591d387a45d624225251efc6cb7a348e",
        "ContainerConfig": {
            "Hostname": "347ca68872ee",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"/hello\"]"
            ],
            "Image": "sha256:62a15619037f3c4fb4e6ba9bd224cba3540e393a55dc52f6bebe212ca7b5e1a7",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {}
        },
        "DockerVersion": "20.10.23",
        "Author": "",
        "Config": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/hello"
            ],
            "Image": "sha256:62a15619037f3c4fb4e6ba9bd224cba3540e393a55dc52f6bebe212ca7b5e1a7",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": null
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 13256,
        "VirtualSize": 13256,
        "GraphDriver": {
            "Data": {
                "MergedDir": "/var/lib/docker/overlay2/c29fd4f9d0243e098b5f4387b384046cf009c4f22190328aa8880cab4c568e91/merged",
                "UpperDir": "/var/lib/docker/overlay2/c29fd4f9d0243e098b5f4387b384046cf009c4f22190328aa8880cab4c568e91/diff",
                "WorkDir": "/var/lib/docker/overlay2/c29fd4f9d0243e098b5f4387b384046cf009c4f22190328aa8880cab4c568e91/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:01bb4fce3eb1b56b05adf99504dafd31907a5aadac736e36b27595c8b92f07f1"
            ]
        },
        "Metadata": {
            "LastTagTime": "0001-01-01T00:00:00Z"
        }
    }
]
  1. Use the docker port command to list the port mappings for a container.

    docker port CONTAINER [PRIVATE_PORT[/PROTO]] List port mappings or a specific mapping for the container

ubuntu@ip-172-31-92-1:~/dockerProjects$ docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED       STATUS       PORTS                 NAMES
6e4a1faca8f7   mysql:latest   "docker-entrypoint.s…"   2 hours ago   Up 2 hours   3306/tcp, 33060/tcp   eager_booth
ubuntu@ip-172-31-92-1:~/dockerProjects$ docker port eager_booth 3306/tcp
Error: No public port '3306/tcp' published for eager_booth
ubuntu@ip-172-31-92-1:~/dockerProjects$ docker port eager_booth 33060/tcp
Error: No public port '33060/tcp' published for eager_booth
  1. Use the docker stats command to view resource usage statistics for one or more containers.

    docker stats <containerid> command returns a live data stream for running containers. To limit data to one or more specific containers, specify a list of container names or ids separated by a space.

ubuntu@ip-172-31-92-1:~$ docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS                 NAMES
6e4a1faca8f7   mysql:latest   "docker-entrypoint.s…"   6 minutes ago   Up 6 minutes   3306/tcp, 33060/tcp   eager_booth
ubuntu@ip-172-31-92-1:~$ docker stats 6e4a1faca8f7
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
CONTAINER ID   NAME          CPU %     MEM USAGE / LIMIT     MEM %     NET I/O       BLOCK I/O       PIDS
6e4a1faca8f7   eager_booth   0.41%     350.2MiB / 957.1MiB   36.59%    1.16kB / 0B   203MB / 272MB   37
  1. Use the docker top command to view the processes running inside a container.

    docker top <containerid> is used to display the running processes of a container.

ubuntu@ip-172-31-92-1:~$ docker top 6e4a1faca8f7
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
lxd                 2246                2226                0                   15:52               ?                   00:00:03            mysqld
  1. Use the docker save command to save an image to a tar archive.

    docker save [OPTIONS] IMAGE [IMAGE...] Save one or more images to a tar archive (streamed to STDOUT by default). It produces a tarred repository to the standard output stream.

    Options[ --output , -o ] - Write to a file, instead of STDOUT

ubuntu@ip-172-31-92-1:~$ docker save mysql:latest --output mysql.tar
ubuntu@ip-172-31-92-1:~$ ls -l mysql.tar
-rw------- 1 ubuntu ubuntu 592743424 Aug 15 16:10 mysql.tar
  1. Use the docker load command to load an image from a tar archive.

docker load [OPTIONS] Load an image from a tar archive or STDIN. Load an image or repository from a tar archive from a file or STDIN. It restores both images and tags.

Options[--input , -i ] - Read from tar archive file, instead of STDIN

ubuntu@ip-172-31-92-1:~$ docker load --input mysql.tar
Loaded image: mysql:latest

Thanks for reading!
~Shilpi