← Back to DevOpsToolbox

Docker

Docker Commands Cheat Sheet

A practical Docker CLI reference for containers, images, Dockerfiles, volumes, networks, Compose, registries, BuildKit, troubleshooting and production container workflows.

Docker Command Syntax

docker [command] [options] [arguments]

Docker CLI commands are used to build images, create and manage containers, configure networks and volumes, run Compose applications, publish images and troubleshoot containerized workloads.

Quick Docker Command Reference

docker ps
docker ps -a
docker images
docker pull nginx
docker run -d -p 8080:80 nginx
docker logs <container>
docker exec -it <container> /bin/bash
docker inspect <container>
docker build -t myapp .
docker network ls
docker volume ls
docker compose up -d
docker compose config
docker system df
docker buildx ls
docker stats

Docker Command Categories

Jump directly to the Docker commands you need.

161 commands across 20 categories

1. Docker Information

7 commands
docker --version

Displays the installed Docker CLI version.

docker version

Displays detailed Docker client and server version information.

docker info

Displays detailed information about the Docker daemon, containers, images, storage and runtime.

docker help

Displays help for Docker commands and available options.

docker system info

Displays Docker system information using the system command group.

docker context ls

Lists Docker contexts available to the current CLI.

docker context show

Displays the currently selected Docker context.

2. Docker Images

11 commands
docker images

Lists Docker images stored locally.

docker image ls

Lists locally available Docker images.

docker pull nginx

Downloads the nginx image from a container registry.

docker pull nginx:latest

Downloads a specific image tag from a container registry.

docker image inspect <image>

Displays detailed metadata and configuration for an image.

docker image history <image>

Shows the layers that make up a Docker image.

docker tag <image> <repository>:<tag>

Creates another tag referencing an existing Docker image.

docker rmi <image>

Removes a Docker image.

docker rmi -f <image>

Force removes an image when normal removal is blocked.

docker image prune

Removes dangling images that are no longer referenced.

docker image prune -a

Removes unused images that are not referenced by containers.

3. Run Containers

12 commands
docker run nginx

Creates and starts a container from the nginx image.

docker run -d nginx

Runs an nginx container in detached mode.

docker run -it ubuntu bash

Starts an interactive Ubuntu container with a Bash shell.

docker run --name web nginx

Creates a container with a custom name.

docker run -d -p 8080:80 nginx

Runs nginx and maps host port 8080 to container port 80.

docker run -d --restart unless-stopped nginx

Runs a container with a restart policy that keeps it running unless explicitly stopped.

docker run --rm nginx

Automatically removes the container after it exits.

docker run -e APP_ENV=production nginx

Passes an environment variable into the container.

docker run -v /host/path:/container/path nginx

Mounts a host directory or volume into the container.

docker run --env-file .env nginx

Loads environment variables from a file.

docker run --network mynetwork nginx

Starts a container attached to a specific Docker network.

docker run --memory 512m --cpus 1 nginx

Limits container memory and CPU resources.

4. Container Management

12 commands
docker ps

Lists currently running containers.

docker ps -a

Lists all containers, including stopped containers.

docker ps --format '{{.Names}}'

Displays container names using a custom output format.

docker start <container>

Starts an existing stopped container.

docker stop <container>

Gracefully stops a running container.

docker restart <container>

Stops and starts a container again.

docker pause <container>

Pauses all processes inside a container.

docker unpause <container>

Resumes processes inside a paused container.

docker kill <container>

Immediately stops a running container.

docker rm <container>

Removes a stopped container.

docker rm -f <container>

Force removes a running or stopped container.

docker rename <old> <new>

Renames an existing container.

5. Container Logs and Inspection

10 commands
docker logs <container>

Displays logs generated by a container.

docker logs -f <container>

Follows container logs in real time.

docker logs --tail 100 <container>

Displays only the latest 100 lines of container logs.

docker logs --since 10m <container>

Displays logs generated during the specified time period.

docker inspect <container>

Displays detailed container configuration and runtime information.

docker inspect -f '{{.State.Status}}' <container>

Displays the current state of a container.

docker stats

Continuously monitors CPU, memory, network and block I/O usage.

docker stats <container>

Monitors resource usage for a specific container.

docker top <container>

Displays processes running inside a container.

docker port <container>

Displays port mappings configured for a container.

6. Execute Commands Inside Containers

6 commands
docker exec -it <container> /bin/bash

Opens an interactive Bash shell inside a running container.

docker exec -it <container> /bin/sh

