Docker Cheat Sheet

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Docker Cheat Sheet

Installation Container registry commands


Command Description
Install Docker Engine (https://docs.docker.com/engine/install/, Linux only) or Docker Desktop
(https://docs.docker.com/desktop/, Linux, macOS and Windows). docker login Login to Docker Hub
docker login <server> Login to another container registry
Container commands docker logout Logout of Docker Hub
Command Description
docker logout <server> Logout of another container registry
docker run <image> Create and run a new container
docker push <image> Upload an image to a registry
docker run -p 8080:80 <image> Publish container port 80 to host port 8080
docker pull <image> Download an image from a registry
docker run -d <image> Run a container in the background
docker search <image> Search Docker Hub for images
docker run -v <host>:<container>
Mount a host directory to a container
<image>
System commands
docker ps List currently running containers
Command Description
docker ps --all List all containers (running or stopped)
docker system df Show Docker disk usage
docker logs <container_name> Fetch the logs of a container
docker system prune Remove unused data
docker logs -f <container_name> Fetch and follow the logs of a container
docker system prune -a Remove all unused data
docker stop <container_name> Stop a running container
docker start <container_name> Start a stopped container
Docker Compose
docker rm <container_name> Remove a container
Command Description
docker compose up Create and start containers
Executing commands in a container
docker compose up -d Create and start containers in background
Command Description
docker compose up --build Rebuild images before starting containers
docker exec <container_name> <command> Execute a command in a running container
docker compose stop Stop services
docker exec -it <container_name> bash Open a shell in a running container
docker compose down Stop and remove containers and networks
docker compose ps List running containers
Image commands
docker compose logs View the logs of all containers
Command Description
docker compose logs <service> View the logs of a specific service
Build a new image from the Dockerfile in the current
docker build -t <image> .
directory and tag it docker compose logs -f View and follow the logs
docker images List local images docker compose pull Pull the latest images
docker rmi <image> Remove an image docker compose build Build or rebuild services
docker compose build --pull Pull latest images before building

© Nic Wortel, Software Consultant & Trainer - Last updated on March 8, 2024 - Find more cheat sheets at https://nicwortel.nl/cheat-sheets
Dockerfile instructions Docker Compose file reference
Instruction Description Key Description
FROM <image> Set the base image name Set the name of the project
FROM <image> AS <name> Set the base image and name the build stage services A list of services defined in the file
RUN <command> Execute a command as part of the build process services.<name>.image Set the image to use or build
RUN ["exec", "param1", "param2"] Execute a command as part of the build process services.<name>.build Build context and options
CMD ["exec", "param1", "param2"] Execute a command when the container starts services.<name>.build.context Build context (default is the current directory)
ENTRYPOINT ["exec", "param1"] Configure the container to run as an executable services.<name>.build.dockerfile Dockerfile to use (default is Dockerfile)
ENV <key>=<value> Set an environment variable services.<name>.build.target Build stage to use
EXPOSE <port> Expose a port services.<name>.build.args Build arguments
COPY <src> <dest> Copy files from source to destination services.<name>.command Override the default command for the container

COPY --from=<name> <src> <dest> Copy files from a build stage to destination services.<name>.entrypoint Override the default entrypoint for the container

WORKDIR <path> Set the working directory services.<name>.volumes Mount volumes in the container

VOLUME <path> Create a mount point services.<name>.ports Publish container ports to the host

USER <user> Set the user services.<name>.environment Set environment variables in the container

ARG <name> Define a build argument services.<name>.restart Restart policy (no/always/on-failure/unless-stopped)

ARG <name>=<default> Define a build argument with a default value services.<name>.scale Set the number of containers to run

LABEL <key>=<value> Set a metadata label services.<name>.networks List of networks to connect the container to

HEALTHCHECK <command> Set a healthcheck command services.<name>.depends_on List of services to start before this service
services.<name>.labels Set metadata labels for the container
See https://docs.docker.com/engine/reference/builder/ for the full Dockerfile reference.
networks A list of networks defined in the file

Example docker-compose.yaml networks.<name>.driver Set the network driver


networks.<name>.external Don't create the network, use an existing one
services:
service1: volumes A list of volumes defined in the file
image: <image>
volumes.<name>.name Set the name of the volume
build: .
volumes: volumes.<name>.driver Set the volume driver
- .:/code:ro
configs A list of configs defined in the file
ports:
- "8000:80" secrets A list of secrets defined in the file
environment:
KEY: value See https://docs.docker.com/compose/compose-file/ for the full Docker Compose file reference.

© Nic Wortel, Software Consultant & Trainer - Last updated on March 8, 2024 - Find more cheat sheets at https://nicwortel.nl/cheat-sheets

You might also like