Why Migrate from Imperative `docker run` to Declarative Compose Stacks
Managing containers via CLI `docker run` commands becomes error-prone as flags accumulate for port mappings, volume mounts, environment secrets, and restart policies. Docker Compose provides a declarative YAML specification that can be version-controlled in Git, reproduced identically across staging and production environments, and orchestrated with single commands like `docker compose up -d`.
# Imperative CLI Command:
docker run -d --name web_gateway -p 8080:80 -v /etc/nginx/nginx.conf:/etc/nginx/nginx.conf:ro -e ENV=production --restart unless-stopped nginx:alpine
# Equivalent Declarative docker-compose.yml:
services:
web_gateway:
image: nginx:alpine
container_name: web_gateway
ports:
- "8080:80"
volumes:
- /etc/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
environment:
- ENV=production
restart: unless-stopped