Opens a shell when Bash is not available.

docker exec <container> env

Displays environment variables inside the container.

docker exec <container> ls -la

Runs a command inside the container without opening a shell.

docker exec -it <container> sh

Starts an interactive shell using sh.

docker exec -u root -it <container> sh

Opens a shell inside the container as root.

7. Copy and Transfer Files

4 commands
docker cp <container>:/path/file.txt ./file.txt

Copies a file from a container to the host.

docker cp ./file.txt <container>:/path/file.txt

Copies a file from the host into a container.

docker cp ./directory <container>:/app/

Copies a directory from the host into a container.

docker cp <container>:/app/logs ./logs

Copies application logs from a container to the host.

8. Build Docker Images

8 commands
docker build -t myapp .

Builds a Docker image from the Dockerfile in the current directory.

docker build -t myapp:1.0 .

Builds an image with a specific repository name and tag.

docker build -f Dockerfile.prod -t myapp:prod .

Builds an image using a specific Dockerfile.

docker build --no-cache -t myapp .

Builds an image without using cached layers.

docker build --pull -t myapp .

Attempts to pull newer versions of base images before building.

docker build --build-arg APP_ENV=production -t myapp .

Passes a build argument to the Docker build process.

docker build --target production -t myapp:prod .

Builds a specific target from a multi-stage Dockerfile.

docker build --progress=plain -t myapp .

Displays detailed build output useful for troubleshooting.

9. Dockerfile Commands

13 commands
FROM node:22

Defines the base image for a Docker image.

WORKDIR /app

Sets the working directory inside the image.

COPY . .

Copies files from the build context into the image.

ADD archive.tar.gz /app/

Adds files or supported archives to the image.

RUN npm install

Executes a command while building the image.

ENV NODE_ENV=production

Defines an environment variable in the image.

ARG APP_VERSION=1.0

Defines a build-time argument.

EXPOSE 3000

Documents the port the application is intended to use.

CMD ["npm", "start"]

Defines the default command executed when a container starts.

ENTRYPOINT ["node"]

Defines the main executable for the container.

USER node

Sets the default user used when running the container.

HEALTHCHECK CMD curl --fail http://localhost:3000/ || exit 1

Defines a container health check.

VOLUME ["/data"]

Declares a mount point intended for persistent data.

10. Docker Networks

8 commands
docker network ls

Lists Docker networks.

docker network inspect <network>

Displays detailed information about a Docker network.

docker network create mynetwork

Creates a user-defined Docker network.

docker network create --driver bridge mynetwork

Creates a bridge network explicitly.

docker network connect mynetwork <container>

Connects an existing container to a network.

docker network disconnect mynetwork <container>

Disconnects a container from a network.

docker network rm <network>

Removes a Docker network.

docker network prune

Removes unused Docker networks.

11. Docker Volumes

7 commands
docker volume ls

Lists Docker volumes.

docker volume create myvolume

Creates a named Docker volume.

docker volume inspect myvolume

Displays detailed information about a Docker volume.

docker run -v myvolume:/data nginx

Mounts a named volume at /data inside the container.

docker run --mount source=myvolume,target=/data nginx

Mounts a volume using the explicit --mount syntax.

docker volume rm myvolume

Removes a Docker volume.

docker volume prune

Removes unused Docker volumes.

12. Docker Compose

13 commands
docker compose up

Creates and starts services defined in compose.yaml.

docker compose up -d

Starts Compose services in detached mode.

docker compose down

Stops and removes Compose services, networks and containers created by the project.

docker compose ps

Lists containers managed by the Compose project.

docker compose logs

Displays logs from Compose services.

docker compose logs -f

Follows Compose service logs in real time.

docker compose build

Builds images defined in the Compose configuration.

docker compose pull

Pulls images required by the Compose project.

docker compose exec <service> sh

Executes an interactive shell inside a Compose service container.

docker compose restart <service>

Restarts a specific Compose service.

docker compose stop <service>

Stops a specific Compose service.

docker compose config

Validates and renders the final Compose configuration.

docker compose up -d --build

Rebuilds required images and starts Compose services.

13. Docker Registry

6 commands
docker login

Authenticates the Docker CLI with a container registry.

docker logout

Logs out of a container registry.

docker tag myapp username/myapp:latest

Tags an image for a registry repository.

docker push username/myapp:latest

Pushes a tagged image to a container registry.

docker pull username/myapp:latest

Downloads an image from a container registry.

docker image inspect username/myapp:latest

