Skip to content

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).

What is the difference between CMD and ENTRYPOINT?

CMD provides defaults for the running container and can be overridden by command-line arguments. ENTRYPOINT sets the main executable that always runs — arguments passed via CLI get appended. They're often combined: ENTRYPOINT ["python"] + CMD ["app.py"] allows docker run myimage other.py to run other.py instead.

What is the difference between COPY and ADD in a Dockerfile?

COPY copies files from the build context into the image — it's simple and explicit. ADD does the same but also supports remote URLs and automatic tar extraction. The Docker docs recommend using COPY unless you specifically need ADD's extra features.

How do I persist data in Docker?

Use volumes (managed by Docker, stored in /var/lib/docker/volumes/) or bind mounts (maps a host directory). Volumes are preferred for production as they're portable and safer. Declare them with docker volume create or volumes: in Compose

See the full Docker tutorials for CI/CD and Orchestration.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro