Docker cheatsheet

This cheat sheet features the most important and commonly used Docker commands for easy reference.

Table of contents

  1. References
  2. Images
    1. Build an image
    2. Build with a specific Dockerfile
    3. List images
    4. Remove an image
    5. Remove all images
    6. Remove all untagged images
    7. Pull an image
    8. Push an image
    9. Tag an image
  3. Containers
    1. Run a container
    2. Run a container with environment variables
    3. Run a container with a volume mount
    4. List running containers
    5. List all containers
    6. Stop a container
    7. Stop all containers
    8. Start a container
    9. Restart a container
    10. Remove a container
    11. Remove all containers
    12. Stop and remove all containers and images
    13. Kill all running containers
    14. Connect to a running container
    15. View container logs
    16. Follow container logs
    17. Copy files from container to host
    18. Copy files from host to container
  4. Inspect
    1. Get IP address of a running container
    2. See all space Docker takes up
    3. Inspect a container
  5. Volumes
    1. Create a volume
    2. List volumes
    3. Remove a volume
    4. Remove all unused volumes
  6. Networks
    1. Create a network
    2. List networks
    3. Connect a container to a network
    4. Remove all unused networks
  7. Cleanup
    1. Remove all unused objects (containers, networks, images, cache)
    2. Remove all unused objects including volumes
    3. Remove all unused images (not just dangling)
  8. Docker Compose
    1. Start services
    2. Stop services
    3. Stop services and remove volumes
    4. View logs
    5. Rebuild and start services
    6. List running services

References


Images

Build an image

docker build -t <image_name>:<tag> .

Build with a specific Dockerfile

docker build -f <Dockerfile> -t <image_name>:<tag> .

List images

docker images

Remove an image

docker rmi <image_id>

Remove all images

docker rmi $(docker images -q)

Remove all untagged images

docker rmi $(docker images | grep '^<none>' | awk '{print $3}')

Pull an image

docker pull <image_name>:<tag>

Push an image

docker push <image_name>:<tag>

Tag an image

docker tag <image_id> <image_name>:<tag>

Containers

Run a container

docker run -d --name <container_name> -p <host_port>:<container_port> <image_name>:<tag>

Run a container with environment variables

docker run -d --name <container_name> -e KEY=VALUE <image_name>:<tag>

Run a container with a volume mount

docker run -d --name <container_name> -v <host_path>:<container_path> <image_name>:<tag>

List running containers

docker ps

List all containers

docker ps -a

Stop a container

docker stop <container_id>

Stop all containers

NOTE: This will stop ALL your containers.

docker stop $(docker ps -aq)

Start a container

docker start <container_id>

Restart a container

docker restart <container_id>

Remove a container

docker rm <container_id>

Remove all containers

NOTE: This will remove ALL your containers.

docker rm $(docker ps -aq)

Stop and remove all containers and images

NOTE: This will remove ALL your images and containers.

docker stop $(docker ps -aq) && docker rm $(docker ps -aq) && docker rmi $(docker images -q)

Kill all running containers

docker kill $(docker ps -q)

Connect to a running container

docker exec -it <container_id> /bin/bash

View container logs

docker logs <container_id>

Follow container logs

docker logs -f <container_id>

Copy files from container to host

docker cp <container_id>:<container_path> <host_path>

Copy files from host to container

docker cp <host_path> <container_id>:<container_path>

Inspect

Get IP address of a running container

docker inspect -f '' <container_id>

See all space Docker takes up

docker system df

Inspect a container

docker inspect <container_id>

Volumes

Create a volume

docker volume create <volume_name>

List volumes

docker volume ls

Remove a volume

docker volume rm <volume_name>

Remove all unused volumes

docker volume prune

Networks

Create a network

docker network create <network_name>

List networks

docker network ls

Connect a container to a network

docker network connect <network_name> <container_id>

Remove all unused networks

docker network prune

Cleanup

Remove all unused objects (containers, networks, images, cache)

docker system prune

Remove all unused objects including volumes

docker system prune --volumes

Remove all unused images (not just dangling)

docker image prune -a

Docker Compose

Start services

docker compose up -d

Stop services

docker compose down

Stop services and remove volumes

docker compose down -v

View logs

docker compose logs -f

Rebuild and start services

docker compose up -d --build

List running services

docker compose ps