Docker Cheat Sheet
Docker Command Aliases
Define these aliases in your shell configuration file (e.g., .bashrc
, .zshrc
) for quicker access.
alias dc='docker-compose'
alias de='docker-machine env'
alias di='docker images'
alias dl='docker-machine ls'
alias dm='docker-machine'
alias dps='docker ps'
alias dpsa='docker ps -a'
Docker Boilerplates and Templates
Explore pre-built Dockerfile templates for various programming languages and frameworks to accelerate your development setup. Find them at Docker boilerplates.
Running Docker Containers
Run an image in detached mode
This command starts a Docker container from a specified image, names it, and maps port 8080 on the host to port 8080 in the container. The -d
flag runs the container in detached mode (in the background).
$ docker run -d --name my_docker -p 8080:8080 image_name
Get the IP Address of a Container
Retrieve the IP address assigned to a running Docker container.
$ docker inspect -f "{{ .NetworkSettings.IPAddress }}" my_docker
Interacting with Docker Containers
Attach to a running container
Execute commands inside a running Docker container. You can use either the container ID or its name.
$ docker exec -it 32adfbf6eb62 /bin/bash
$ docker exec -it my_docker /bin/bash
To detach from a running container without stopping it, use the escape sequence: CTRL + p
followed by CTRL + q
.
Mounting the current directory into a container
This is useful for development workflows where you want to edit code on your host machine and have the changes reflected immediately inside the container. The --rm
flag automatically removes the container when it exits.
# On Linux/macOS (using PowerShell)
docker run --rm -v ${PWD}:/home/jovyan/work jupyter/scipy-notebook
# On Windows (using Command Prompt)
docker run --rm %cd%:/home/jovyan/work jupyter/scipy-notebook
For more details, see Stack Overflow discussion.
Managing Docker Containers
Stop a container
Gracefully stop a running Docker container using its ID.
$ docker kill <container_id>
Remove a stopped container
Delete a Docker container that is no longer running.
$ docker rm <container_id>
Remove multiple stopped containers
Efficiently remove all containers that have exited.
docker rm $(docker ps --filter status=exited -q)
# Or using xargs for potentially large numbers of containers
docker ps --filter status=exited -q | xargs docker rm
Refer to the official documentation for docker rm
.
Start a container with the remove switch
This option automatically removes the container once it has stopped.
$ docker run -d --name my_docker image_name --rm
See Stack Overflow for stopping and removing containers.
See all containers (running and stopped)
List all Docker containers on your system, including those that are not currently running.
$ docker ps -a
Docker System Cleanup
Clean up unused Docker resources
This command removes stopped containers, dangling images, unused networks, and build cache. It's a powerful way to free up disk space.
$ docker system prune
Remove all exited containers
Specifically target and remove containers that have finished execution.
$ docker rm $(docker ps -a -f status=exited -q)
Remove all stopped containers
Remove all containers that are not currently running.
$ docker rm $(docker ps -a -q)
Managing Docker Images
See all images
List all Docker images available on your system.
$ docker images -a
Format image list output
Customize the output of the docker images
command for better readability.
$ docker images --format "table {{.Repository}}\t{{.Tag}}\t{{.ID}}"
You can also configure default formats in your ~/.docker/config.json
file:
{
"psFormat": "table {{.Names}}\t{{.Image}}\t{{.RunningFor}} ago\t{{.Status}}\t{{.Command}}",
"imagesFormat": "table {{.Repository}}\t{{.Tag}}\t{{.ID}}\t{{.Size}}"
}
See Docker Quicktip #7: docker ps --format.
Remove all untagged (dangling) images
Dangling images are layers that are not tagged and are not referenced by any container. They often result from build processes.
$ docker rmi $(docker images -q -f dangling=true)
$ docker rmi $(docker images | grep "^<none>" | awk '{print $3}')
$ docker images -q -a | xargs --no-run-if-empty docker rmi
Remove images by tag pattern
Delete Docker images that match a specific tag pattern.
docker rmi $(docker images --filter reference=*/*/my-image*:* -q)
For more information, see Stack Overflow on deleting images by tag and Docker | Using filters.
Managing Docker Volumes
List volumes
Display all Docker volumes on your system.
$docker volume ls
$docker volume ls -f dangling=true
Remove all unused volumes
Clean up volumes that are not currently attached to any containers.
$ docker volume prune
$ docker volume prune -f # Bypasses the confirmation prompt
Create a named volume
Manually create a Docker volume with a specific name.
$ docker volume create db_data
View a volume's contents
Use a temporary container to inspect the files within a Docker volume.
docker run --rm -i -v=postgres-data:/tmp/myvolume busybox find /tmp/myvolume
Docker Compose Commands
Rebuild when composing
When using docker-compose
, this command rebuilds the images before starting the services in detached mode.
$ docker-compose up -d --build
Tagging Docker Images
Tag an image after building
Assign a tag (e.g., dev:latest
) to an image after it has been successfully built.
$ docker build -t dev:latest .
Other Useful Resources
Related Cheat Sheets
A comprehensive Docker Cheat Sheet by wsargent.
External Resources
- Awesome Docker - A curated list of Docker resources.
- Docker Bench Security - A script to check if your Docker deployment is configured securely.
- Remove Untagged Images From Docker - An explanation of how to clean up dangling images.
- What are Docker <none>:<none> images? - Understanding untagged images.
- Docker – Clean Up After Yourself! - Tips for managing Docker resources.
- How to remove unused Docker containers and images - A guide to cleaning up Docker.
- docker rmi - Official documentation for removing images.
- How do you attach and detach from Docker's process? - Stack Overflow discussion on container interaction.
- How to remove old and unused Docker images - Strategies for image cleanup.
- How to remove <none> images after building - Forum discussion on untagged images.
- How To Remove Docker Images, Containers, and Volumes - DigitalOcean tutorial on cleanup.
- How to list the content of a named volume in docker 1.9+? - Inspecting volume contents.
- How to persist data in a dockerized postgres database using volumes - Example of using volumes for data persistence.