Docker Cheat-sheet for beginners 🐳
keshav Sandhu

keshav Sandhu @keshav___dev

About: 👋 Hi there! I'm a passionate developer with a love for solving challenging problems through code. I enjoy exploring new algorithms, optimizing solutions, and continuously learning about new techs.

Joined:
Sep 13, 2024

Docker Cheat-sheet for beginners 🐳

Publish Date: Sep 19 '24
745 29

🔧 Common Docker Commands

  • Start Docker:
  systemctl start docker  # Linux
  open -a Docker  # macOS
Enter fullscreen mode Exit fullscreen mode
  • Check Docker Version:
  docker --version
Enter fullscreen mode Exit fullscreen mode

📦 Working with Containers

  • List Running Containers:
  docker ps
Enter fullscreen mode Exit fullscreen mode
  • List All Containers (Running + Stopped):
  docker ps -a
Enter fullscreen mode Exit fullscreen mode
  • Run a Container (starts and attaches):
  docker run <image_name>
Enter fullscreen mode Exit fullscreen mode
  • Run in Detached Mode:
  docker run -d <image_name>
Enter fullscreen mode Exit fullscreen mode
  • Run with Port Mapping:
  docker run -p <host_port>:<container_port> <image_name>
Enter fullscreen mode Exit fullscreen mode
  • Stop a Running Container:
  docker stop <container_id>
Enter fullscreen mode Exit fullscreen mode
  • Start a Stopped Container:
  docker start <container_id>
Enter fullscreen mode Exit fullscreen mode
  • Remove a Stopped Container:
  docker rm <container_id>
Enter fullscreen mode Exit fullscreen mode

📜 Images

  • List Docker Images:
  docker images
Enter fullscreen mode Exit fullscreen mode
  • Pull an Image from Docker Hub:
  docker pull <image_name>
Enter fullscreen mode Exit fullscreen mode
  • Build an Image from Dockerfile:
  docker build -t <image_name> .
Enter fullscreen mode Exit fullscreen mode
  • Tag an Image:
  docker tag <image_id> <new_image_name>:<tag>
Enter fullscreen mode Exit fullscreen mode
  • Remove an Image:
  docker rmi <image_id>
Enter fullscreen mode Exit fullscreen mode

🔄 Container Management

  • View Logs of a Container:
  docker logs <container_id>
Enter fullscreen mode Exit fullscreen mode
  • Access a Running Container (Interactive Shell):
  docker exec -it <container_id> /bin/bash
Enter fullscreen mode Exit fullscreen mode
  • Copy Files from Container to Host:
  docker cp <container_id>:<path_inside_container> <host_path>
Enter fullscreen mode Exit fullscreen mode

🏗 Docker Networks

  • List Networks:
  docker network ls
Enter fullscreen mode Exit fullscreen mode
  • Create a Network:
  docker network create <network_name>
Enter fullscreen mode Exit fullscreen mode
  • Connect a Running Container to a Network:
  docker network connect <network_name> <container_id>
Enter fullscreen mode Exit fullscreen mode

🐳 Docker Compose

  • Start Services in Detached Mode:
  docker-compose up -d
Enter fullscreen mode Exit fullscreen mode
  • Stop Services:
  docker-compose down
Enter fullscreen mode Exit fullscreen mode
  • Build and Start Containers:
  docker-compose up --build
Enter fullscreen mode Exit fullscreen mode

📊 Inspecting and Monitoring

  • Inspect Container Details:
  docker inspect <container_id>
Enter fullscreen mode Exit fullscreen mode
  • Display Resource Usage (CPU, Memory):
  docker stats
Enter fullscreen mode Exit fullscreen mode

🛠 Volumes

  • List Volumes:
  docker volume ls
Enter fullscreen mode Exit fullscreen mode
  • Create a Volume:
  docker volume create <volume_name>
Enter fullscreen mode Exit fullscreen mode
  • Mount a Volume (during docker run):
  docker run -v <volume_name>:<path_inside_container> <image_name>
Enter fullscreen mode Exit fullscreen mode

💡 Pro Tip: Use docker system prune to remove unused containers, networks, and images.

Feel free to save or bookmark this cheat sheet for quick reference!

Docker #CheatSheet #Containers #DevOps

