Docker Cheat Sheet
Last updated
Images
An image is the read-only template a container starts from.
| What it does | Command |
|---|---|
| Download an image | docker pull nginx |
| Download a specific tag | docker pull node:22-alpine |
| List local images | docker images |
| List image IDs only | docker images -q |
| Build an image from the Dockerfile here | docker build -t myapp . |
| Add another name and tag to an image | docker tag myapp myuser/myapp:1.0 |
| Show the layers an image was built from | docker history myapp |
| Show an image's full details as JSON | docker image inspect myapp |
| Remove an image | docker rmi myapp |
| Remove images no container uses | docker image prune -a |
| Search Docker Hub | docker search redis |
| Save an image to a tar file | docker save -o myapp.tar myapp |
| Load an image from a tar file | docker load -i myapp.tar |
Containers
A container is a running (or stopped) instance of an image.
| What it does | Command |
|---|---|
| Start a container from an image | docker run nginx |
| Start one in the background with a name | docker run -d --name web nginx |
| List running containers | docker ps |
| List all containers, stopped ones too | docker ps -a |
| Stop a container | docker stop web |
| Start a stopped container | docker start web |
| Restart a container | docker restart web |
| Remove a stopped container | docker rm web |
| Stop and remove in one step | docker rm -f web |
| Open a shell in a running container | docker exec -it web sh |
| Run one command in a running container | docker exec web ls /usr/share/nginx/html |
| Show a container's logs | docker logs web |
| Follow the logs as they come | docker logs -f web |
| Show the last 100 lines of the logs | docker logs --tail 100 web |
| Copy a file out of a container | docker cp web:/etc/nginx/nginx.conf . |
| Copy a file into a container | docker cp ./index.html web:/usr/share/nginx/html/ |
| Show the processes running in a container | docker top web |
| Show live CPU and memory use | docker stats |
| Show which host ports map to a container | docker port web |
| Rename a container | docker rename web web-old |
docker run options
The flags you combine on docker run. Options go before the image name; anything after it is the command to run.
| What it does | Command |
|---|---|
| Run in the background (detached) | docker run -d nginx |
| Give the container a name | docker run --name web nginx |
| Publish port 80 as port 8080 on the host | docker run -p 8080:80 nginx |
| Publish every exposed port on a random host port | docker run -P nginx |
| Open an interactive shell | docker run -it ubuntu bash |
| Remove the container when it exits | docker run --rm alpine echo hello |
| Set an environment variable | docker run -e NODE_ENV=production myapp |
| Read environment variables from a file | docker run --env-file .env myapp |
| Mount a named volume | docker run -v data:/var/lib/postgresql/data postgres |
| Mount the current folder (bind mount) | docker run -v "$(pwd)":/app myapp |
| Mount a folder read-only | docker run -v "$(pwd)":/app:ro myapp |
| Set the working directory | docker run -w /app node:22 npm test |
| Join a network | docker run --network mynet myapp |
| Restart unless stopped by hand | docker run -d --restart unless-stopped nginx |
| Limit memory and CPU | docker run -m 512m --cpus 1.5 myapp |
| Override the entrypoint | docker run --entrypoint sh -it myapp |
| Run as another user | docker run -u 1000:1000 myapp |
Building images
| What it does | Command |
|---|---|
| Build and tag from the Dockerfile here | docker build -t myapp:1.0 . |
| Build with a Dockerfile under another name | docker build -f Dockerfile.dev -t myapp:dev . |
| Build without the layer cache | docker build --no-cache -t myapp . |
Pass a build argument (an ARG) | docker build --build-arg VERSION=1.2 -t myapp . |
| Build only one stage of a multi-stage file | docker build --target build -t myapp:build . |
| Build for another CPU architecture | docker build --platform linux/amd64 -t myapp . |
| Show every step's full output | docker build --progress=plain -t myapp . |
| Build for two architectures and push | docker buildx build --platform linux/amd64,linux/arm64 -t myuser/myapp:1.0 --push . |
Dockerfile instructions
A Dockerfile lists the steps that build an image, one instruction per line. Files to leave out of the build go in a .dockerignore file.
| What it does | Instruction |
|---|---|
| The base image to start from | FROM node:22-alpine |
| Name a stage of a multi-stage build | FROM node:22 AS build |
| Set the working directory for later steps | WORKDIR /app |
| Copy files from the build context | COPY package*.json ./ |
| Copy files from an earlier stage | COPY --from=build /app/dist ./dist |
| Copy, also unpacking archives and fetching URLs | ADD app.tar.gz /opt/ |
| Run a command while building | RUN npm ci |
| Set an environment variable | ENV NODE_ENV=production |
| Declare a build argument | ARG VERSION=1.0 |
| Document the port the app listens on | EXPOSE 3000 |
| The default command when the container starts | CMD ["node", "server.js"] |
The fixed program to run (CMD becomes its arguments) | ENTRYPOINT ["python", "app.py"] |
| Run the next steps as another user | USER node |
| Mark a folder as a volume | VOLUME /data |
| Check that the app is healthy | HEALTHCHECK CMD curl -f http://localhost:3000/ || exit 1 |
| Add metadata | LABEL org.opencontainers.image.source="https://github.com/me/myapp" |
Volumes
Volumes keep data when a container is removed.
| What it does | Command |
|---|---|
| Create a volume | docker volume create data |
| List volumes | docker volume ls |
| Show where a volume lives | docker volume inspect data |
| Remove a volume | docker volume rm data |
| Remove volumes no container uses | docker volume prune |
Networks
Containers on the same user-defined network reach each other by container name.
| What it does | Command |
|---|---|
| Create a network | docker network create mynet |
| List networks | docker network ls |
| Show a network and its containers | docker network inspect mynet |
| Connect a running container | docker network connect mynet web |
| Disconnect a container | docker network disconnect mynet web |
| Remove a network | docker network rm mynet |
| Remove networks no container uses | docker network prune |
Docker Compose
Compose runs several containers described in a compose.yaml (or docker-compose.yml) file in the current folder.
| What it does | Command |
|---|---|
| Start every service in the background | docker compose up -d |
| Rebuild the images, then start | docker compose up -d --build |
| Stop and remove the containers and networks | docker compose down |
| Also remove the volumes | docker compose down -v |
| List the project's containers | docker compose ps |
| Follow the logs of every service | docker compose logs -f |
| Follow one service's logs | docker compose logs -f web |
| Open a shell in a running service | docker compose exec web sh |
| Run a one-off command in a new container | docker compose run --rm web npm test |
| Build or rebuild the images | docker compose build |
| Pull the latest images | docker compose pull |
| Restart the services | docker compose restart |
| Stop without removing | docker compose stop |
| Show the merged, final configuration | docker compose config |
| Use another file | docker compose -f compose.prod.yaml up -d |
Registries
Share images through Docker Hub or another registry.
| What it does | Command |
|---|---|
| Log in to Docker Hub | docker login |
| Log in to another registry | docker login ghcr.io |
| Tag an image with your account name | docker tag myapp myuser/myapp:1.0 |
| Upload an image | docker push myuser/myapp:1.0 |
| Log out | docker logout |
Inspecting and debugging
| What it does | Command |
|---|---|
| Show a container's full details as JSON | docker inspect web |
| Print one field from the details | docker inspect -f '{{.State.Status}}' web |
| Print a container's IP address | docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' web |
| Show the files changed inside a container | docker diff web |
| Take one snapshot of resource use | docker stats --no-stream |
| Watch events from the Docker daemon | docker events |
| Show client and server versions | docker version |
| Show system-wide information | docker info |
Cleanup
Docker keeps stopped containers, old images and build cache until you remove them.
| What it does | Command |
|---|---|
| Show disk space used by Docker | docker system df |
| Remove stopped containers | docker container prune |
| Remove dangling images | docker image prune |
| Remove every unused image | docker image prune -a |
| Remove the build cache | docker builder prune |
| Remove stopped containers, unused networks, dangling images and cache | docker system prune |
| Remove everything unused, volumes included | docker system prune -a --volumes |
| Stop every running container | docker stop $(docker ps -q) |
| Remove every container | docker rm -f $(docker ps -aq) |
Every Docker command you reach for, in copy-paste tables: images and containers first, then the docker run flags, building, Dockerfile instructions, volumes, networks, Docker Compose, registries and cleanup.
The commands are for the current Docker CLI, where Compose is the docker compose plugin (older installs use docker-compose with a hyphen). They work the same on Linux, macOS and Windows. To try them without installing anything, open the terminal playground, which has a Docker simulator built in.
Docker cheat sheet FAQ
What is the difference between an image and a container?
docker build and docker pull give you images; docker run starts a container from one. You can run many containers from the same image.What is the difference between CMD and ENTRYPOINT?
CMD is the default command, and anything you type after the image name in docker run replaces it. ENTRYPOINT is the fixed program, and docker run arguments are added to it instead (CMD then supplies its default arguments). Use CMD for most apps and ENTRYPOINT when the image is a single tool.What is the difference between COPY and ADD?
ADD also unpacks a local tar archive and can download a URL. Use COPY unless you need one of those, because its behaviour is plainer.Is it docker compose or docker-compose?
docker compose (with a space) is Compose V2, a plugin of the Docker CLI and the one installed today. docker-compose (with a hyphen) is the older standalone program, no longer updated. The subcommands are the same: up, down, ps, logs, exec.How do I remove all stopped containers and unused images?
docker system prune removes stopped containers, unused networks, dangling images and the build cache. Add -a to also remove every image no container uses, and --volumes to remove unused volumes, which deletes their data.How do I get a shell inside a container?
docker exec -it web sh (or bash if the image has it). To start a new container with a shell instead, docker run -it --rm ubuntu bash.Can I practice Docker commands online?
docker build, docker run, docker ps, volumes, networks and more answer as the real CLI does, with nothing to install. It runs a set of common tools inside containers rather than each image's own programs, so for a full Docker install use Docker Desktop or Docker Engine on your machine.Is this Docker cheat sheet free?
Yes. It is free to read, copy and print, with no sign-up.