Some useful docker commands
2 min read

Some useful docker commands

Some useful docker commands

The other day I had to reboot one of my machines running docker. I've noticed that only a number of containers restarted on reboot. What's worse, is that my pihole container didn't. Oops. So I've decided to look into it and found a number of docker commands which helped me:

Processes

Docker has a ps command which I normally use like:

$ docker ps
CONTAINER ID   IMAGE                                 COMMAND                  CREATED       STATUS                  PORTS                                                                                                                                                                                          NAMES
ba7314beea63   requarks/wiki:2.5                     "docker-entrypoint.s…"   4 weeks ago   Up 33 hours             <...>       wiki
fd2f83b02eb9   cbcrowe/pihole-unbound:latest         "/bin/bash -c ./star…"   4 weeks ago   Up 33 hours (healthy)   <...>       pihole
11fef420ad5c   linuxserver/unifi-controller:latest   "/init"                  4 weeks ago   Up 4 minutes            <...>       unifi

Note: You can use docker ps -a to show all containers, not only the started ones. I found this useful when I built my own containers and varied versions.

Show container ID

You can show only the container ID with:

$ docker ps -q
ba7314beea63
fd2f83b02eb9
11fef420ad5c

This is useful when you want to loop through the (active) containers.

Restart policy

I've used the above commands in the past, but now I wanted to make all my containers to start automatically on reboot.

Docker has four container (re)start policies:

Policy Default Description
no * Don't restart the container automatically
on-failure Restart if the container exits because of an error (non-zero exit code)
always Always restart the container if it stops. If manually stopped, the container is restarted only if the Docker daemon restarts or the container is manually restarted.
unless-stopped As above, but if the container is stopped, it is not restarted even after the daemon restarts

Show the restart policy

My first step was to see what the policy was for my pihole container (which didn't start automatically). For this, I used the inspect command:

$ docker inspect pihole
...
        "HostConfig: {
...
            "RestartPolicy": {
                "Name": "",
                "MaximumRetryCount": 0
            }
...
        }
...

Sure enough, my container showed no explicit policy, so it was the default of no restart.

I did the same for all my containers with a variation of a command found on SO here:

$ docker inspect -f "{{ .HostConfig.RestartPolicy.Name }} - {{.Name}}, {{.Id}}" $(docker ps -q)
no - /wiki, ba7314beea63...
unless-stopped - /pihole, fd2f83b0eb92...
no - /unifi, 11fef420ad5c...

Update the restart policy

You can always start a container with a policy:

$ docker run -d --restart unless-stopped wiki

You can update the policy of an existing container with:

$ docker update --restart unless-stopped pihole

You can batch-update for all containers (the docker ps command above becomes useful here):

docker update --restart unless-stopped $(docker ps -q)

After the update, I got:

$ docker inspect -f "{{ .HostConfig.RestartPolicy.Name }} - {{.Name}}, {{.Id}}" $(docker ps -q)
unless-stopped - /wiki, ba7314beea63...
unless-stopped - /pihole, fd2f83b0eb92...
unless-stopped - /unifi, 11fef420ad5c...

Much better.

HTH,