Docker Cheatsheet

Complete Docker cheatsheet covering containers, images, Docker Compose, services, stacks, and Docker Machine commands.

Docker Cheatsheet

Docker

Containers

DevOps

Orchestration

Complete reference for Docker commands, from basic container operations to advanced orchestration with Docker Compose, services, stacks, and Docker Machine.

Quick Reference

🐳 Basic Commands

Essential Docker container and image operations

🔧 Docker Compose

Multi-container application management

⚙️ Services & Stacks

Production deployment and scaling

🖥️ Docker Machine

Virtual machine provisioning and management

Basic Docker Commands

Getting Started

docker init

Create image using this directory's Dockerfile

docker build -t friendlyname .

Run "friendlyname" mapping port 4000 to 80

docker run -p 4000:80 friendlyname

Same thing, but in detached mode

docker run -d -p 4000:80 friendlyname

Container Management

Enter a running container

docker exec -it [container-id] bash

See a list of all running containers

docker ps

See a list of all containers, even the ones not running

docker ps -a

Gracefully stop the specified container

docker stop <hash>

Force shutdown of the specified container

docker kill <hash>

Remove the specified container from this machine

docker rm <hash>

Remove force specified container from this machine

docker rm -f <hash>

Remove all containers from this machine

docker rm $(docker ps -a -q)

Image Management

Show all images on this machine

docker images -a

Remove the specified image from this machine

docker rmi <imagename>

Remove all images from this machine

docker rmi $(docker images -q)

Tag <image> for upload to registry

docker tag <image> username/repository:tag

Upload tagged image to registry

docker push username/repository:tag

Run image from a registry

docker run username/repository:tag

Logging and Registry

Live tail a container's logs

docker logs <container-id> -f

Log in this CLI session using your Docker credentials

docker login

System Cleanup

Remove all unused containers, networks, images (both dangling and unreferenced), and optionally, volumes

docker system prune

Remove all unused containers, networks, images not just dangling ones

docker system prune -a

Remove all unused local volumes

docker volume prune

Remove all unused networks

docker network prune

Docker Compose

Docker Compose is perfect for defining and running multi-container Docker applications using a YAML file.

Basic Operations

Create and start containers

docker-compose up

Create and start containers in detached mode

docker-compose up -d

Stop and remove containers, networks, images, and volumes

docker-compose down

Restart all service

docker-compose restart

Development & Debugging

View output from containers

docker-compose logs

Validate and view the Compose file

docker-compose config

Display the running processes

docker-compose top

Start web service and runs bash as its command, remove old container

docker-compose run -rm -p 2022:22 web bash

Image & Service Management

Pull all image service

docker-compose pull

Build all image service

docker-compose build

Scale special service(s)

docker-compose scale <service_name>=<replica>

Docker Services

Docker services are used in Docker Swarm mode for managing containerized applications across multiple nodes.

Service Management

Create new service

docker service create <options> <image> <command>

List Services

docker service ls

List the tasks of Services

docker service ps

Display detailed information Service(s)

docker service inspect --pretty <service_name>

Scaling & Updates

Scale special service(s)

docker service scale <service_name>=<replica>

Update Service options

docker service update <options> <service_name>

Docker Stack

Docker Stack is used to deploy and manage a complete application stack in Docker Swarm mode.

Stack Operations

List all running applications on this Docker host

docker stack ls

Run the specified Compose file

docker stack deploy -c <composefile> <appname>

List the services associated with an app

docker stack services <appname>

List the running containers associated with an app

docker stack ps <appname>

Tear down an application

docker stack rm <appname>

Docker Machine

Docker Machine is primarily used for provisioning Docker hosts on various platforms and cloud providers.

Machine Creation

Create a VM (Mac, Win7, Linux)

docker-machine create --driver virtualbox myvm1

Win10

docker-machine create -d hyperv --hyperv-virtual-switch "myswitch" myvm1

Machine Management

Start a VM that is currently not running

docker-machine start myvm1

Stop all running VMs

docker-machine stop $(docker-machine ls -q)

Delete all VMs and their disk images

docker-machine rm $(docker-machine ls -q)

Machine Information & Access

View basic information about your node

docker-machine env myvm1

Open an SSH session with the VM; type "exit" to end

docker-machine ssh myvm1

Copy file to node's home dir

docker-machine scp docker-compose.yml myvm1:~

Swarm Operations via Machine

List the nodes in your swarm

docker-machine ssh myvm1 "docker node ls"

Inspect a node

docker-machine ssh myvm1 "docker node inspect <node ID>"

View join token

docker-machine ssh myvm1 "docker swarm join-token -q worker"

Make the worker leave the swarm

docker-machine ssh myvm2 "docker swarm leave"

Make master leave, kill swarm

docker-machine ssh myvm1 "docker swarm leave -f"

Deploy an app

docker-machine ssh myvm1 "docker stack deploy -c <file> <app>"

Best Practices

Follow these Docker best practices for better performance and security.

  • Use .dockerignore to exclude unnecessary files from the build context
  • Multi-stage builds to keep final images small and secure
  • Non-root users in containers for better security
  • Health checks to monitor container health
  • Resource limits to prevent containers from consuming too many resources
  • Environment-specific configs using environment variables
  • Regular cleanup of unused images, containers, and volumes

Learn More

Explore comprehensive Docker documentation

Written by

Deepak Jangra

Created At

Wed Jan 15 2025

Updated At

Fri Jun 13 2025

Cheatsheets

Your go-to resource for quick reference guides on essential development tools and technologies.

© 2025 Deepak Jangra. All rights reserved.