Inspects metadata for a registry image after pulling it.

14. Save, Load, Export and Import

4 commands
docker save -o myapp.tar myapp:latest

Exports a Docker image and its layers to a tar archive.

docker load -i myapp.tar

Loads Docker images from a tar archive.

docker export -o container.tar <container>

Exports a container filesystem to a tar archive.

docker import container.tar myimage:latest

Creates a Docker image from a filesystem tar archive.

15. Container Resources and Health

5 commands
docker stats <container>

Monitors live CPU, memory, network and block I/O usage.

docker inspect <container>

Inspects resource limits, mounts, networking and runtime configuration.

docker update --memory 512m <container>

Updates the memory limit of an existing container.

docker update --cpus 1 <container>

Updates the CPU limit of an existing container.

docker inspect --format '{{.State.Health.Status}}' <container>

Displays the health status of a container with a health check.

16. Docker System Management

9 commands
docker system df

Shows Docker disk usage by images, containers, volumes and build cache.

docker system prune

Removes unused Docker resources.

docker system prune -a

Removes unused resources including unused images.

docker container prune

Removes stopped containers.

docker image prune

Removes dangling images.

docker network prune

Removes unused Docker networks.

docker volume prune

Removes unused Docker volumes.

docker builder prune

Removes unused build cache.

docker builder prune -a

Removes all unused build cache.

17. Docker Buildx and BuildKit

7 commands
docker buildx version

Displays the installed Docker Buildx version.

docker buildx ls

Lists available Buildx builders.

docker buildx create --name mybuilder --use

Creates a Buildx builder and switches the CLI to it.

docker buildx inspect

Displays information about the active Buildx builder.

docker buildx build -t myapp:latest .

Builds an image using Buildx.

docker buildx build --platform linux/amd64,linux/arm64 -t myapp:latest --push .

Builds and pushes a multi-platform image.

docker buildx prune

Removes unused Buildx build cache.

18. Docker Contexts

5 commands
docker context ls

Lists available Docker contexts.

docker context show

Shows the active Docker context.

docker context inspect <context>

Displays configuration details for a Docker context.

docker context use <context>

Switches the Docker CLI to another context.

docker context create <context> --docker host=ssh://user@host

Creates a Docker context that connects to a remote Docker host over SSH.

19. Docker Troubleshooting

8 commands
docker ps -a

Check whether a container exited or failed to start.

docker logs <container>

Check application or startup errors in container logs.

docker inspect <container>

Inspect container configuration, networking, mounts and runtime state.

docker stats

Check whether containers are consuming excessive CPU or memory.

docker network inspect <network>

Investigate container network configuration and connected containers.

docker system df

Check whether Docker is consuming excessive disk space.

docker events

Streams Docker daemon events useful for troubleshooting container lifecycle problems.

docker inspect --format '{{.State.ExitCode}}' <container>

Checks the exit code of a container.

20. Production Docker Workflow

6 commands
docker build -t myapp:1.0 .

Builds a versioned application image.

docker image inspect myapp:1.0

Inspects the built image before deployment.

docker run -d --name myapp -p 8080:3000 --restart unless-stopped myapp:1.0

Runs a production-style container with port mapping and restart policy.

docker logs --tail 100 myapp

Checks the latest application logs after deployment.

docker stats myapp

Monitors the running application container.

docker exec -it myapp sh

Opens a shell for application troubleshooting.

Docker Troubleshooting Workflow

# Check Docker installation
docker --version

# Check Docker daemon
docker info

# Check running containers
docker ps

# Check all containers
docker ps -a

# Inspect container details
docker inspect <container>

# Check container logs
docker logs <container>

# Follow logs
docker logs -f <container>

# Check processes
docker top <container>

# Check resource usage
docker stats

# Check port mappings
docker port <container>

# Check networks
docker network ls

# Inspect a network
docker network inspect <network>

# Check Docker disk usage
docker system df

# Check build cache
docker builder prune

# Check image layers
docker image history <image>

# Check image metadata
docker image inspect <image>

# Check Docker events
docker events

Start withdocker ps -ato determine the container state, then checkdocker logsanddocker inspect. If the application is running but unreachable, verify port mappings and Docker networks. For performance problems, inspect CPU and memory usage withdocker stats.

DevOpsToolbox Tip

When a container is not working, start withdocker ps -a, then checkdocker logsanddocker inspect. For production deployments, use versioned image tags, health checks, restart policies, resource limits and a controlled cleanup strategy.