Getting started with Docker containers and images to deploy Python application – part 2 (new automation system – part 17)

  1. Preface
    After going over the basic and minimal stuff required to compose an initial working Dockerfile “draft” to host (and eventually run) the automation project in this previous post , in this post I will go over some enhancement and more basic stuff that need to be done in order to “finalize” the Dockerfile and also to run the Docker container (in this post NOT within the context of the Python automation project/application, but in general).
  2. Building Docker image from a Dockerfile
    Once the Dockerfile is ready, now it is time to build (create) a Docker image from this Dockerfile that later on will be used to run Docker container(s). In order to build a Dokcer image from a Dockerfile it is first important to understand where does this Dockerfile exist and what other files/directories are present in this folder or any other sub folders of it, due to the fact that once the build “command” is executed on a given Dockerfile, it takes into account all other file(s) that might reside in the mentioned places. In this case the Dockerfile is the only file in the directory where it reside.
    Assuming the current working directory is the folder in which the Dockerfile is present, the command that is used to build an image from it is the following:
    docker build -t=”image_name” .
    # The -t=”image_name” will be the “name” of the image.
  3. Inspecting Docker image(s)
    After that the initial-minimal Dockerfile for the container was built, it is possible to view its “content” using the images docker related commands.

    1. List all images:
      In order to view all the images that the Docker engine is “aware” of, run the docker images command. In my case the output looked like so:

      guya@guy-vm:~/dev/dockerEngine/ubuntu18_04_python_3Dockerfile$ docker images
      REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
      automation_image_1   latest              234fb833c004        54 seconds ago      602MB
                                   bc852e99bba7        2 months ago        92.6MB
                                   b6548ab364f4        2 months ago        92.6MB
                                   bd842f11bc67        2 months ago        92.6MB
      ubuntu               18.04               c3c304cb4f22        2 months ago        64.2MB
      ubuntu               latest              4e5021d210f6        4 months ago        64.2MB
      hello-world          latest              bf756fb1ae65        6 months ago        13.3kB

      In this case, the image of our interest is the automation_image_1 image.

    2. Display all layers of an image:
      In order to have a “dipper” look on all the layers a Docker image is composed of, one can run the following command: docker history <image_id>

      guya@guy-vm:~/dev/dockerEngine/ubuntu18_04_python_3Dockerfile$ docker history 234fb833c004
      IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
      234fb833c004        3 minutes ago       /bin/sh -c update-alternatives --install /usג€¦   10.8kB              
      dc571c0f31e5        3 minutes ago       /bin/sh -c python3.8 -m pip install wheel       556kB               
      e00fa3087958        3 minutes ago       /bin/sh -c python3.8 -m pip install pip --upג€¦   9.81MB              
      724be0f48cb2        3 minutes ago       /bin/sh -c apt-get install -y git               44.2MB              
      ff579e02b3ef        4 minutes ago       /bin/sh -c apt-get install -y build-essentiaג€¦   359MB               
      b0bc64e4bd27        6 minutes ago       /bin/sh -c apt-get update &&   apt-get instaג€¦   124MB               
      c3c304cb4f22        2 months ago        /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B                  
                 2 months ago        /bin/sh -c mkdir -p /run/systemd && echo 'doג€¦   7B                  
                 2 months ago        /bin/sh -c set -xe   && echo '#!/bin/sh' > /ג€¦   745B                
                 2 months ago        /bin/sh -c [ -z "$(apt-get indextargets)" ]     987kB               
                 2 months ago        /bin/sh -c #(nop) ADD file:c3e6bb316dfa6b81dג€¦   63.2MB

      As you can see, it is almost a “one-to-one” mapping between each row in the Dockerfile –> to a layer of the complete docker image. This concept of layering by “separate” command is a topic that can be much more elaborated on and used in order to (mainly) reduce the finalized image size (here I will not “deep dive” into it).

  4. Running a Docker container
    Now that the image is finally set, it is now time to run the Docker container. Recall that a container, simply put, is a running instance of a Docker image.An important note to point here is that a container “lives” (runs) as long as its process(es) that it was brought up to execute in the first place, are running. Once these process(es) are terminated, the container is “terminated” (stop running) as well.

    1. Running a docker container “interactively”
      In order to run the container with the capability to “log into” it (SSH wise), one can run the following command:
      docker run -i -t , for example:
      docker run -i -t automation_image_1 /bin/bash

      1. The -i argument is for interactive mode, meaning, the stdin of the container is kept “open”.
      2. The -t argument is in order to open a pseudo TTY towards the container.
    2. Observing the container process(es)
      Once the container is up and running, one can see ALL the “user defined” processes that are running within the container by executing the command:
      docker ps
      In this example, if we run this command we shall see output similar to the following one:

      guya@guy-vm:~/dev/dockerEngine/ubuntu18_04_python_3Dockerfile$ docker ps
      CONTAINER ID        IMAGE                COMMAND             CREATED             STATUS              PORTS               NAMES
      f271bec8af9a        automation_image_1   "/bin/bash"         8 minutes ago       Up 8 minutes                            goofy_kowalevski
    3. Terminating a container:
      In this example, the container was started in an interactive mode with an “bash SSH” connection as its “main process”. So, sticking to what was mentioned earlier, in order to kill (stop) the container, all its process(es) MUST be terminated. In this case, the docker run command automatically opens an SSH session towards the container, so to kill it, in this case, it is “enough” to kill (exit) the SSH session. After doing so, the output of the docker ps command will be “empty”, as follows:

      guya@guy-vm:~/dev/dockerEngine/ubuntu18_04_python_3Dockerfile$ docker ps
      CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES


  5. Conclusions
    In this post, a further dive into Docker images and container was introduced.
    The full “container cycle flow” of compose-build-run-kill Docker image and container was introduced along with sample commands for minimal working examples.Resources:
    a) Docker run argument from the official website
    The picture:
    Chauchilla Cemetery, near the city of Nazca, Peru.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s