Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Docker CLI Cheatsheet

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

Docker CLI Commands / Examples

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


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


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


docker rmi $(docker images -q)  


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 "" you would use:

docker tag bob
docker push

You may need to use docker login first for authentication.

View Container Logs / Output

You can view the logs by running:


That will just output the logs it has so far. If you want to see the logs and watch/tail them for updates, then do the following:

docker logs --follow $CONTAINER_ID_OR_NAME


The following will result in Docker logging to a JSON file, with a max size of 10 MiB, and only keeping up to 3 of them. Thus your logs will never exceed 30 MiB.

docker run \
  --log-driver json-file \
  --log-opt max-size=10m \
  --log-opt max-file=3 \

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


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: 18th February 2022
First published: 16th August 2018