DEV Community

Sameh Sharaf
Sameh Sharaf

Posted on • Edited on

13 3

Your Humble Docker Command-Line Guide

Your Humble Docker Guide

This guide gathers the basic commands every Docker user will frequently need. Personally, I keep getting back to it from time to time when my Docker knowledge gets rusty. Hope it helps you too!

List downloaded or created images

docker images

Delete image

docker rmi <image-name>

List running containers

docker ps

List all containers (including stopped ones)

docker ps -a

List container IDs

docker ps -a -q

Delete all containers

docker rm $(docker ps -a -q)

Prune unused images & containers

docker system prune

Prune all images & containers:

docker system prune -a

Build image

Note the dot (.) at the end

docker build -t image-name .

Build image referring to Dockerfile location

docker build -t image-name -f /path/to/your/Dockerfile .

Run container

Note: Run = create + start

docker run -dit image-name:latest

Create container

docker create image-name:latest

Start (existing) container

docker start <container-id>

Log into container's shell

Note: Assuming image's OS is Ubuntu/Debian

docker exec -it <container-id> sh

Stop container

docker stop <container-id>

Delete container

docker rm <container-id>

Force delete (running) container

docker rm -f <container-id>

Run container with port published

docker run -p <local-port>:<container-port> image-name:latest

E.g.: If the image exposes port 8008 and you want to use port 3000 on the machine hosting the container:

docker run -p 3000:8008 image-name:latest

Get container logs

docker logs <container-id>
view raw docker.md hosted with ❤ by GitHub

Top comments (6)

Collapse
 
kodekloud profile image
KodeKloud

Looks good. Thanks for sharing:)

Collapse
 
samehsharaf profile image
Sameh Sharaf

Glad it's helpful!

Collapse
 
shinde_hs profile image
Hanmant Shinde (HS)

This is awesome!

Collapse
 
samehsharaf profile image
Sameh Sharaf

Thanks!

Collapse
 
mausworks profile image
Rasmus Wennerström

I need this, thanks! :)

Collapse
 
samehsharaf profile image
Sameh Sharaf

Anytime!

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay