Essential Docker commands and concepts for containerized application development. Master the fundamentals of Docker containers, images, and orchestration.
🐳 Core Concepts
- Image: Lightweight, standalone, executable package containing code, runtime, system tools, and OS
- Container: Runnable instance of an image, representing a specific execution environment
- Volume: Persistent data storage mechanism, ensuring data durability even if containers are stopped or removed
- Network: Facilitates connectivity between Docker containers
🔧 Essential Commands
Container Operations
docker build -t <image_name>
: Build a Docker imagedocker images
: View Docker imagesdocker ps
: View running containersdocker ps -a
: View all containersdocker prune
: Remove all idle containersdocker rm <container_id>
: Delete a container
Running Containers
docker run -p <host_port>:<container_port> <image_name>
: Run a containerdocker run -p <host_port>:<container_port> -v "$(pwd):/app" -v /app/node_modules <image_name>
: Run a container with live code updates
📄 Dockerfile Example
FROM node:20-alpine
WORKDIR /app
COPY . .
CMD node hello.js
🎼 Docker Compose
Docker Compose simplifies multi-container Docker applications:
- Define services in a YAML file (
docker-compose.yaml
) - Use
docker compose up
to start services defined in the configuration - Use
docker compose watch
for development, syncing changes and rebuilding containers as necessary
Docker Compose Configuration
services:
web:
build:
context: .
ports:
- 5173:5173
volumes:
- .:/app
- /app/node_modules
develop:
watch:
- path: ./frontend/package.json
action: rebuild
- path: ./frontend/package-lock.json
action: rebuild
- path: ./frontend
target: /app
action: sync
🔄 Docker Compose Watch Mode
Enables live development and testing by automatically syncing changes, rebuilding, and restarting containers:
docker compose watch
💡 Best Practices
- Use
.dockerignore
to exclude unnecessary files - Keep images lightweight with multi-stage builds
- Use specific version tags instead of
latest
- Regularly clean up unused containers and images