Docker Cheat Sheet

Installation

Install Docker Engine (https://docs.docker.com/engine/install/, Linux only) or Docker Desktop
(https://docs.docker.com/desktop/, Linux, macOS and Windows).

Container commands

CommandDescription
docker run <image>Create and run a new container
docker run -p 8080:80 <image>Publish container port 80 to host port 8080
docker run -d <image>Run a container in the background
docker run -v <host>:<container> <image>Mount a host directory to a container
docker psList currently running containers
docker ps –allList all containers (running or stopped)
docker logs <container_name>Fetch the logs of a container
docker logs -f <container_name>Fetch and follow the logs of a container
docker stop <container_name>Stop a running container
docker start <container_name>Start a stopped container
docker rm <container_name>Remove a container

Executing commands in a container

CommandDescription
docker exec <container_name> <command>Execute a command in a running container
docker exec -it <container_name> bashOpen a shell in a running container

Image commands

CommandDescription
docker build -t <image> .Build a new image from the Dockerfile in the current directory and tag it
docker imagesList local images
docker rmi <image>Remove an image

Container registry commands

CommandDescription
docker loginLogin to Docker Hub
docker login <server>Login to another container registry
docker logoutLogout of Docker Hub
docker logout <server>Logout of another container registry
docker push <image>Upload an image to a registry
docker pull <image>Download an image from a registry
docker search <image>Search Docker Hub for images

System commands

CommandDescription
docker system dfShow Docker disk usage
docker system pruneRemove unused data
docker system prune -aRemove all unused data

Docker Compose

CommandDescription
docker compose upCreate and start containers
docker compose up -dCreate and start containers in background
docker compose up –buildRebuild images before starting containers
docker compose stopStop services
docker compose downStop and remove containers and networks
docker compose psList running containers
docker compose logsView the logs of all containers
docker compose logs <service>View the logs of a specific service
docker compose logs -fView and follow the logs
docker compose pullPull the latest images
docker compose buildBuild or rebuild services
docker compose build –pullPull latest images before building

Dockerfile instructions

InstructionDescription
FROM <image>Se the base image
FROM <image> AS <name>Set the base image and name the build stage
RUN <command>Execute a command as part of the build process
RUN [“exec”, “param1”, “param2”]Execute a command as part of the build process
CMD [“exec”, “param1”, “param2”]Execute a command when the container starts
ENTRYPOINT [“exec”, “param1”]Configure the container to run as an executable
ENV <key>=<value>Set an environment variable
EXPOSE <port>Expose a port
COPY <src> <dest>Copy files from source to destination
COPY –from=<name> <src> <dest>Copy files from a build stage to destination
WORKDIR <path>Set the working directory
VOLUME <path>Create a mount point
USER <user>Set the user
ARG <name>Define a build argument
ARG <name>=<value>Define a build argument with a default value
LABEL <key>=<value>Set a metadata label
HEALTHCHECK <command>Set a healthcheck command

See https://docs.docker.com/engine/reference/builder/ for the full Dockerfile reference.

Example compose.xml

Note: this file used to be called docker-compose.yaml, but now compose.yaml is preferred.

services:
service1:
image: <image>
build: .
volumes:
- .:/code:ro
ports:
- "8000:80"
environment:
KEY: value

Docker Compose file reference

KeyDescription
nameSet the name of the project
servicesA list of services defined in the file
services.<name>.imageSet the image to use or build
services.<name>.buildBuild context and options
services.<name>.build.contextBuild context (defaults is the current directory)
services.<name>.build.dockerfile Dockerfile to use (default is Dockerfile)
services.<name>.build.targetBuild stage to use
services.<name>.build.argsBuild arguments
services.<name>.commandOverride the default command for the container
services.<name>.entrypointOverride the default entrypoint for the container
services.<name>.volumesMount volumes in the container
services.<name>.portsPublish container ports to the host
services.<name>.environmentSet environment variables in the container
services.<name>.restartRestart policy (no/always/on-failture/unless-stopped)
services.<name>.scaleSet the number of containers to run
services.<name>.networksList of networks to connect the container to
services.<name>.depends_onList of services to start before this service
services.<name>.labelsSet metadata labels for the container
networksA list of networks defined in the file
networks.<name>.driverSet the network driver
networks.<name>.externalDon’t create the network, use an existing one
volumesA list of volumes defined in the file
volumes.<name>.nameSet the name of the volume
volumes.<name>.driverSet the volume driver
configsA list of configs defined in the file
secretsA list of secrets defined in the file

See https://docs.docker.com/compose/compose-file/ for the full Docker Compose file reference.

Data from: https://nth-root.nl/en/cheat-sheets (thank you).