All References

Docker Cheatsheet

Quick reference for Docker CLI commands — containers, images, volumes, networks, Docker Compose, and Dockerfile instructions.

Containers

Command Description
docker run -d -p 8080:80 nginxRun a container in background, map port 8080→80
docker run --rm -it ubuntu bashRun interactively, auto-remove on exit
docker run --name myapp -e ENV=prod imageSet container name and env variable
docker psList running containers
docker ps -aList all containers including stopped
docker start <id>Start a stopped container
docker stop <id>Gracefully stop container (SIGTERM)
docker restart <id>Stop and start a container
docker rm <id>Remove a stopped container
docker rm -f <id>Force-remove a running container
docker container pruneRemove all stopped containers
docker exec -it <id> bashOpen interactive shell in running container
docker logs <id>Show container logs
docker logs -f <id>Follow (tail) container logs
docker inspect <id>Show detailed container/image info as JSON
docker cp <id>:/path ./localCopy file from container to host
docker statsLive resource usage stats for all containers

Images

Command Description
docker imagesList local images
docker pull nginx:latestPull image from registry
docker push user/image:tagPush image to registry
docker build -t myapp:1.0 .Build image from Dockerfile in current dir
docker build --no-cache -t myapp .Build without layer cache
docker tag image:old user/image:newTag/rename an image
docker rmi <image>Remove a local image
docker image prune -aRemove all unused images
docker history <image>Show image layer history
docker save -o image.tar myappExport image to tar archive
docker load -i image.tarImport image from tar archive

Volumes & Networks

Command Description
docker volume create mydataCreate a named volume
docker volume lsList volumes
docker volume rm mydataRemove a volume
docker run -v mydata:/data imageMount named volume into container
docker run -v $(pwd):/app imageBind mount current directory
docker network create mynetCreate a user-defined network
docker network lsList networks
docker run --network mynet imageAttach container to a network

Docker Compose

Command Description
docker compose upStart all services defined in compose.yaml
docker compose up -dStart services in detached mode
docker compose downStop and remove containers and networks
docker compose down -vAlso remove volumes
docker compose psList compose services and status
docker compose logs -fFollow logs from all services
docker compose exec web bashOpen shell in the "web" service
docker compose buildBuild or rebuild services
docker compose pullPull latest images for all services
docker compose restart webRestart specific service

Dockerfile Instructions

Instruction Description & Example
FROMBase image. FROM node:20-alpine
RUNExecute command in a new layer. RUN npm install
COPYCopy files from build context. COPY . /app
ADDLike COPY but also supports URLs and auto-extracts tar files
ENVSet environment variable. ENV NODE_ENV=production
ARGBuild-time variable (not persisted). ARG VERSION=1.0
WORKDIRSet working directory. WORKDIR /app
EXPOSEDocument that container listens on port. EXPOSE 3000
CMDDefault command (overridable). CMD ["node", "server.js"]
ENTRYPOINTFixed executable; CMD becomes arguments. ENTRYPOINT ["node"]
VOLUMECreate mount point. VOLUME ["/data"]
HEALTHCHECKHEALTHCHECK CMD curl -f http://localhost/ || exit 1
USERSet user for subsequent instructions. USER node

Frequently Asked Questions

What is the difference between CMD and ENTRYPOINT in Docker?

CMD specifies the default command to run when a container starts, but it can be fully overridden by passing a command to docker run. ENTRYPOINT sets the fixed executable that always runs; anything passed to docker run is appended as arguments to the entrypoint. The common pattern is to use ENTRYPOINT for the executable and CMD for default arguments: ENTRYPOINT ["node"] + CMD ["app.js"].

How do I remove all stopped containers in Docker?

Run docker container prune to remove all stopped containers in one command (it will prompt for confirmation). Alternatively, docker system prune removes stopped containers, dangling images, unused networks, and build cache. To also remove all unused images (not just dangling ones), use docker system prune -a.

What is the difference between docker compose up and docker compose start?

docker compose up creates and starts containers for services defined in your compose file. If the containers don't exist yet, it creates them. It also rebuilds images if needed (with --build). docker compose start only starts existing containers — it does not create new containers or networks. Use up when starting fresh or applying configuration changes; use start only to resume stopped containers.