Skip to content

Docker Cheat Sheet

Docker Commands

Pull Images

Get images from Docker Hub.

Use the pull command to download the image to your system.

💡 Try to use the images with the slim tag to drastically reduce your image size.

docker pull python:3.12.3-slim

Build Images

Build your image based on a Dockerfile and tag (name) it. TAG (not to be confused with -t) to give it a version. If no TAG is given, your image will be given the latest tag by default.

docker build -t MY-IMAGE-NAME:TAG .
docker build -t MY-IMAGE-NAME:TAG -f /PATH/TO/DOCKERFILE .
  • build builds the image based on the Dockerfile.
  • -t is the tag flag
  • :TAG (optional) to give it a version e.g. docker build -t hello-world:0.0.1 .
  • . is the PATH location of your Dockerfile. The . is used for the current directory and specifies the build context.
  • -f to specify the PATH location of your Dockerfile.

Run Containers

Start a container based on the image you created.

💡 You should use docker compose up instead of docker run as a more convenient way to run your containers. However, both methods are fine.

docker run MY-IMAGE-NAME:TAG

Other container commands:

docker stop MY-IMAGE-NAME:TAG
docker start MY-IMAGE-NAME:TAG
docker restart MY-IMAGE-NAME:TAG
docker rm MY-IMAGE-NAME:TAG

👇 These are optional (but useful) flags.

docker run \
  --name MY-CONTAINER-NAME \
  -d \
  -it \
  --rm \
  -p 8080:80 \
  -v $(pwd):/app \
  MY-IMAGE-NAME:TAG\
  tail -f /dev/null
  • run will start a container based on the image.
  • --name will name the container otherwise Docker will assign the container a random name.
  • -d is for detached mode. This will run your container in the background.
  • -it connect to the container's terminal once it starts.
  • --rm will remove the container once it stops.
  • -p HOST_PORT:CONTAINER_PORT publishes the ports e.g. -p 8080:8080
  • -v HOST_DIRECTORY/CONTAINER_DIRECTORY will bind mount volume a folder on your machine to the container.
  • Use pwd (print working directory) instead of writing out the full path manually.
  • Linux: docker run -v $(pwd):/path/in/container my-image
  • Windows PowerShell: docker run -v ${PWD}:/path/in/container my-image
  • tail -f /dev/null prevents the container from closing by searching for a non-existent file.

General Commands

Check status of containers:

docker ps -a

Go into the container's terminal:

docker exec -it NAME-OF-CONTAINER bash

List all images:

docker image ls

Delete images:

docker image rm IMAGE-NAME

Check container logs:

docker logs NAME-OF-CONTAINER

Check container config:

docker inspect NAME-OF-CONTAINER

Check the resource usage of your container:

docker stats NAME-OF-CONTAINER

Docker Compose

💡 Make sure your terminal is in the same directory as the docker-compose.yaml file.

Build and start up container(s):

docker compose up -d
  • -d is also detached mode to run it in the background (optional)

Stop container(s):

docker compose stop

Start container(s):

docker compose start

Restart container(s):

docker compose start

Remove container(s):

docker compose down