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
Create a Docker engine:
docker-machine create --driver virtualbox default
Set environment variables for Docker engine:
docker-machine env default
eval $(docker-machine env default)
Start a Docker engine:
docker-machine start default
Stop a Docker engine:
docker-machine stop default
Retrieve IP address for running Docker engine:
docker-machine ip default
DOCKER IMAGES
List Docker images:
docker images
Remove Docker image:
docker rmi <image_id>
docker image rm <image_id>
Create Docker image (requirement: Dockerfile):
docker build -t <dockerhub_username>/<custom_docker_image_name> .
DOCKER CONTAINERS
List Docker containers:
docker ps
docker container ls -a
Stop and remove Docker container:
docker stop <container_id>
docker rm <container_id>
Remove all stopped Docker containers:
docker container prune
Delete all stopped containers:
docker rm $(docker ps -a -q)
Create Docker container (requirement: Docker image):
docker run --name <custom_container_name> -p <new_port>:<defined_port> -d <dockerhub_username>/<custom_docker_image_name>
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>
If testing, build and run once (e.g. service_id equals test):
docker-compose build <service_id>
docker-compose run --rm <service_id>
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
Top comments (16)
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.
Glad it was helpful! Sweet thanks for the suggestion just added it :)
Great Cheatsheet !!
I would add:
docker rm $(docker ps -a -q)
to delete all stopped containers
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
No you have to use -f (force) to remove also running containers
Thanks for the contribution! Just added it :)
Thanks for sharing!
Glad it was useful!
Thank you for sharing , this would help many for sure.
Awesome so glad it was helpful!
Thank You for sharing
Glad it was useful!
Thank you for the post, much appreciated!
So glad it was helpful!
Thanks for sharing...will you be making one for Kubernetes as well?
Glad you found it useful. Great idea! I can certainly start on one :)