Docker cheat sheet
96 one-line examples for Docker. Open it while you code, or print it and keep it near the keyboard. Free, with no signup and no ads.
Container Basics
| Code | What it does |
|---|---|
docker run hello-world | Start a test container to check the install. |
docker run -it ubuntu bash | Open an interactive shell inside a new container. |
docker run -d nginx | Start a container in the background. |
docker run --name web nginx | Give the container a fixed name. |
docker run -p 8080:80 nginx | Map host port 8080 to container port 80. |
docker run --rm alpine echo hi | Delete the container after it stops. |
docker ps | List the containers that run now. |
docker ps -a | List all containers, including stopped ones. |
docker stop web | Stop the container named web. |
docker start web | Start the stopped container named web. |
docker rm web | Remove the stopped container named web. |
docker logs web | Show the output of the container named web. |
Images
| Code | What it does |
|---|---|
docker pull nginx | Download the nginx image from the registry. |
docker images | List the images on your machine. |
docker build -t myapp . | Build an image from the Dockerfile in this folder. |
docker build -t myapp:v1 . | Build an image and add a version tag. |
docker tag myapp myapp:latest | Add a second tag to an image. |
docker push myuser/myapp | Upload your image to Docker Hub. |
docker rmi nginx | Remove the nginx image from the disk. |
docker image prune | Delete images that no container uses. |
docker history myapp | Show the layers of the myapp image. |
docker inspect nginx | Show full details of the nginx image. |
docker save -o app.tar myapp | Save the image to a tar file. |
docker load -i app.tar | Load an image from a tar file. |
Dockerfile
| Code | What it does |
|---|---|
FROM node:20-alpine | Set the base image for the build. |
WORKDIR /app | Set the working folder inside the image. |
COPY package.json . | Copy one file into the image. |
COPY . . | Copy all files from the host to the image. |
RUN npm install | Run a command during the build. |
ENV NODE_ENV=production | Set an environment variable in the image. |
EXPOSE 3000 | Document the port the app uses. |
CMD ["node", "server.js"] | Set the default command for the container. |
ENTRYPOINT ["node"] | Set a fixed command that CMD extends. |
USER node | Run the container as the node user. |
ARG VERSION=1.0 | Define a build-time variable with a default. |
docker build --no-cache -t myapp . | Build the image again without the cache. |
Ports and Volumes
| Code | What it does |
|---|---|
docker run -p 3000:3000 myapp | Publish container port 3000 on the host. |
docker run -P myapp | Publish all exposed ports on random host ports. |
docker port web | Show the port mappings of container web. |
docker volume create data | Create a named volume called data. |
docker volume ls | List all volumes on the machine. |
docker run -v data:/app myapp | Mount the volume data at /app. |
docker run -v $(pwd):/app myapp | Mount the current folder into the container. |
docker run --mount type=bind,src=.,dst=/app myapp | Mount the current folder with the mount flag. |
docker volume inspect data | Show details of the volume named data. |
docker volume rm data | Remove the volume named data. |
docker volume prune | Delete volumes that no container uses. |
docker run -v $(pwd):/app:ro myapp | Mount the folder as read-only. |
Compose
| Code | What it does |
|---|---|
docker compose up | Start all services in the compose file. |
docker compose up -d | Start all services in the background. |
docker compose down | Stop and remove the services and network. |
docker compose ps | List the containers of this project. |
docker compose logs -f | Follow the logs of all services. |
docker compose build | Build the images for all services. |
docker compose restart web | Restart the web service. |
docker compose exec web sh | Open a shell in the running web service. |
docker compose run web npm test | Run a one-time command in a new container. |
docker compose pull | Download the latest images for the services. |
docker compose config | Check and print the final compose file. |
docker compose down -v | Stop services and delete their volumes. |
Inspect and Debug
| Code | What it does |
|---|---|
docker exec -it web sh | Open a shell in the running container. |
docker exec web ls /app | Run one command inside the container. |
docker logs -f web | Follow the live output of the container. |
docker logs --tail 50 web | Show the last 50 log lines. |
docker inspect web | Show full details of the container. |
docker stats | Show live CPU and memory use. |
docker top web | List the processes inside the container. |
docker diff web | Show files changed since the container started. |
docker cp web:/app/log.txt . | Copy a file from the container to the host. |
docker cp ./conf web:/app/conf | Copy a file from the host to the container. |
docker events | Stream the events of the Docker daemon. |
docker inspect -f '{{.State.Pid}}' web | Print one field with a format string. |
Cleanup
| Code | What it does |
|---|---|
docker system df | Show how much disk space Docker uses. |
docker system prune | Delete stopped containers and unused data. |
docker system prune -a | Also delete all unused images. |
docker container prune | Delete all stopped containers. |
docker image prune -a | Delete all images that no container uses. |
docker network prune | Delete networks that no container uses. |
docker rm -f web | Force remove a running container. |
docker rm $(docker ps -aq) | Remove all containers, running or stopped. |
docker rmi $(docker images -q) | Remove all images on the machine. |
docker stop $(docker ps -q) | Stop all running containers. |
docker system prune --volumes | Also delete unused volumes. |
docker builder prune | Delete the build cache. |
Networks and Registry
| Code | What it does |
|---|---|
docker network ls | List the networks on the machine. |
docker network create appnet | Create a user-defined network called appnet. |
docker run --network appnet myapp | Connect the container to the appnet network. |
docker network inspect appnet | Show the containers in the network. |
docker network connect appnet web | Add the running container to the network. |
docker network disconnect appnet web | Remove the container from the network. |
docker network rm appnet | Remove the network called appnet. |
docker login | Log in to Docker Hub. |
docker logout | Log out of Docker Hub. |
docker search nginx | Search Docker Hub for images. |
docker pull myuser/myapp:v1 | Download a tagged image from Docker Hub. |
docker version | Show the client and server versions. |