Docker Commands Cheat Sheet

Complete Docker quick reference — images, containers, networking, volumes, Docker Compose, and common Dockerfile instructions.

Images Containers Networking Volumes Compose Dockerfile

Images

docker pull image:tag Pull image from registry
docker images List local images
docker build -t name:tag . Build image from Dockerfile in current dir
docker build --no-cache -t name . Build without cache
docker tag src dst Tag image with new name
docker push name:tag Push image to registry
docker rmi image Remove image
docker image prune -a Remove all unused images
docker inspect image Show detailed image metadata

Containers

docker run -d -p 8080:80 --name web nginx Run detached, map port, name container
docker run -it --rm image bash Interactive shell, auto-remove on exit
docker run -e KEY=val image Set environment variable
docker ps List running containers
docker ps -a List all containers including stopped
docker logs -f container Follow container logs
docker exec -it container bash Interactive shell inside running container
docker stop / start / restart container Manage container lifecycle
docker rm -f container Force remove container
docker stats Live CPU/memory/network stats
docker cp src container:/path Copy files between host and container

Networking

docker network ls List networks
docker network create mynet Create custom bridge network
docker run --network mynet image Connect container to network at run
docker network connect mynet container Connect running container to network
docker network inspect mynet Show network details and connected containers

Volumes

docker run -v vol:/app/data image Mount named volume
docker run -v /host/path:/container/path image Bind mount host directory
docker volume ls List all volumes
docker volume create myvol Create named volume
docker volume prune Remove unused volumes

Docker Compose

docker compose up -d Start all services in background
docker compose up --build Rebuild images before starting
docker compose down Stop and remove containers & networks
docker compose down -v Also remove named volumes
docker compose ps Service status
docker compose logs -f service Follow logs of a specific service
docker compose exec service sh Shell into running service
docker compose run --rm service cmd Run one-off command in new container

Dockerfile Instructions

FROM node:20-alpine Base image (always first; use minimal variants)
WORKDIR /app Set working directory (creates if needed)
COPY package*.json ./ Copy files (copy deps first for cache efficiency)
RUN npm ci Execute command (creates new layer)
ENV NODE_ENV=production Set environment variable
ARG VERSION=1.0 Build-time argument (not available at runtime)
EXPOSE 3000 Document intended port (doesn't publish)
USER node Run as non-root user (security best practice)
CMD ["node", "server.js"] Default command (overridable at run time)
ENTRYPOINT ["docker-entrypoint.sh"] Fixed executable (CMD becomes its arguments)

More Cheat Sheets