Ten useful Docker commands

Written by
Date: 2020-07-29 21:10:00 00:00


Introduction

Docker launched in 2011, but was founded in 2010 by Solomon Hykes, and was finally released to the public in 2013.

Docker is an Open Source project, that allows running virtualized containers where complete applications run. It is like Virtual Machines, but, a lot lighter, because the Kernel is shared between the host and all containers.

10 Useful commands of Docker

We'll see here some very useful and not well-known docker commands:

1. docker save

The first command we will see, is docker save, it saves docker images to tar files, and then those files can be shared or moved to other machines.

docker images

The output should be something like this:

REPOSITORY TAG IMAGE ID CREATED SIZE
nextcloud latest 3ade53def063 6 days ago 752MB
portainer/portainer latest 62771b0b9b09 6 days ago 79.1MB
tvial/docker-mailserver latest 8351e42ea61d 13 days ago 603MB
zerok/webmentiond latest cf76e9eee58b 2 weeks ago 26.4MB
bitwardenrs/server latest e572351fa819 3 weeks ago 131MB

And now to save a Docker image to a file in order to share it run:

docker save 3ade53def063 > nextcloud_image.tar

Now, that file can be shared.

2. docker load

Once, on the other computer, the image can be restored with docker load

docker load < nextcloud_image.tar

And now the image is available on the new server.

3. docker top

This command allows checking the process that runs inside a container, without the need to attach to that container.

docker top <container_id>

The output should look like:

UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
101                 3369276             3369258             0                   Jul25               ?                   00:00:24            varnishd -F -f /etc/varnish/default.vcl -a http=:80,HTTP -a proxy=:8443,PROXY -s malloc,100M
102                 3369333             3369276             0                   Jul25               ?                   00:02:26            varnishd -F -f /etc/varnish/default.vcl -a http=:80,HTTP -a proxy=:8443,PROXY -s malloc,100M

4. docker events

With docker events you can get what is going on the host, on the Docker server, everything related to the containers of course. This is a good debugging command, in case some Docker Container fails to boot.

docker events

5. docker diff

This command displays the differences between the container when it was created, until the time the command docker diff is run. Let's find first a container to run the command on.

docker ps

Once we have the container ID we can run the command

docker diff <container_id>

And the output should looks like this

C /run
C /run/apache2
A /run/apache2/apache2.pid

In this example, the file was run on an Apache Server, so you can see that two folders were created, and one file was added.

6. docker history

This command is useful when you need to know what were used to create the image, this is in case you do not have the Dockerfile, it will give you a good idea on how the image was built.

docker image ls

With that command you will list all images in your system, pick the one you want to how it was created, and run

docker history <image_id>

I run the command on bitwarden_rs image, this was the output:

IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
e572351fa819        3 weeks ago         /bin/sh -c #(nop)  CMD ["/start.sh"]            0B
<missing>           3 weeks ago         /bin/sh -c #(nop) WORKDIR /                     0B
<missing>           3 weeks ago         /bin/sh -c #(nop)  HEALTHCHECK &{["CMD" "/he…   0B
<missing>           3 weeks ago         /bin/sh -c #(nop) COPY file:54ff4b025c7a95d6…   251B
<missing>           3 weeks ago         /bin/sh -c #(nop) COPY file:9cb86afa23b0b725…   1.68kB
<missing>           3 weeks ago         /bin/sh -c #(nop) COPY file:b5bc0a7608eab1f6…   22.6MB
<missing>           3 weeks ago         /bin/sh -c #(nop) COPY dir:c9bab59b3e8aa698e…   24.2MB
<missing>           6 weeks ago         /bin/sh -c #(nop) COPY file:5b7ffe6343912d16…   41B
<missing>           6 weeks ago         /bin/sh -c #(nop)  EXPOSE 3012                  0B
<missing>           6 weeks ago         /bin/sh -c #(nop)  EXPOSE 80                    0B
<missing>           6 weeks ago         /bin/sh -c #(nop)  VOLUME [/data]               0B
<missing>           6 weeks ago         /bin/sh -c mkdir /data                          0B
<missing>           6 weeks ago         /bin/sh -c apt-get update && apt-get install…   15.1MB
<missing>           6 weeks ago         /bin/sh -c #(nop)  ENV ROCKET_WORKERS=10        0B
<missing>           6 weeks ago         /bin/sh -c #(nop)  ENV ROCKET_PORT=80           0B
<missing>           6 weeks ago         /bin/sh -c #(nop)  ENV ROCKET_ENV=staging       0B
<missing>           7 weeks ago         /bin/sh -c #(nop)  CMD ["bash"]                 0B
<missing>           7 weeks ago         /bin/sh -c #(nop) ADD file:4d35f6c8bbbe6801c…   69.2MB

