Skip to content

Docker Cheat Sheet

Container Management

CommandDescription
docker run [options] IMAGECreate and start a new container
docker start CONTAINERStart a stopped container
docker stop CONTAINERStop a running container
docker restart CONTAINERRestart a container
docker pause CONTAINERPause a running container
docker unpause CONTAINERUnpause a paused container
docker rm CONTAINERRemove a stopped container
docker exec [options] CONTAINER COMMANDRun a command in a running container

docker run options

  • -d --detach - Run container in background and print container ID
  • -e --env - Set environment variables
  • -i --interactive - Keep STDIN open even if not attached
  • -p --publish - Publish a container's port(s) to the host (format: -p HOST:CONTAINER)
  • -t --tty - Allocate a pseudo-TTY (teletype)
  • -v --volume - Bind mount a volume (format: -v HOST:CONTAINER)

Image Management

CommandDescription
docker build [options] PATHBuild an image from a Dockerfile
docker pull IMAGEPull an image from a registry
docker push IMAGEPush an image to a registry
docker imagesList local images
docker rmi IMAGERemove an image
docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]Create a tag for an image

Container Inspection

CommandDescription
docker psList running containers
docker ps -aList all containers (including stopped)
docker logs CONTAINERView container logs
docker inspect CONTAINERDisplay detailed information about a container
docker top CONTAINERDisplay running processes of a container
docker stats CONTAINERDisplay a live stream of container resource usage statistics

Image Inspection

CommandDescription
docker history IMAGEShow the history of an image
docker inspect IMAGEDisplay detailed information about an image

Network Management

CommandDescription
docker network create NETWORKCreate a network
docker network lsList networks
docker network inspect NETWORKDisplay detailed information about a network
docker network connect NETWORK CONTAINERConnect a container to a network
docker network disconnect NETWORK CONTAINERDisconnect a container from a network

Volume Management

CommandDescription
docker volume create VOLUMECreate a volume
docker volume lsList volumes
docker volume inspect VOLUMEDisplay detailed information about a volume
docker volume rm VOLUMERemove a volume

System Management

CommandDescription
docker infoDisplay system-wide information
docker versionShow the Docker version information
docker system pruneRemove unused data
docker system dfShow docker disk usage

Docker Compose

CommandDescription
docker-compose upCreate and start containers
docker-compose downStop and remove containers, networks, images, and volumes
docker-compose psList containers
docker-compose logsView output from containers
docker-compose buildBuild or rebuild services

Docker Hub

CommandDescription
docker loginLog in to a Docker registry
docker logoutLog out from a Docker registry
docker search TERMSearch Docker Hub for images

Dockerfile

Base Image & Environment Setup

InstructionDescription
FROM <image>:<tag>Set the base image for subsequent instructions
ARG <name>[=<default>]Define a build-time variable
ENV <key>=<value>Set an environment variable

Working Directory

InstructionDescription
WORKDIR <path>Set the working directory for subsequent instructions

File Manipulation

InstructionDescription
COPY <src> <dest>Copy files or directories from the build context to the container
ADD <src> <dest>Copy files or directories from the build context to the container (supports URLs and unpacking archives)

Running Commands

InstructionDescription
RUN <command>Execute a command in a new layer on top of the current image and commit the results

Exposing Ports

InstructionDescription
EXPOSE <port>Expose a port to the network

Volumes

InstructionDescription
VOLUME ["/path"]Create a mount point with the specified name and mark it as holding externally mounted volumes from native host or other containers

User

InstructionDescription
USER <user>Set the user name or UID to use when running the image

Labels & Metadata

InstructionDescription
LABEL <key>=<value>Add metadata to an image

Health Checks

InstructionDescription
HEALTHCHECK [options] CMD commandSet a command to run to check the health of a container

Entrypoint & Runtime Configuration

InstructionDescription
ENTRYPOINT ["executable", "param1", "param2"]Set the primary command to be run when the container starts
CMD ["executable", "param1", "param2"]Provide defaults for an executing container