Skip to main content

Command Palette

Search for a command to run...

Docker Revision ( 1 Page)

Updated
โ€ข2 min read

๐Ÿณ Docker Cheatsheet โ€“ Quick Revision

๐Ÿ”น Basics

  • What is Docker?
    Platform to build, ship, and run applications in containers (lightweight, isolated environments).

  • Key Components:

    • Image โ†’ Blueprint (read-only).

    • Container โ†’ Running instance of an image.

    • Dockerfile โ†’ Instructions to build an image.

    • Registry โ†’ Store/distribute images (e.g., Docker Hub).


๐Ÿ”น Common Commands

ActionCommand
Check versiondocker --version
List imagesdocker images
List containers (running/all)docker ps / docker ps -a
Pull imagedocker pull nginx:latest
Run containerdocker run -d -p 8080:80 nginx
Stop containerdocker stop <container_id>
Remove containerdocker rm <container_id>
Remove imagedocker rmi <image_id>
Logsdocker logs <container_id>
Exec inside containerdocker exec -it <id> bash

๐Ÿ”น Dockerfile Essentials

FROM ubuntu:20.04        # Base image
WORKDIR /app             # Set working dir
COPY . /app              # Copy files
RUN apt-get update && apt-get install -y python3
CMD ["python3", "app.py"] # Default command

๐Ÿ‘‰ Build: docker build -t myapp .
๐Ÿ‘‰ Run: docker run -d myapp


๐Ÿ”น Networking & Volumes

  • Ports: docker run -p 8080:80 nginx โ†’ Host:8080 โ†’ Container:80

  • Volumes (persistent data):

    • docker run -v /host/data:/container/data mysql
  • Read-only volume:

    • docker run -v /host/data:/container/data:ro nginx

๐Ÿ”น Docker Compose

  • Define multi-container apps (docker-compose.yml):
version: '3'
services:
  web:
    image: nginx
    ports:
      - "8080:80"
  db:
    image: mysql
    environment:
      MYSQL_ROOT_PASSWORD: root

๐Ÿ‘‰ Run: docker-compose up -d


๐Ÿ”น Best Practices

  • Use .dockerignore to avoid copying unnecessary files.

  • Use multi-stage builds for smaller images.

  • Keep containers stateless โ†’ use volumes for data.

  • Tag images (myapp:v1) instead of using latest.


โšก๏ธ Quick Tip: Containers are ephemeral. Always use volumes for persistence.