Comments 29 total

  • EHSAN.
    EHSAN.Sep 19, 2024

    This is very helpful 😊

  • Bhawesh Chaudhary
    Bhawesh ChaudharySep 19, 2024

    Very useful. :)

  • Gonzalo Marcos
    Gonzalo MarcosSep 19, 2024

    Super helpful! Thanks!

  • Shashi Varshneya
    Shashi VarshneyaSep 19, 2024

    helpful thanks

  • Berkay
    BerkaySep 19, 2024

    Helpful thank you!

  • tauksun
    tauksunSep 20, 2024

    Recover shrinking disk space on every build :

    1. sudo docker image prune -a ( removes un-used images )
    2. sudo docker buildx prune ( removes image cache, this holds the most space )
  • Nnaemeka G. Anyadike
    Nnaemeka G. AnyadikeSep 20, 2024

    Thank. Helpful

  • Andrei Telteu
    Andrei TelteuSep 20, 2024

    You can also make a helper script:
    New file at the root folder called dc without extension.
    Content example. Modify for your needs.

    #!/bin/bash
    trap "exit" 0
    DC="docker compose" # add  `-f docker/compose.yml` if it's in another folder
    if [ $# -eq 0 ]; then
        $DC ps -a
    elif [ $1 == "up" ]; then
        $DC up -d
    elif [ $1 == "nr" ]; then
        if [ $# -gt 1 ]; then
            $DC exec node su node -c "${*:2}"
        else
            $DC exec node su node
        fi
    elif [ $1 == "npm" ]; then
        $DC exec node su node -c "npm ${*:2}"
    elif [ $1 == "install" ]; then
        $DC exec node su node -c 'npm install'
    elif [ $1 == "recreate" ]; then
        $DC up -d --force-recreate ${*:2}
    elif [ $1 == "build" ]; then
        $DC up -d --force-recreate --build ${*:2}
    else
        $DC $*
    fi
    
    Enter fullscreen mode Exit fullscreen mode

    Give execute permission with chmod +x ./dc
    And now you can run:

    • ./dc to show all containers with status
    • ./dc up to start in detached mode
    • ./dc install to run npm install in the node container as user node
    • ./dc npm install package-name-here to run any npm command inside node container. Works with ./dc npm run start too
    • ./dc nr interactive exec inside node container
    • ./dc nr node index.js run any command inside node container
    • ./dc recreate applies any modifications to docker-compose.yml
    • ./dc recreate node applies modifications to compose, only for node container
    • ./dc build if you have a custom dockerfile, does run dc up with a fresh build.
    • ./dc logs -n 10 -f node - any other docker-compose command works as expected.
    • Certification Support system
      Certification Support systemSep 21, 2024

      Hello there
      Interested in Passing all IT and none IT exams with no efforts
      PMP, azure, Salesforce, comptia, CCNA, CISCO and all others with just a click
      WhatsApp Link: wa.me/message/LKPTS55J4JT6H1
      Send us a massage

      No UPFRONT payment
      100% pass guaranteed and pay after pass
      Low cost for each exam cleared
      10yrs+ experience

      Disclaimer: Do not Text if being certified is not your Goal
      No time wasters

      Send us a massage now
      WhatsApp: WhatsApp Link: wa.me/message/LKPTS55J4JT6H1

  • Amir H. Moayeri
    Amir H. MoayeriSep 20, 2024

    Great read, thank you 👍

  • Mohammad-Ali A'RÂBI
    Mohammad-Ali A'RÂBISep 20, 2024

    Great read. I would just suggest you to use Compose v2, which is without a hyphen: docker compose up.

  • Certification Support system
    Certification Support systemSep 21, 2024

    Hello there
    Interested in Passing all IT and none IT exams with no efforts
    PMP, azure, Salesforce, comptia, CCNA, CISCO and all others with just a click
    WhatsApp Link: wa.me/message/LKPTS55J4JT6H1
    Send us a massage

    No UPFRONT payment
    100% pass guaranteed and pay after pass
    Low cost for each exam cleared
    10yrs+ experience

    Disclaimer: Do not Text if being certified is not your Goal
    No time wasters

    Send us a massage now
    WhatsApp: WhatsApp Link: wa.me/message/LKPTS55J4JT6H1

  • Codelab
    CodelabSep 21, 2024

    Hi there. as a mern stack developer, i launched my first web. Feel free to submit your feedback if it needs improving
    codelabs.uz/

  • João Angelo
    João AngeloSep 23, 2024

    Hi keshav Sandhu,
    Top, very nice and helpful !
    Thanks for sharing.

  • Waseem shah Waseem shah
    Waseem shah Waseem shahSep 24, 2024

    Hamen krna kiah

  • Vivek Vishal
    Vivek VishalSep 24, 2024

    Thank you @keshav___dev

  • Rash Edmund
    Rash EdmundSep 29, 2024

    Thanks man

  • sumukha sureban
    sumukha surebanOct 12, 2024

    Can I get PDF of this ..?

  • Erick Otieno Awino
    Erick Otieno AwinoOct 14, 2024

    This is incredible.

  • Prathmesh Jagtap
    Prathmesh JagtapOct 21, 2024

    The most used commands in docker.
    Thank for Writing.

  • Abdullah
    AbdullahNov 3, 2024

    Adding one more

    docker system prune

    This will remove:
    - all stopped containers
    - all networks not used by at least one container
    - all dangling images
    - unused build cache

  • Andreas
    AndreasDec 6, 2024

    This is awesome! Instantly added to github.com/devmount/CheatSheets

    Thank you!

    • Raavan
      RaavanDec 8, 2024

      @devmount Your Github profile is super cool, bro. Much to learn

      • Andreas
        AndreasDec 8, 2024

        Thank you so much 🤗

  • Mahmoud Zakaria
    Mahmoud ZakariaDec 6, 2024

    Thanks!!

  • Tyrus Malmström
    Tyrus MalmströmDec 9, 2024

    Excellent article, thank you! 💯

    Something that made me smile, you can tell that the banner image was created using AI :D

    • keshav Sandhu
      keshav SandhuDec 11, 2024

      If service is there, why not use it 😉

Add comment