Docker Commands Cheat Sheet
Your go-to reference for Docker CLI, Compose, and Dockerfile best practices.
Image Management
docker build -t app-name:latest .
-t tag -f specify file --no-cache ignore cachedocker pull nginx:alpine
docker images
docker rmi image_id
docker image prune -a
Container Lifecycle
docker run -d -p 8080:80 --name myapp nginx
-d detached -p host:container port -v volumedocker ps -a
-a include stopped containersdocker stop container_id
docker rm container_id
-f force removal (even if running)Interacting & Executing
docker exec -it container_id /bin/sh
-it interactive ttydocker logs -f container_id
-f follow --tail 100 last 100 linesdocker top container_id
docker inspect container_id
Docker Compose
docker compose up -d
docker compose down
-v also remove named volumesdocker compose logs -f
docker compose build --no-cache
Networks & Volumes
docker network ls
docker network create my_net
docker volume ls
docker volume create my_data
docker system prune -a --volumes
Dockerfile Basics
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
COPY . .
RUN npm install --production
CMD ["node", "app.js"]
Why Use This Docker Cheat Sheet?
Docker has fundamentally changed how developers build, ship, and run applications. However, remembering every command, flag, and configuration option in the Docker CLI can be challenging. This interactive reference guide is designed to provide quick, filterable access to the most common Docker commands and Dockerfile patterns.
Best Practices for Dockerfiles
- Use Official Base Images: Start with official, minimal images like
alpineor slim variants of Node/Python to reduce attack surface and download size. - Leverage Build Cache: Order your
Dockerfilecommands from least likely to change (like copying dependency manifests) to most likely to change (like source code). - Multi-stage Builds: Use multi-stage builds to compile code in one container and copy only the final artifacts into a smaller runtime container.
- Don't Run as Root: Define a non-root user (e.g.,
USER node) for better security and isolation.
Understanding Container Lifecycle
A typical Docker workflow involves building an image (docker build), running a container instance from that image (docker run), viewing its outputs (docker logs), and eventually stopping and removing it (docker stop and docker rm). For multi-container applications, Docker Compose (docker compose up) simplifies orchestration by defining the entire stack in a single YAML file.