DEV Community

Cover image for Docker Cheatsheet
Mrinalini Sugosh (Mrina)
Mrinalini Sugosh (Mrina)

Posted on • Updated on

Docker Cheatsheet

I often find myself looking up commands or forgetting what I did for a simple task. Thought it would be a good opportunity to compile all the Docker commands I am using on a regular basis for developing applications with Docker. Hoping to crowdsource this, so let me know in the comments below!

DOCKER MACHINE

List all Docker engines:

docker-machine ls
Enter fullscreen mode Exit fullscreen mode

Create a Docker engine:

docker-machine create --driver virtualbox default
Enter fullscreen mode Exit fullscreen mode

Set environment variables for Docker engine:

docker-machine env default
eval $(docker-machine env default)
Enter fullscreen mode Exit fullscreen mode

Start a Docker engine:

docker-machine start default
Enter fullscreen mode Exit fullscreen mode

Stop a Docker engine:

docker-machine stop default
Enter fullscreen mode Exit fullscreen mode

Retrieve IP address for running Docker engine:

docker-machine ip default
Enter fullscreen mode Exit fullscreen mode

DOCKER IMAGES

List Docker images:

docker images
Enter fullscreen mode Exit fullscreen mode

Remove Docker image:

docker rmi <image_id>
docker image rm <image_id>
Enter fullscreen mode Exit fullscreen mode

Create Docker image (requirement: Dockerfile):

docker build -t <dockerhub_username>/<custom_docker_image_name> .
Enter fullscreen mode Exit fullscreen mode

DOCKER CONTAINERS

List Docker containers:

docker ps
docker container ls -a
Enter fullscreen mode Exit fullscreen mode

Stop and remove Docker container:

docker stop <container_id>
docker rm <container_id>
Enter fullscreen mode Exit fullscreen mode

Remove all stopped Docker containers:

docker container prune
Enter fullscreen mode Exit fullscreen mode

Delete all stopped containers:

docker rm $(docker ps -a -q)
Enter fullscreen mode Exit fullscreen mode

Create Docker container (requirement: Docker image):

docker run --name <custom_container_name> -p <new_port>:<defined_port> -d <dockerhub_username>/<custom_docker_image_name>
Enter fullscreen mode Exit fullscreen mode

DOCKER COMPOSE

If development, build, run and keep running (e.g. service_id equals dev):

docker-compose build <service_id>
docker-compose up <service_id>
Enter fullscreen mode Exit fullscreen mode

If testing, build and run once (e.g. service_id equals test):

docker-compose build <service_id>
docker-compose run --rm <service_id>
Enter fullscreen mode Exit fullscreen mode

DOCKER SYSTEM

Remove all unused containers, networks, images (both dangling and unreferenced), volumes and any resource created. Note: Do not apply this command for resources in production. Recommended for use in dev/test environments.

docker system prune -a 
Enter fullscreen mode Exit fullscreen mode

Top comments (16)

Collapse
 
jitendra profile image
Jitendra Thakur

it's really nice cheatsheet.
I would like to add :
docker system prune -a
to delete all the resources which has been created by user.
Note: Do not apply this command in production like environment. it is just for practice purpose.

Collapse
 
mrinasugosh profile image
Mrinalini Sugosh (Mrina)

Glad it was helpful! Sweet thanks for the suggestion just added it :)

Collapse
 
cordigliere profile image
Cordigliere • Edited

Great Cheatsheet !!
I would add:
docker rm $(docker ps -a -q)
to delete all stopped containers

Collapse
 
floryn90 profile image
Florin Lungu

It's wrong this command since it will remove all containers (running and stopped containers)!
docker ps -a -q will show you all containers into short syntax and docker rm will remove them!
The right syntax should be docker rm $(docker ps --filter status=exited -q) to remove all stopped containers

Collapse
 
cordigliere profile image
Cordigliere

No you have to use -f (force) to remove also running containers

Collapse
 
mrinasugosh profile image
Mrinalini Sugosh (Mrina)

Thanks for the contribution! Just added it :)

Collapse
 
ted_lee_2697 profile image
Ted Lee

Thanks for sharing!

Collapse
 
mrinasugosh profile image
Mrinalini Sugosh (Mrina)

Glad it was useful!

Collapse
 
vajracool profile image
VajraCool

Thank you for sharing , this would help many for sure.

Collapse
 
mrinasugosh profile image
Mrinalini Sugosh (Mrina)

Awesome so glad it was helpful!

Collapse
 
rkyadav profile image
Ram

Thank You for sharing

Collapse
 
mrinasugosh profile image
Mrinalini Sugosh (Mrina)

Glad it was useful!

Collapse
 
speni profile image
spenis

Thank you for the post, much appreciated!

Collapse
 
mrinasugosh profile image
Mrinalini Sugosh (Mrina)

So glad it was helpful!

Collapse
 
swatirajan7 profile image
Swati Rajan • Edited

Thanks for sharing...will you be making one for Kubernetes as well?

Collapse
 
mrinasugosh profile image
Mrinalini Sugosh (Mrina)

Glad you found it useful. Great idea! I can certainly start on one :)