Docker for Developers
Complete guide to Docker including Dockerfile creation, Docker Compose orchestration, and CLI commands for containerized applications
Welcome to the Docker for Developers documentation! This comprehensive guide covers everything from creating Dockerfiles to orchestrating multi-container applications with Docker Compose.
What You'll Learn
This documentation covers:
- Dockerfile: Building optimized container images with best practices
- Docker Compose: Orchestrating multi-container applications
- CLI Commands: Essential Docker commands for daily workflows
- Networking: Container communication and network configuration
- Volumes: Data persistence and volume management
- Security: Best practices for secure container deployments
Docker Architecture
Prerequisites
Before diving in, you should have:
- Docker Desktop or Docker Engine installed
- Basic command line familiarity
- Understanding of Linux fundamentals
Installation
Install Docker from docker.com or use your package manager.
Quick Navigation
Dockerfile
Build optimized container images
Docker Compose
Orchestrate multi-container apps
CLI Commands
Essential Docker commands
Topic Overview
Dockerfile
Master container image creation:
- Base Images: Choosing the right foundation
- Instructions: FROM, RUN, COPY, CMD, ENTRYPOINT
- Multi-stage Builds: Optimizing image size
- Layer Caching: Speeding up builds
- Best Practices: Security and optimization
Docker Compose
Learn multi-container orchestration:
- Services: Defining application components
- Networks: Container communication
- Volumes: Data persistence
- Environment Variables: Configuration management
- Profiles: Environment-specific setups
CLI Commands
Essential commands for daily workflows:
- Container Management: run, start, stop, rm
- Image Management: build, pull, push, tag
- Inspection: logs, exec, inspect
- Cleanup: prune, system df
Docker Workflow
Quick Start
Verify Installation
# Check Docker version
docker --version
# Check Docker Compose version
docker compose version
# Verify Docker is running
docker infoRun Your First Container
# Run a simple container
docker run hello-world
# Run an interactive container
docker run -it ubuntu bash
# Run a web server
docker run -d -p 8080:80 nginxBuild Your First Image
# Dockerfile
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]# Build the image
docker build -t my-app .
# Run the container
docker run -d -p 3000:3000 my-appContainer Lifecycle
Key Concepts
Images are read-only templates used to create containers.
# List images
docker images
# Pull an image
docker pull nginx:alpine
# Remove an image
docker rmi nginx:alpine
# Build an image
docker build -t my-app:latest .Images are built in layers, with each instruction in a Dockerfile creating a new layer.
Containers are runnable instances of images.
# List running containers
docker ps
# List all containers
docker ps -a
# Start a container
docker start container_name
# Stop a container
docker stop container_name
# Remove a container
docker rm container_nameContainers are isolated processes that share the host OS kernel.
Volumes provide persistent storage for containers.
# Create a volume
docker volume create my-data
# List volumes
docker volume ls
# Mount a volume
docker run -v my-data:/app/data my-app
# Bind mount
docker run -v $(pwd):/app my-appVolumes persist data even when containers are removed.
Networks enable container communication.
# List networks
docker network ls
# Create a network
docker network create my-network
# Connect container to network
docker network connect my-network container_name
# Run container on specific network
docker run --network my-network my-appDocker provides bridge, host, and overlay network drivers.
Best Practices Summary
Production Considerations
Always follow these best practices when deploying to production.
| Category | Best Practice |
|---|---|
| Images | Use specific tags, not latest |
| Security | Run as non-root user |
| Size | Use multi-stage builds |
| Caching | Order Dockerfile instructions wisely |
| Compose | Use environment files for secrets |
| Volumes | Named volumes for persistent data |
| Networks | Custom networks for isolation |
| Cleanup | Regular pruning of unused resources |