Docker Cheatsheet — Commands & Quick Reference
DodaTech
3 min read
In this tutorial, you'll learn about Docker Cheatsheet. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
Docker CLI commands, Dockerfile syntax, Docker Compose, volumes, networks, and best practices — a dense reference for containerizing and deploying applications.
Docker CLI — Container Lifecycle
| Command | What it does |
|---|---|
docker run -d -p 8080:80 nginx |
Run container detached, map port |
docker run -it ubuntu bash |
Interactive shell |
docker ps |
List running containers |
docker ps -a |
List all containers |
docker stop <id> |
Stop container |
docker start <id> |
Start stopped container |
docker rm <id> |
Remove container |
docker rm $(docker ps -aq) |
Remove all containers |
Images
| Command | What it does |
|---|---|
docker images |
List local images |
docker pull nginx:alpine |
Pull image from registry |
docker rmi <image> |
Remove image |
docker build -t myapp:1.0 . |
Build image from Dockerfile |
docker tag myapp:1.0 user/myapp:1.0 |
Tag image |
docker push user/myapp:1.0 |
Push to registry |
Exec & Logs
docker exec -it <container> bash # shell into running container
docker logs -f <container> # tail logs
docker logs --tail 50 <container> # last 50 lines
Dockerfile Instructions
| Instruction | Purpose |
|---|---|
FROM python:3.11-slim |
Base image |
WORKDIR /app |
Set working dir |
COPY . . |
Copy files from context |
ADD archive.tar.gz /tmp |
Copy + auto-extract |
RUN pip install -r req.txt |
Execute command at build |
CMD ["python", "app.py"] |
Default command (runtime) |
ENTRYPOINT ["python"] |
Entry point (runtime) |
EXPOSE 8080 |
Document port |
ENV MODE=production |
Set env variable |
ARG VERSION=1.0 |
Build-time variable |
VOLUME /data |
Mount point declaration |
USER appuser |
Switch user |
Example Dockerfile:
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
EXPOSE 3000
CMD ["node", "server.js"]
Docker Compose
# docker-compose.yml
version: "3.9"
services:
web:
build: .
ports:
- "3000:3000"
volumes:
- .:/app
environment:
- NODE_ENV=development
db:
image: postgres:16
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata:
Compose commands:
| Command | What it does |
|---|---|
<a href="/devops/docker-compose/">Docker Compose</a> up -d |
Start services |
<a href="/devops/docker-compose/">Docker Compose</a> down |
Stop + remove |
<a href="/devops/docker-compose/">Docker Compose</a> logs -f |
Tail logs |
<a href="/devops/docker-compose/">Docker Compose</a> build |
Rebuild images |
<a href="/devops/docker-compose/">Docker Compose</a> exec web bash |
Shell into service |
Volumes & Networks
docker volume create myvol # create volume
docker volume ls # list volumes
docker network create mynet # create network
docker network ls # list networks
# Attach container:
docker run -d --net=mynet --name app myapp
Volume types: bind (host path), volume (managed by Docker), tmpfs (in-memory).
See the full Docker tutorials for CI/CD and Orchestration.
← Previous
Git Cheatsheet — Common Commands Quick Reference
Next →
SQL Cheatsheet — Query Quick Reference
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro