DEV Community

Cover image for Docker Cheat-sheet for beginners 🐳

Docker Cheat-sheet for beginners 🐳

keshav Sandhu on September 19, 2024

šŸ”§ Common Docker Commands Start Docker: systemctl start docker # Linux open -a Docker # macOS Enter fullscreen mode ...
Collapse
Ā 
andreitelteu profile image
Andrei Telteu •

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.
Collapse
Ā 
abdullah-dev0 profile image
Abdullah •

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

Collapse
Ā 
devmount profile image
Andreas •

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

Thank you!

Collapse
Ā 
mahendrarao profile image
Raavan •

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

Collapse
Ā 
devmount profile image
Andreas •

Thank you so much šŸ¤—

Collapse
Ā 
prathmeshjagtap profile image
Prathmesh Jagtap •

The most used commands in docker.
Thank for Writing.

Collapse
Ā 
vishalvivekm profile image
Vivek Vishal •

Thank you @keshav___dev

Collapse
Ā 
mahmoud974 profile image
Mahmoud Zakaria •

Thanks!!

Collapse
Ā 
tiru5 profile image
Tyrus Malmstrƶm •

Excellent article, thank you! šŸ’Æ

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

Collapse
Ā 
keshav___dev profile image
keshav Sandhu •

If service is there, why not use it šŸ˜‰

Collapse
Ā 
orashus profile image
Rash Edmund •

Thanks man

Collapse
Ā 
jangelodev profile image
JoĆ£o Angelo •

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

Collapse
Ā 
awinooliyo profile image
Erick Otieno Awino •

This is incredible.

Collapse
Ā 
ishashi profile image
Shashi Varshneya •

helpful thanks

Collapse
Ā 
tauksun profile image
tauksun •

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 )
Collapse
Ā 
aerabi profile image
Mohammad-Ali A'RƂBI •

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

Collapse
Ā 
keshav___dev profile image
keshav Sandhu •
Collapse
Ā 
berkaydisli profile image
Berkay •

Helpful thank you!

Collapse
Ā 
xybersec profile image
Bhawesh Chaudhary •

Very useful. :)

Collapse
Ā 
ymir profile image
Amir H. Moayeri •

Great read, thank you šŸ‘

Collapse
Ā 
nnaemeka_ganyadike_7f29 profile image
Nnaemeka G. Anyadike •

Thank. Helpful

Collapse
Ā 
developerehsan profile image
EHSAN. •

This is very helpful 😊

Collapse
Ā 
gonzalo_marcos_1981 profile image
Gonzalo Marcos • • Edited

Super helpful! Thanks!

Collapse
Ā 
waseem_shahwaseemshah_0 profile image
Waseem shah Waseem shah •

Hamen krna kiah

Collapse
Ā 
dev-sumukha profile image
sumukha sureban •

Can I get PDF of this ..?

Collapse
Ā 
henry_johnson profile image
Codelab •

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

Some comments have been hidden by the post's author - find out more