Post

Docker Cheat Sheet Tutorial

Basic Commands

  • Check Docker Version

    1
    
    docker --version
    
  • Get Docker Info

    1
    
    docker info
    
  • List Docker Images

    1
    
    docker images
    
  • List Docker Containers

    1
    2
    
    docker ps          # Running containers
    docker ps -a       # All containers, including stopped ones
    
  • Run a Docker Container

    1
    
    docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
    
  • Start a Docker Container

    1
    
    docker start CONTAINER_ID
    
  • Stop a Docker Container

    1
    
    docker stop CONTAINER_ID
    
  • Remove a Docker Container

    1
    
    docker rm CONTAINER_ID
    
  • Remove a Docker Image

    1
    
    docker rmi IMAGE_ID
    

Building and Managing Images

  • Build a Docker Image

    1
    
    docker build -t IMAGE_NAME:TAG .
    
  • Tag a Docker Image

    1
    
    docker tag IMAGE_ID REPOSITORY:TAG
    
  • Push an Image to a Repository

    1
    
    docker push REPOSITORY:TAG
    
  • Pull an Image from a Repository

    1
    
    docker pull REPOSITORY:TAG
    
  • View Dockerfile History

    1
    
    docker history IMAGE_NAME
    

Container Operations

  • Execute a Command in a Running Container

    1
    
    docker exec [OPTIONS] CONTAINER_ID COMMAND [ARG...]
    
  • Get Interactive Shell in a Container

    1
    
    docker exec -it CONTAINER_ID /bin/bash
    
  • View Container Logs

    1
    
    docker logs CONTAINER_ID
    
  • Check Container Resource Usage

    1
    
    docker stats
    
  • Inspect Container Details

    1
    
    docker inspect CONTAINER_ID
    

Networking

  • List Docker Networks

    1
    
    docker network ls
    
  • Create a New Docker Network

    1
    
    docker network create NETWORK_NAME
    
  • Connect a Container to a Network

    1
    
    docker network connect NETWORK_NAME CONTAINER_ID
    
  • Disconnect a Container from a Network

    1
    
    docker network disconnect NETWORK_NAME CONTAINER_ID
    
  • Inspect a Network

    1
    
    docker network inspect NETWORK_NAME
    

Volumes

  • List Docker Volumes

    1
    
    docker volume ls
    
  • Create a Docker Volume

    1
    
    docker volume create VOLUME_NAME
    
  • Inspect a Volume

    1
    
    docker volume inspect VOLUME_NAME
    
  • Remove a Docker Volume

    1
    
    docker volume rm VOLUME_NAME
    

Docker Compose

  • Install Docker Compose

    1
    
    # Follow official installation instructions: https://docs.docker.com/compose/install/
    
  • Start Services Defined in docker-compose.yml

    1
    
    docker-compose up
    
  • Start Services in Detached Mode

    1
    
    docker-compose up -d
    
  • Stop Services

    1
    
    docker-compose down
    
  • View Logs for Services

    1
    
    docker-compose logs
    
  • Execute Command in a Service Container

    1
    
    docker-compose exec SERVICE_NAME COMMAND
    
  • List All Docker Compose Services

    1
    
    docker-compose ps
    

Advanced Commands

  • Build and Run Docker Image in One Command

    1
    
    docker build -t IMAGE_NAME:TAG . && docker run IMAGE_NAME:TAG
    
  • Save a Docker Image to a Tar File

    1
    
    docker save -o IMAGE_NAME.tar IMAGE_NAME:TAG
    
  • Load a Docker Image from a Tar File

    1
    
    docker load -i IMAGE_NAME.tar
    
  • Export a Docker Container to a Tar File

    1
    
    docker export CONTAINER_ID -o CONTAINER_NAME.tar
    
  • Import a Docker Container from a Tar File

    1
    
    docker import CONTAINER_NAME.tar
    
  • Remove All Stopped Containers

    1
    
    docker container prune
    
  • Remove All Unused Images

    1
    
    docker image prune -a
    
  • Remove All Unused Volumes

    1
    
    docker volume prune
    
  • Remove All Unused Networks

    1
    
    docker network prune
    

Helpful Tips

  • View Docker Command Help

    1
    
    docker COMMAND --help
    
  • View Docker Compose Command Help

    1
    
    docker-compose COMMAND --help
    

This cheatsheet should cover most of the essential Docker commands and operations you’ll need for everyday tasks and advanced use cases.

This post is licensed under CC BY 4.0 by the author.