Tools › Cheat sheets › Containers & DevOps › Docker
🐳

Docker cheat sheet

Essential Docker and Docker Compose commands. Commands marked with a warning delete containers, images or volumes (volumes may hold your data), so read them twice.

Click any command to copy it.

Run and manage containers

CommandWhat it does
docker psList running containers.
docker ps -aList all containers including stopped ones.
docker run -d --name web -p 8080:80 nginxRun a container in the background, mapping host port 8080 to container port 80.
docker run -it --rm alpine shRun an interactive shell and remove the container on exit.
docker exec -it web shOpen a shell in a running container.
docker stop webStop a container.
docker start webStart a stopped container.
docker restart webRestart a container.
docker rm webRemove a stopped container.
docker rm -f webDestructive: Force-remove a container, even if it is running.

Logs and inspection

CommandWhat it does
docker logs -f --tail 100 webFollow the last 100 log lines.
docker inspect webShow full container details as JSON.
docker statsLive CPU, memory and network usage.
docker top webShow processes inside a container.
docker port webShow port mappings.
docker cp web:/path/file ./Copy a file out of a container.

Images

CommandWhat it does
docker imagesList local images.
docker pull image:tagDownload an image.
docker build -t name:tag .Build an image from the Dockerfile in the current directory.
docker tag name:tag registry/name:tagAdd another name to an image.
docker push registry/name:tagUpload an image to a registry.
docker rmi imageRemove an image.
docker save -o image.tar name:tagExport an image to a tar file.
docker load -i image.tarImport an image from a tar file.

Cleanup

CommandWhat it does
docker system dfShow disk space used by Docker.
docker container pruneDestructive: Remove all stopped containers.
docker image pruneRemove dangling (untagged) images.
docker system pruneDestructive: Remove stopped containers, unused networks and dangling images.
docker system prune -a --volumesDestructive: Also remove all unused images and unused volumes, which can delete data.
docker volume lsList volumes.

Docker Compose

CommandWhat it does
docker compose up -dCreate and start all services in the background.
docker compose downStop and remove the stack's containers and networks.
docker compose down -vDestructive: Also delete the stack's named volumes.
docker compose psList the stack's containers.
docker compose logs -f serviceFollow one service's logs.
docker compose pullPull newer images for the stack.
docker compose up -d --buildRebuild images and recreate changed services.
docker compose exec service shOpen a shell in a service container.
docker compose configValidate and print the resolved configuration.

Networks

CommandWhat it does
docker network lsList networks.
docker network create nameCreate a network.
docker network inspect nameShow which containers are attached.

Frequently asked questions

How do I update a running container to a new image?

Pull the new image, then recreate the container. With Compose: docker compose pull && docker compose up -d.

Will docker system prune delete my data?

Plain prune keeps volumes. Adding --volumes removes unused volumes, which can contain database data, so only use it when you are sure.

More Containers & DevOps cheat sheets

🌿 Git☸️ kubectl🅰️ Ansible🏗️ TerraformAll Containers & DevOps cheat sheets →
Copied to clipboard