Docker Cheat Sheet
Table of Contents
-
- Installation
-
- Container Commands
-
- Executing Commands in a Container
-
- Image Commands
-
- Container Registry Commands
-
- System Commands
-
- Docker Compose
-
- Dockerfile Instructions
-
- Docker Compose File Reference
Container Commands
| Example | |
|---|---|
| Create and run a new container docker run |
docker run nginx |
| Publish container port 80 to host port 8080 docker run -p 8080:80 |
docker run -p 8080:80 nginx |
| Run a container in the background docker run -d |
docker run -d nginx |
| Mount a host directory to a container docker run -v |
docker run -v /home/user/app:/usr/share/nginx/html nginx |
| List currently running containers docker ps |
docker ps |
| List all containers (running or stopped) docker ps --all |
docker ps --all |
| Fetch the logs of a container docker logs |
docker logs my_nginx |
| Fetch and follow the logs of a container docker logs -f |
docker logs -f my_nginx |
| Stop a running container docker stop |
docker stop my_nginx |
| Start a stopped container docker start |
docker start my_nginx |
| Remove a container docker rm |
docker rm my_nginx |
Executing Commands in a Container
| Example | |
|---|---|
| Execute a command in a running container docker exec |
docker exec my_nginx ls /usr/share/nginx/html |
| Open a shell in a running container docker exec -it |
docker exec -it my_nginx bash |
Image Commands
| Example | |
|---|---|
| Build a new image from the Dockerfile in the current directory and tag it docker build -t |
docker build -t myapp:1.0 . |
| List local images docker images |
docker images |
| Remove an image docker rmi |
docker rmi myapp:1.0 |
Container Registry Commands
| Example | |
|---|---|
| Login to Docker Hub docker login |
docker login |
| Login to another container registry docker login |
docker login registry.example.com |
| Logout of Docker Hub docker logout |
docker logout |
| Logout of another container registry docker logout |
docker logout registry.example.com |
| Upload an image to a registry docker push |
docker push myusername/myapp:1.0 |
| Download an image from a registry docker pull |
docker pull nginx:latest |
| Search Docker Hub for images docker search |
docker search nginx |
System Commands
| Example | |
|---|---|
| Show Docker disk usage docker system df |
docker system df |
| Remove unused data docker system prune |
docker system prune |
| Remove all unused data docker system prune -a |
docker system prune -a |
Docker Compose
| Example | |
|---|---|
| Create and start containers docker compose up |
docker compose up |
| Create and start containers in background docker compose up -d |
docker compose up -d |
| Rebuild images before starting containers docker compose up --build |
docker compose up --build |
| Stop services docker compose stop |
docker compose stop |
| Stop and remove containers and networks docker compose down |
docker compose down |
| List running containers docker compose ps |
docker compose ps |
| View the logs of all containers docker compose logs |
docker compose logs |
| View the logs of a specific service docker compose logs |
docker compose logs web |
| View and follow the logs docker compose logs -f |
docker compose logs -f web |
| Pull the latest images docker compose pull |
docker compose pull |
| Build or rebuild services docker compose build |
docker compose build |
| Pull latest images before building docker compose build --pull |
docker compose build --pull |
Dockerfile Instructions
| Example | |
|---|---|
| Set the base image FROM |
FROM ubuntu:22.04 |
| Set the base image and name the build stage FROM |
FROM node:18 AS builder |
| Execute a command as part of the build process RUN |
RUN apt-get update &6 apt-get install -y curl |
| Execute a command as part of the build process RUN ["exec", “parami", "param2"] |
RUN ["npm", "“install"] |
| Execute a command when the container starts CMD ["exec", "parami", "param2"] |
CMD ["node", “app.js"] |
| Configure the container to run as an executable ENTRYPOINT ["exec", "parami"] |
ENTRYPOINT ["docker-entrypoint.sh"] |
| Set an environment variable ENV |
ENV NODE_ENV=production |
| Expose a port EXPOSE |
EXPOSE 3000 |
| Copy files from source to destination COPY |
COPY . /app |
| Copy files from a build stage to destination COPY --from= |
COPY --from=builder /app/dist /app |
| Set the working directory WORKDIR |
|
| Create a mount point VOLUME |
VOLUME /data |
| Set the user USER |
USER node |
| Define a build argument ARG |
ARG VERSION |
| Define a build argument with a default value ARG |
ARG VERSION=1.0 |
| Set a metadata label LABEL |
LABEL maintainer="youdexample.com" |
| Set a healthcheck command HEALTHCHECK |
HEALTHCHECK CMD curl -f http://localhost:3000/ |
Docker Compose File Reference
| Example | |
|---|---|
| Set the name of the project name |
name: myproject |
| A list of services defined in the file services |
services: web: image: nginx |
| Set the image to use or build services. |
image: nginx: latest |
| Build context and options services. |
build: . |
| Build context (default is the current directory) services. |
context: ./app |
| Dockerfile to use (default is Dockerfile) services. |
dockerfile: Dockerfile.dev |
| Build stage to use services. |
target: builder |
| Build arguments services. |
args: VERSION: 1.0 |
| Override the default command for the container services. |
command: npm start |
| Override the default entrypoint for the container services. |
entrypoint: /bin/sh |
| Mount volumes in the container services. |
volumes: - ./data:/data |
| Publish container ports to the host services. |
ports: - "8080:80" |
| Set environment variables in the container services. |
environment: - NODE_ENV=production |
| Restart policy (no/always/on-failure/unless-stopped) services. |
restart: always |
| Set the number of containers to run services. |
scale: 3 |
| List of networks to connect the container to services. |
networks: - frontend |
| List of services to start before this service services. |
depends_on: - db |
| Set metadata labels for the container services. |
labels: - com.example.description=My app |
| A list of networks defined in the file networks |
networks: - frontend |
| Set the network driver networks. |
driver: bridge |
| Don't create the network, use an existing one networks. |
external: true |
| A list of volumes defined in the file volumes |
volumes: db_data: |
| Set the name of the volume volumes. |
name: myvolume |
| Set the volume driver volumes. |
driver: local |
| A list of configs defined in the file configs |
configs: my_config: file: ./config.yml |
| A list of secrets defined in the file secrets |
secrets: db_password: file: ./db_password.txt |