Back to Portfolio
DevOps / Containers

Docker Commands Cheat Sheet

Essential Docker commands every developer should know. From basic container management to advanced networking.

By Ramjee PrasadDecember 5, 202510 min read

1. Basic Docker Commands

Check Docker Version

docker --version

Displays the installed Docker version.

Docker System Info

docker info

Shows detailed information about the Docker installation.

Docker Help

docker --help

Lists all available Docker commands.

2. Container Management

Run a Container

docker run -d --name my-container -p 8080:80 nginx

Runs a container in detached mode with port mapping. -d = detached, -p = port mapping.

List Running Containers

docker ps

Shows all currently running containers.

List All Containers

docker ps -a

Shows all containers including stopped ones.

Stop a Container

docker stop <container_id>

Gracefully stops a running container.

Start a Container

docker start <container_id>

Starts a stopped container.

Remove a Container

docker rm <container_id>

Removes a stopped container. Use -f to force remove a running container.

Container Logs

docker logs -f <container_id>

View container logs. -f follows the log output in real-time.

Execute Command in Container

docker exec -it <container_id> /bin/bash

Opens an interactive shell inside the running container.

3. Image Commands

List Images

docker images

Lists all Docker images on your system.

Pull an Image

docker pull nginx:latest

Downloads an image from Docker Hub.

Build an Image

docker build -t my-app:1.0 .

Builds an image from a Dockerfile in the current directory.

Tag an Image

docker tag my-app:1.0 username/my-app:1.0

Creates a tag for an image (useful for pushing to registries).

Push an Image

docker push username/my-app:1.0

Pushes an image to Docker Hub or another registry.

Remove an Image

docker rmi <image_id>

Removes an image from your system.

4. Volume Commands

Create a Volume

docker volume create my-volume

Creates a new named volume for persistent data storage.

List Volumes

docker volume ls

Lists all Docker volumes.

Run with Volume

docker run -d -v my-volume:/data nginx

Runs a container with a volume mounted at /data.

Bind Mount

docker run -d -v $(pwd):/app nginx

Mounts current directory to /app in the container.

Remove Volume

docker volume rm my-volume

Removes a volume. Volume must not be in use.

5. Network Commands

List Networks

docker network ls

Lists all Docker networks.

Create a Network

docker network create my-network

Creates a new bridge network.

Run Container in Network

docker run -d --network my-network --name app nginx

Runs a container connected to a specific network.

Connect Container to Network

docker network connect my-network container_name

Connects an existing container to a network.

Inspect Network

docker network inspect my-network

Shows detailed information about a network.

6. Docker Compose

Example docker-compose.yml

version: '3.8'
services:
  web:
    image: nginx:latest
    ports:
      - "80:80"
    volumes:
      - ./html:/usr/share/nginx/html
    depends_on:
      - db
  
  db:
    image: postgres:15
    environment:
      POSTGRES_DB: mydb
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
    volumes:
      - postgres_data:/var/lib/postgresql/data

volumes:
  postgres_data:

Start Services

docker compose up -d

Starts all services defined in docker-compose.yml in detached mode.

Stop Services

docker compose down

Stops and removes all containers, networks created by compose.

View Logs

docker compose logs -f

Shows combined logs from all services.

Rebuild Services

docker compose up -d --build

Rebuilds images and restarts services.

Scale Service

docker compose up -d --scale web=3

Runs 3 instances of the web service.

7. Cleanup Commands

⚠️ Warning: These commands permanently delete resources. Use with caution!

Remove All Stopped Containers

docker container prune

Removes all stopped containers.

Remove Unused Images

docker image prune -a

Removes all images not used by containers.

Remove Unused Volumes

docker volume prune

Removes all unused volumes.

Remove Everything Unused

docker system prune -a --volumes

Nuclear option: removes all unused containers, images, networks, and volumes.

Check Disk Usage

docker system df

Shows Docker disk usage breakdown.

Written by Ramjee Prasad • Backend Developer

← Back to Portfolio