Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Docker CLI Cheatsheet

This is just a cheatsheet of commands you may wish to execute to manage your docker images/containers.

List Images

For a version that removes the ID and Command columns so it is likely to fit in your terminal window:

docker ps -a --format="table {{.Names}}\t{{.Image}}\t{{.Ports}}\t{{.Status}}"

Docker - Get Latest Built Image ID

docker images -q | sed -n 2p  

Remove stopped Containers

docker rm $(docker ps -a -q)

You may get the message Error: failed to remove one or more containers but that just occurs when you have running containers (running containers get passed to the remove command but will faile to get removed.

Stop And Remove All Containers

docker stop $(docker ps -aq)  
docker rm $(docker ps -aq)  

Delete "Dangling" Images

docker image prune

Alternatively...

docker rmi $(docker images --quiet --filter "dangling=true")  

shorthand:

docker rmi $(docker images -q --f "dangling=true")  docker-compose exec web php artisan october:up

This could be unsafe if you run it whilst docker images are being pulled.

Delete All Images

docker prune images --all

Alternatively...

docker rmi $(docker images -q)  

Alternatively....

IMAGES=`docker images | tail -n +2 | tr -s ' ' | cut -d ' ' -f3`
echo "${IMAGES}" | xargs -I %s docker rmi %s

You won't be able to remove images that running containers are using.

Get the Storage Driver Being Used

Different distributions will use different storage drivers. For example, CentOS and Amazon linux will use the devicemapper driver, whereas Ubuntu will use aufs. To find out which storage driver you are using, run:

docker info | grep "Storage Driver"

Go here to get a list of the storage drivers you can use.

Copy Files From Container To Host

Sometimes you may need to copy files from a container to the host, such as the logs. For this use the docker cp command:

docker cp $CONTAINER_ID:/path/to/files/in/container /path/on/host

Push To Registry

If you just built a container that doesn't contain a registry name, you can push it to your own docker registry by tagging it first and then pushing it.

E.g. to push an image you just created called "bob" to your registry at "registry.mydomain.com:5000" you would use:

docker tag bob registry.mydomain.com:5000/bob
docker push registry.mydomain.com:5000/bob

You may need to use docker login registry.mydomain.com:5000 first for authentication.

Docker Compose

Run A Command (Exec)

The following can be used to run a command against a service (not the name of the container).

docker-compose exec $SERVICE_NAME /command/to/run param1

e.g.

docker-compose exec web php artisan october:up

Start A Specific Service

Sometimes one needs to only start a specific service, or manually start the services in order to start them in the correct order. E.g. it's nice to start your database service first so it's ready when your application starts.

docker-compose up $SERVICE_NAME

Use A Specific Docker-compose File

docker-compose will use a file called docker-compose.yml by default, but if you want to use a different file, you can specify its path.

docker-compose -f /path/to/file.yml up

I use this when I have a separate docker-compose file for dev, which will use a volume that production doesn't use.

Last updated: 24th September 2020
First published: 16th August 2018