Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Docker Cheat-sheet for beginners 🐳
keshav Sandhu
keshav Sandhu

Posted on

     

Docker Cheat-sheet for beginners 🐳

🔧Common Docker Commands

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

📦Working with Containers

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

📜Images

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

🔄Container Management

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

🏗Docker Networks

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

🐳Docker Compose

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

📊Inspecting and Monitoring

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

🛠Volumes

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

💡Pro Tip: Usedocker 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

Top comments(26)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
andreitelteu profile image
Andrei Telteu
Full-Stack Web Developer working with Laravel and React-Native mainly,learning SolidJS, Golang, and Kubernetes on the side
  • Location
    Constanta, Romania
  • Work
    Full-Stack Web Developer
  • Joined

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

#!/bin/bashtrap"exit" 0DC="docker compose"# add  `-f docker/compose.yml` if it's in another folderif[$#-eq 0];then$DC ps-aelif[$1=="up"];then$DC up-delif[$1=="nr"];then    if[$#-gt 1];then$DCexecnode su node-c"${*:2}"else$DCexecnode su nodefielif[$1=="npm"];then$DCexecnode su node-c"npm${*:2}"elif[$1=="install"];then$DCexecnode 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 modeExit fullscreen mode

Give execute permission withchmod +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.
CollapseExpand
 
abdullah-dev0 profile image
Abdullah
Passionate Full-Stack DeveloperSkilled in the MERN stack and Next.js, I am dedicated to creating and improving web applications.
  • Location
    Pakistan
  • Education
    BSIT
  • Pronouns
    He/Him
  • Work
    Student
  • Joined

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

CollapseExpand
 
devmount profile image
Andreas
freelancing software engineer.javascript. php. python. css.husband. dad². guitarero. climber. retrogamer.
  • Location
    Berlin, Germany
  • Education
    M.Sc. Computer Engineering
  • Pronouns
    he/him/his
  • Work
    Freelancing Software Engineer
  • Joined

This is awesome! Instantly added togithub.com/devmount/CheatSheets

Thank you!

CollapseExpand
 
mahendrarao profile image
Raavan
Into developing APIs & Integration
  • Education
    Bachelor of Computer application
  • Work
    Sprinklr Inc
  • Joined

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

CollapseExpand
 
devmount profile image
Andreas
freelancing software engineer.javascript. php. python. css.husband. dad². guitarero. climber. retrogamer.
  • Location
    Berlin, Germany
  • Education
    M.Sc. Computer Engineering
  • Pronouns
    he/him/his
  • Work
    Freelancing Software Engineer
  • Joined

Thank you so much 🤗

CollapseExpand
 
mahmoud974 profile image
Mahmoud Zakaria
  • Joined

Thanks!!

CollapseExpand
 
vishalvivekm profile image
Vivek Vishal
Software engineer
  • Joined

Thank you@keshav___dev

CollapseExpand
 
prathmeshjagtap profile image
Prathmesh Jagtap
  • Joined

The most used commands in docker.
Thank for Writing.

CollapseExpand
 
jangelodev profile image
João Angelo
Full Stack Developer | Angular, Nx and .NET Core Expert | Building high-performance, scalable and secure solutions for the web.
  • Location
    Brazil
  • Pronouns
    He / Him
  • Joined

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

CollapseExpand
 
orashus profile image
Rash Edmund
Software developer
  • Education
    Rebase Academy
  • Pronouns
    he/him
  • Work
    Full stack web developer
  • Joined

Thanks man

CollapseExpand
 
awinooliyo profile image
Erick Otieno Awino
I am a newbie Software Engineering student at ALX Africa.
  • Location
    Kenya
  • Joined

This is incredible.

CollapseExpand
 
tiru5 profile image
Tyrus Malmström
I love Pizza, Programming, and People.Another individual on this planet that loves solving problems with code.
  • Email
  • Location
    localghost:1337
  • Education
    Bachelor of Computer Science
  • Pronouns
    he/him
  • Work
    Senior Software Engineer @ iris.ai
  • Joined

Excellent article, thank you! 💯

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

CollapseExpand
 
keshav___dev profile image
keshav Sandhu
👋 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

If service is there, why not use it 😉

Some comments may only be visible to logged-in visitors.Sign in to view all comments. Some comments have been hidden by the post's author -find out more

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

👋 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

More fromkeshav Sandhu

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp