Advanced Container Techniques
Multi-Container Orchestration
Docker Compose enables complex application architectures through declarative configuration of interconnected containers.
graph TD
A[Web Container] --> B[Database Container]
B --> C[Cache Container]
C --> D[Monitoring Container]
Docker Compose Configuration
version: '3.8'
services:
web:
image: nginx:latest
ports:
- "80:80"
database:
image: postgres:13
environment:
POSTGRES_PASSWORD: secure_password
cache:
image: redis:alpine
Container Network Management
Network Type |
Description |
Use Case |
Bridge |
Default internal network |
Container-to-container communication |
Host |
Direct host network access |
High-performance scenarios |
Overlay |
Multi-host networking |
Distributed systems |
Advanced Networking
## Create custom network
docker network create --driver bridge my_custom_network
## Connect container to network
docker network connect my_custom_network container_name
## Inspect network details
docker network inspect my_custom_network
Resource Constraint Management
## Run container with CPU and memory limits
docker run -it \
--cpus=2 \
--memory=4g \
--memory-reservation=2g \
ubuntu:latest
Container Health Monitoring
HEALTHCHECK --interval=5m --timeout=3s \
CMD curl -f || exit 1
Data Persistence Strategies
## Create named volume
docker volume create app_data
## Mount volume to container
docker run -v app_data:/var/lib/data image_name
Container Logging Mechanisms
## View container logs
docker logs container_name
## Follow log output
docker logs -f container_name
## Limit log size
docker run --log-driver json-file \
--log-opt max-size=10m \
--log-opt max-file=3 \
image_name
Security Enhancements
## Run container as non-root user
docker run --user 1000:1000 image_name
## Disable container privilege escalation
docker run --security-opt no-new-privileges image_name