Lab 3: Docker Internals

Now let’s have a more in depth look at the running containers.

Docker process inspection

Docker top gives you the running processes inside a container. We can see that we have the node server running inside the container.

docker top k8sdemo

> UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
> www                 35532               35512               0                   10:48               ?                   00:00:00            npm
> www                 35606               35532               0                   10:48               ?                   00:00:00            sh -c node ./bin/www
> www                 35607               35606               0                   10:48               ?                   00:00:00            node ./bin/www

Docker inspect outputs the detailed configuration of the running container.

docker inspect k8sdemo

> [
>     {
>         "Id": "48f5cd2708b27da27b0edcc1aaa3b6308249e6de94d45c0b123bf5fc7f2efbbf",
>         "Created": "2020-05-19T08:48:03.867340977Z",
>         "Path": "docker-entrypoint.sh",
>         "Args": [
>             "npm",
>             "start"
>         ],
>         "State": {
>             "Status": "running",
>  ...

Docker logs

In order to access the logs of the container (especially if it runs in the background with the -d option) you can use docker logs.

docker logs k8sdemo

> test@0.0.0 start /app
> node ./bin/www

Docker processes

As we have seen, running a Docker container starts an isolated process on the host system. So we are able to see those processed running in the VM. One for the frontend and one for the backend.

ps -Alf | grep "sh -c node"

> 4 S root        9474    9411  0  80   0 -  1070 -      10:00 ?        00:00:00 sh -c node ./bin/www
> 4 S root       12020   11960  0  80   0 -  1070 -      10:04 ?        00:00:00 sh -c node ./bin/www

The pstree command shows the process in the complete process tree. If you execute the following command you can see that the processes from our demo application are running under the containerd process, which is the container runtime used by Docker for this lab.

pstree | grep node

>        |-containerd-+-containerd-shim-+-portainer---10*[{portainer}]
>
>  ...
>
>        |            |-containerd-shim-+-npm-+-sh---node---9*[{node}]
>        |            |                 `-9*[{containerd-shim}]
>        |            |-containerd-shim-+-npm-+-sh---node---5*[{node}]
>        |            |                 `-9*[{containerd-shim}]
>        |            `-17*[{containerd}]

Docker exec

Docker exec allows to run commands inside already running containers. This is very useful for debugging.

docker exec -it k8sdemo /bin/bash

You get a prompt inside the container. Now you can explore the content of the running container.

  1. List the files in the home directory

    root@9bffd6c12fb0:/app# ls -al
       
    > total 84
    > drwxr-xr-x 1 root root  4096 May 14 07:25 .
    > drwxr-xr-x 1 root root  4096 May 19 08:04 ..
    > -rw-rw-r-- 1 root root  6148 May 14 06:55 .DS_Store
    > -rw-rw-r-- 1 root root   496 May 14 06:55 Dockerfile
    > -rw-rw-r-- 1 root root    78 May 14 06:55 README
    > -rw-rw-r-- 1 root root  1226 May 14 06:55 app.js
    > drwxrwxr-x 2 root root  4096 May 14 06:55 bin
    > -rw-rw-r-- 1 root root   306 May 14 06:55 build.sh
    > drwxrwxr-x 1 root root  4096 May 14 06:55 node_modules
    > -rw-rw-r-- 1 root root 26382 May 14 06:55 package-lock.json
    > -rw-rw-r-- 1 root root   307 May 14 06:55 package.json
    > drwxrwxr-x 5 root root  4096 May 14 06:55 public
    > drwxrwxr-x 2 root root  4096 May 14 06:55 routes
    > drwxrwxr-x 2 root root  4096 May 14 06:55 views
    
    

    ``

  2. List the web files for the node application

    root@9bffd6c12fb0:/app# cd public/
    

    ``

    root@9bffd6c12fb0:/app# ls -al
       
    > total 144
    > drwxrwxr-x 1 root root  4096 May 14 06:55 .
    > drwxr-xr-x 1 root root  4096 May 14 07:25 ..
    > -rw-rw-r-- 1 root root  6148 May 14 06:55 .DS_Store
    > -rw-rw-r-- 1 root root 32939 May 14 06:55 404.html
    > -rw-rw-r-- 1 root root 55515 May 14 06:55 500.html
    > drwxrwxr-x 2 root root  4096 May 14 06:55 images
    > -rwxrwxr-x 1 root root  6240 May 19 08:22 index.html
    > -rwxrwxr-x 1 root root  1188 May 14 06:55 index.js
    > drwxrwxr-x 4 root root  4096 May 14 06:55 public
    > -rwxrwxr-x 1 root root  1157 May 14 06:55 style.css
    > drwxrwxr-x 2 root root  4096 May 14 06:55 stylesheets
    

    ``

  3. Modify the HTML code for the running container.

    root@9bffd6c12fb0:/app# echo "HELLO" >> index.html 
       
    

    ``

  4. Reload the Web App and look for the HELLO text displayed in the lower left part of the page.

  5. Type exit in order to get back to the commandline