Docker Cheat Sheet & Quick Reference

Compose

$ docker compose up

Create and run containers in the foreground

$ docker compose up -d

Create and run containers in the background

$ docker compose ps

List running containers

$ docker compose ps --all
$ docker compose ps -a

List all containers

$ docker compose stop

Stop containers

$ docker compose down

Stop and remove containers, networks created by up

$ docker compose pull

Pull images defined in a Compose file

$ docker compose create

Create a container using Docker Compose

$ docker compose up --build
$ docker compose up -d --build

Build and start containers using Docker Compose

$ docker compose up -d --scale service_name=num

Scale services defined in a Compose file

$ docker compose logs
$ docker compose logs service_name

View the logs of services defined in a Compose file

$ docker compose exec service_name command

Execute a command in a running container defined in a Compose file

$ docker compose run service_name command

Run a one-off command in a service container defined in a Compose file

$ docker compose rm

Remove stopped containers created by up

Container

$ docker ps
$ docker container list
$ docker container ls

List running containers

$ docker ps --all
$ docker ps -a
$ docker container list --all
$ docker container ls --all

List all containers

$ docker run my-image:1.0
$ docker container run my-image:1.0

Run a container from my-image:1.0

$ docker run --name www my-image:1.0

Run a container from my-image:1.0 with the name www

$ docker run --name www -p 80:3000 my-image:1.0

Run a container from my-image:1.0 exposing internal port 3000 to external port 80

$ docker stop www
$ docker container stop www

Stop a container by the name www

$ docker stop www1 www2
$ docker container stop www1 www2

Stop containers by the name www1 www2 ...

$ docker stop $(docker ps -a | grep "example" | awk '{print $1}')

Stop all the containers that match a keyword example

$ docker kill www
$ docker container kill www

Kill a container by the name www

$ docker kill www1 www2
$ docker container kill www1 www2

Kill containers by the name www1 www2 ...

$ docker rm www1 www2
$ docker container rm www1 www2

Remove containers by the name www1 www2 ...

$ docker rm $(docker ps -a | grep "example" | awk '{print $1}')

Remove all the containers that match a keyword example

$ docker exec www id
$ docker container exec www id

Runs id in www container

$ docker exec -it ubuntu bash
$ docker container exec -it ubuntu bash

Run an interactive bash shell on the container

$ docker run -d my-image:1.0
$ docker container run -d my-image:1.0

Run a container in the background from my-image:1.0

$ docker attach www
$ docker container attach www

Attach to a running container named www

$ docker inspect www
$ docker container inspect www

Inspect a container named www

$ docker logs www
$ docker container logs www

List logs of a container named www

$ docker start www
$ docker container start www

Start a stopped container named www

$ docker restart www
$ docker container restart www

Restart a running container named www

$ docker pause www
$ docker container pause www

Pause a running container named www

$ docker unpause www
$ docker container unpause www

Unpause a paused container named www

$ docker rename www web
$ docker container rename www web

Rename a container named www to web

Image

$ docker build -t my-image:1.0 .

Build a image with the name my-image and tag 1.0

$ docker image ls

List local images

$ docker image rm my-image:1.0

Delete local image by the name:tag my-image:1.0

$ docker build -t my-image .

Build an image in the current directory with the name my-image

$ docker pull example
$ docker image pull example

Pull image example from a registry

$ docker push myrepo/my-image:2.0
$ docker image push myrepo/my-image:2.0

Push image my-image to repository myrepo under a registry

Network

$ docker network ls

List networks

$ docker network create my-network

Create a network with the name my-network

$ docker network rm my-network

Remove a network with the name my-network

$ docker network inspect my-network

Inspect a network with the name my-network

System

$ docker system prune

Remove all unused containers, networks, images (both dangling and unreferenced)

$ docker system prune --volumes

Remove all unused containers, networks, images (both dangling and unreferenced), and volumes.

$ docker system prune --all --volumes

Remove all unused containers, networks, all unused images, and volumes.

Volume

$ docker volume ls

List volumes

$ docker volume create hello

Create a volume

$ docker volume prune

Remove all unused local volumes

$ docker run --rm --volume my_volume:/mount_bkp --volume $(pwd):/backup ubuntu tar cvf /backup/my_backup.tar /mount_bkp

Backup my_volume to my_backup.tar

$ docker run --rm --volume my_volume:/mount_bkp --volume $(pwd):/backup ubuntu tar xvf /backup/my_backup.tar -C /mount_bkp --strip 1

Restore my_backup.tar to my_volume