Docker useful commands
This is a brief post just to share some useful Docker commands that might not be so well-known.
Showing events
The following command shows all the events and it can filter them by timestamp, type, etc. [1].
$ docker events
It is especially useful for troubleshooting.
Showing differences in file system
The following command shows the changes ocurred on the container’s file system
since it was started. As the docker events
command might help with
troubleshooting.
$ docker diff <container_name>
Saving and loading images
The save
and load
commands allow to export container images as snapshots
and import them into other nodes. This could save you from some disasters when
local images are not stored on any container registry. The following is an
example:
$ docker save <image_id> -o <image_name.tar>
The *.tar
file can be moved to any other node which might need that image and
imported by executing:
$ docker load < <image_name.tar>
Process running inside a container
Although it is quite common to attach to a running container by executing an
interactive load like a shell docker exec -it <container_name> sh
, it is also
useful to execute a top inside by just executing:
$ docker top <container_name>
Image history
Sometimes we don’t have the Dockerfile which was used to build the image, but
we want to know the commands used to do it; history
comes to the rescue:
$ docker history <image_id>
Search for available images on Docker Hub
The following command allows you to search for images available on Docker Hub
$ docker search <string>
Let’s comment on the output:
$ docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 15178 [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 2048 [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 816 [OK]
jc21/nginx-proxy-manager Docker container for managing Nginx proxy ho… 217
linuxserver/nginx An Nginx container, brought to you by LinuxS… 149
tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp… 135 [OK]
jlesage/nginx-proxy-manager Docker container for Nginx Proxy Manager 123 [OK]
alfg/nginx-rtmp NGINX, nginx-rtmp-module and FFmpeg from sou… 102 [OK]
jasonrivers/nginx-rtmp Docker images to host RTMP streams using NGI… 92 [OK]
nginxdemos/hello NGINX webserver that serves a simple page co… 70 [OK]
privatebin/nginx-fpm-alpine PrivateBin running on an Nginx, php-fpm & Al… 56 [OK]
nginx/nginx-ingress NGINX and NGINX Plus Ingress Controllers fo… 55
nginxinc/nginx-unprivileged Unprivileged NGINX Dockerfiles 44
staticfloat/nginx-certbot Opinionated setup for automatic TLS certs lo… 24 [OK]
nginx/nginx-prometheus-exporter NGINX Prometheus Exporter for NGINX and NGIN… 19
schmunk42/nginx-redirect A very simple container to redirect HTTP tra… 19 [OK]
centos/nginx-112-centos7 Platform for running nginx 1.12 or building … 15
centos/nginx-18-centos7 Platform for running nginx 1.8 or building n… 13
raulr/nginx-wordpress Nginx front-end for the official wordpress:f… 13 [OK]
flashspys/nginx-static Super Lightweight Nginx Image 10 [OK]
mailu/nginx Mailu nginx frontend 9 [OK]
sophos/nginx-vts-exporter Simple server that scrapes Nginx vts stats a… 7 [OK]
navidonskis/nginx-php5.6 Docker nginx + php5.6 on Ubuntu 7 [OK]
ansibleplaybookbundle/nginx-apb An APB to deploy NGINX 2 [OK]
wodby/nginx Generic nginx 1 [OK]
The images are sorted by their popularity and we can see if they are official and whether they are updated automatically or not.
Update a running container
Sometimes we want to update a running container without stopping it, for example, to give it a new restarting policy:
$ docker update --restart=unless-stopped <container_id>
Waiting for a container to finish
This command is useful to create scripts that wait until a container finishes and prints its exit code.
$ docker wait <container_name>