Skip to content

📄 Docker (Essential Commands & Practical Cheatsheet)

🟦 Containers

  • List running containers
    docker ps

  • List all containers
    docker ps -a

  • Run a container
    docker run -d -p <host_port>:<container_port> -e <KEY>=<VALUE> --name <name> <image>
    Description:
    -d - run the container in detached mode (in the background)
    -p <host_port>:<container_port> - map a container port to a port on the host (check the image documentation to know which ports the service exposes)
    -e <KEY>=<VALUE> - set an environment variable inside the container
    --name <name> - assign a custom name to the container
    <image> - the image to run

  • Stop a container
    docker stop <container>

  • Remove a container
    docker rm <container>
    docker rm -f <container> — force removal

  • Enter a container shell
    docker exec -it <container> bash

  • View logs
    docker logs <container>
    docker logs -f <container> — follow live logs

  • Inspect container details
    docker inspect <container>

  • Container stats
    docker stats

  • Stop all containers
    docker stop $(docker ps -q)

  • Remove all containers
    docker rm $(docker ps -aq)


🟩 Images

  • List images
    docker image ls

  • Remove an image
    docker rmi <image_id>

  • Build an image
    docker build -t <image_name> .

  • Pull an image
    docker pull <image>

  • Prune unused images
    docker image prune docker image prune -a - removes all unused images


🟧 Volumes

  • List volumes
    docker volume ls

  • Remove a volume
    docker volume rm <volume>

  • Mount a volume
    -v <host_path>:<container_path>


🟨 Networks

  • List networks
    docker network ls

  • Create a network
    docker network create <network_name>

  • Remove a network
    docker network rm <network_name>

  • Run a container in a network
    docker run --network <network_name> ...


🟪 Docker Compose

  • Start services in background
    docker compose up -d

  • Stop and remove services
    docker compose down

  • Stop without removing
    docker compose stop

  • Build + start
    docker compose up --build

  • Build only
    docker compose build

  • View logs
    docker compose logs -f

  • Restart services
    docker compose restart