7. docker pause / docker unpause

I use this command when I run scripts to backup docker volumes, it is a good practice to pause the docker container for some seconds make the backup and unpause the docker container. This command sends SIGSTOP to the processes running inside the container.

docker pause <container_id>

And to unpause it:

docker unpause <container_ide>

With this command you can search images from the command line, without the need to go to the docker site, let's see an example

docker search nginx

The output will be:

NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
nginx                              Official build of Nginx.                        13524               [OK]
jwilder/nginx-proxy                Automated Nginx reverse proxy for docker con…   1846                                    [OK]
richarvey/nginx-php-fpm            Container running Nginx + PHP-FPM capable of…   780                                     [OK]
linuxserver/nginx                  An Nginx container, brought to you by LinuxS…   123
bitnami/nginx                      Bitnami nginx Docker Image                      87                                      [OK]
tiangolo/nginx-rtmp                Docker image with Nginx using the nginx-rtmp…   85                                      [OK]
jc21/nginx-proxy-manager           Docker container for managing Nginx proxy ho…   73
alfg/nginx-rtmp                    NGINX, nginx-rtmp-module and FFmpeg from sou…   71                                      [OK]
nginxdemos/hello                   NGINX webserver that serves a simple page co…   57                                      [OK]
jlesage/nginx-proxy-manager        Docker container for Nginx Proxy Manager        53                                      [OK]
nginx/nginx-ingress                NGINX Ingress Controller for Kubernetes         37
privatebin/nginx-fpm-alpine        PrivateBin running on an Nginx, php-fpm & Al…   31                                      [OK]
schmunk42/nginx-redirect           A very simple container to redirect HTTP tra…   18                                      [OK]
nginxinc/nginx-unprivileged        Unprivileged NGINX Dockerfiles                  16
nginx/nginx-prometheus-exporter    NGINX Prometheus Exporter                       14
raulr/nginx-wordpress              Nginx front-end for the official wordpress:f…   13                                      [OK]
centos/nginx-18-centos7            Platform for running nginx 1.8 or building n…   13
centos/nginx-112-centos7           Platform for running nginx 1.12 or building …   13
sophos/nginx-vts-exporter          Simple server that scrapes Nginx vts stats a…   7                                       [OK]
mailu/nginx                        Mailu nginx frontend                            7                                       [OK]
bitwarden/nginx                    The Bitwarden nginx web server acting as a r…   6
bitnami/nginx-ingress-controller   Bitnami Docker Image for NGINX Ingress Contr…   6                                       [OK]
wodby/nginx                        Generic nginx                                   1                                       [OK]
ansibleplaybookbundle/nginx-apb    An APB to deploy NGINX                          1                                       [OK]

Results are sorted by starts, and if there is an official image, it is clearly marked

9. docker update

With docker update you can update an option in an already running container, without the need to restart it. You can update some options only, for example

 docker update -m <momory_limit> <container_id>

or

docker update --restart=always <container_id>

10. docker wait

With docker wait the command will wait until the container stops, and outputs the error of the container if any. You can use it for scripts, because you can run a container, and if you need the result of it before running the next command, you just need to run docker wait <container_id> and then the next command in your script.