Skip to main content

Docker, Docker Compose, and Swarm Cheat Sheet

A list of useful commands for daily development workflows with Docker, Docker Compose, and Docker Swarm.


Docker Commands

Source: https://docs.docker.com/reference/cli/docker/

Manage Containers

docker run [OPTIONS] IMAGE [COMMAND]

Creates and runs a new container from an image.

docker run -d -p 80:80 --name my-nginx nginx
  • -d: Detached mode (runs in the background).
  • -p <host_port>:<container_port>: Publishes a container's port to the host.
  • -it: Creates an interactive terminal session.
  • --name <name>: Assigns a name to the container.
  • -v <host_path>:<container_path>: Mounts a host volume into the container.
  • --rm: Automatically removes the container when it exits.

docker ps [OPTIONS]

Lists running containers.

docker ps
  • -a: Shows all containers (running and stopped).
  • -q: Only displays container IDs.

docker stop <container_id_or_name>

Stops one or more running containers.

docker stop my-nginx

docker start <container_id_or_name>

Starts one or more stopped containers.

docker start my-nginx

docker rm [OPTIONS] <container_id_or_name>

Removes one or more containers.

docker rm my-nginx
  • -f: Forces the removal of a running container.

docker logs [OPTIONS] <container_id_or_name>

Fetches the logs of a container.

docker logs my-nginx
  • -f: Follows the log output.
  • --tail <number>: Shows the last N lines of the logs.

docker exec [OPTIONS] <container_id_or_name> <command>

Runs a command in a running container.

docker exec -it my-nginx /bin/bash
  • -it: Creates an interactive terminal session.

docker inspect <container_id_or_name>

Displays detailed, low-level information on a container.

docker inspect my-nginx

Manage Images

docker images

Lists all locally stored images.

docker images

docker build [OPTIONS] .

Builds a Docker image from a Dockerfile.

docker build -t myapp:latest .
  • -t <name:tag>: Sets the name and tag for the image.
  • -f <path_to_dockerfile>: Specifies the path to the Dockerfile.
  • --no-cache: Builds the image without using the cache.

docker pull <image_name:tag>

Downloads an image from a registry (e.g., Docker Hub).

docker pull ubuntu:latest

docker rmi [OPTIONS] <image_id_or_name>

Removes one or more images.

docker rmi myapp:latest
  • -f: Forces the removal of an image.

System

docker system prune [OPTIONS]

Removes unused Docker objects.

docker system prune
  • -a: Removes all unused images, not just dangling ones.
  • --volumes: Removes unused volumes.

Remote Repositories

docker login [OPTIONS] [SERVER]

Logs in to a Docker registry.

docker login docker.io
  • -u <username>: Specifies the username.
  • -p <password>: Specifies the password.
  • --password-stdin: Takes the password from standard input.

docker logout [SERVER]

Logs out from a Docker registry.

docker logout docker.io

docker push <image_name:tag>

Pushes an image or a repository to a registry.

docker push myusername/myapp:latest

Compose Commands

Source: https://docs.docker.com/reference/cli/docker/compose/

docker compose up [OPTIONS]

Builds, creates, starts, and attaches to containers for a service.

docker compose up -d
  • -d: Detached mode.
  • --build: Builds images before starting containers.
  • --force-recreate: Recreates containers even if their configuration has not changed.
  • --no-deps: Doesn't start linked services.

docker compose down [OPTIONS]

Stops and removes containers, networks, and volumes.

docker compose down -v
  • -v: Removes named volumes declared in the volumes section.
  • --rmi all: Removes all images used by any service.

docker compose ps

Lists the containers for the services defined in the docker compose.yml.

docker compose ps

docker compose logs [OPTIONS] [SERVICE...]

Displays log output from services.

docker compose logs -f web
  • -f: Follows the log output.

docker compose build [OPTIONS] [SERVICE...]

Builds or rebuilds services.

docker compose build --no-cache web
  • --no-cache: Does not use cache when building images.

docker compose pull [SERVICE...]

Pulls service images from the registry.

docker compose pull db

docker compose exec <service_name> <command>

Executes a command inside a running service container.

docker compose exec web /bin/bash

Swarm Commands

Source: https://docs.docker.com/reference/cli/docker/swarm/

Manage Swarm

docker swarm init [OPTIONS]

Initializes a new swarm.

docker swarm init --advertise-addr 192.168.1.100
  • --advertise-addr <ip>: Specifies the address for other nodes to connect.

docker swarm join --token <token> <manager_ip:port>

Joins an existing swarm as a worker or manager node.

docker swarm join --token SWMTKN-1-xxxxxxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxx 192.168.1.100:2377

docker swarm join-token <worker|manager>

Displays the command and token to join the swarm.

docker swarm join-token worker

Manage Services

docker service create [OPTIONS] IMAGE [COMMAND]

Creates a new service in the swarm.

docker service create --name my-web-app --replicas 3 -p 80:80 nginx
  • --name <name>: Assigns a name to the service.
  • --replicas <number>: Sets the number of replica tasks.
  • -p <host_port>:<container_port>: Publishes a port.

docker service ls

Lists the services running in the swarm.

docker service ls

docker service ps <service_id_or_name>

Lists the tasks (containers) for a service.

docker service ps my-web-app

docker service scale <service_id_or_name>=<replicas>

Scales one or more services to the desired number of replicas.

docker service scale my-web-app=5

docker service update [OPTIONS] <service_id_or_name>

Updates a service's configuration.

docker service update --image nginx:1.21 my-web-app
  • --image <image:tag>: Updates the service to use a new image version.

docker service rm <service_id_or_name>

Removes a service from the swarm.

docker service rm my-web-app

Stack Commands

Source: https://docs.docker.com/reference/cli/docker/stack/

docker stack deploy [OPTIONS] <stack_name>

Deploys a new stack or updates an existing one from a Compose file.

docker stack deploy -c docker compose.yml my-stack
  • -c <file>: Specifies the path to the Docker Compose file.

docker stack ls

Lists all the stacks deployed in the swarm.

docker stack ls

docker stack rm <stack_name>

Removes a stack.

docker stack rm my-stack