DEV Community

James Murdza
James Murdza

Posted on

The only Docker cheatsheet you need 🐳

My reference of the most useful commands to know when you're building in Docker:

Build and run

Build an image:

docker build -t myimage .
Enter fullscreen mode Exit fullscreen mode

Build an image, deleting the old one:

docker rmi myimage && docker build -t myimage .
Enter fullscreen mode Exit fullscreen mode

Build an image, and print all the steps:

docker build --progress=plain -t myimage .
Enter fullscreen mode Exit fullscreen mode

Run container from image, with port forwarding:

docker run -p 80:80 myimage
Enter fullscreen mode Exit fullscreen mode

Run container for debugging purposes, replacing the entrypoint with a shell:

docker run --rm -it --entrypoint bash myimage
Enter fullscreen mode Exit fullscreen mode

Inventory

List all images:

docker image ls
Enter fullscreen mode Exit fullscreen mode

List all containers: (The -a flag is to show stopped containers as well)

docker container ls -a
Enter fullscreen mode Exit fullscreen mode

List all docker images and containers and file sizes:

docker system df -v
Enter fullscreen mode Exit fullscreen mode

Show the history of an image:

docker history myimage
Enter fullscreen mode Exit fullscreen mode

Clean up

**Dangling images* are untagged and not needed by any tagged images.*
**Unused image* are not needed by any existing containers.*

Delete stopped containers:

docker container prune
Enter fullscreen mode Exit fullscreen mode

Remove dangling images:

docker image prune
Enter fullscreen mode Exit fullscreen mode

Remove unused images:

docker image prune -a
Enter fullscreen mode Exit fullscreen mode

Remove build cache:

docker builder prune
Enter fullscreen mode Exit fullscreen mode

Top comments (0)