From docker run to Docker Compose
Docker Compose is a tool for defining and running multi-container Docker applications. Instead of running long docker run commands with many flags, you describe your services in a docker-compose.yml file and manage them all with simple commands.
This guide shows how to convert docker run commands to Docker Compose format.
Basic Conversion Example
A typical docker run command:
docker run -d \
--name my-web-app \
-p 8080:80 \
-e NODE_ENV=production \
-e DATABASE_URL=postgres://localhost:5432/mydb \
-v /host/data:/app/data \
--restart unless-stopped \
my-web-app:latest
The equivalent docker-compose.yml:
version: '3.8'
services:
my-web-app:
image: my-web-app:latest
container_name: my-web-app
ports:
- "8080:80"
environment:
- NODE_ENV=production
- DATABASE_URL=postgres://localhost:5432/mydb
volumes:
- /host/data:/app/data
restart: unless-stopped
Mapping docker run Flags to Compose
| docker run flag | Compose key | Example |
|---|---|---|
--name |
container_name |
container_name: myapp |
-p 8080:80 |
ports |
- "8080:80" |
-e VAR=val |
environment |
- VAR=val |
-v /host:/container |
volumes |
- /host:/container |
--restart |
restart |
restart: unless-stopped |
--network |
networks |
networks: [mynet] |
--link |
depends_on |
depends_on: [db] |
--memory 512m |
mem_limit |
mem_limit: 512m |
--cpus 0.5 |
cpus |
cpus: 0.5 |
--health-cmd |
healthcheck.test |
test: ["CMD", "curl", "-f", "http://localhost"] |
--user |
user |
user: "1000:1000" |
--entrypoint |
entrypoint |
entrypoint: /app/start.sh |
--command |
command |
command: npm start |
Multi-Service Example: Web + Database
version: '3.8'
services:
web:
build: .
ports:
- "3000:3000"
environment:
- DATABASE_URL=postgres://user:pass@db:5432/myapp
depends_on:
db:
condition: service_healthy
restart: unless-stopped
db:
image: postgres:15
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: pass
POSTGRES_DB: myapp
volumes:
- postgres_data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U user"]
interval: 5s
timeout: 5s
retries: 5
restart: unless-stopped
volumes:
postgres_data:
Docker Compose Commands
# Start all services (detached)
docker compose up -d
# Stop all services
docker compose down
# Stop and remove volumes
docker compose down -v
# View logs
docker compose logs -f
# View specific service logs
docker compose logs -f web
# Execute command in running container
docker compose exec web bash
# Rebuild images
docker compose build
# Scale a service
docker compose up -d --scale worker=3
# View running services
docker compose ps
Environment Variables in Compose
Use a .env file (automatically loaded by Compose) for secrets and environment-specific config:
# .env file
POSTGRES_PASSWORD=secretpassword
NODE_ENV=production
API_KEY=abc123
# docker-compose.yml
services:
web:
environment:
- NODE_ENV=${NODE_ENV}
- API_KEY=${API_KEY}
db:
environment:
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
The .env file should be in .gitignore. Share a .env.example file instead with placeholder values.
Restart Policies
| Policy | Behavior |
|---|---|
no |
Never restart (default) |
always |
Always restart, including on Docker daemon restart |
on-failure |
Only restart on non-zero exit code |
unless-stopped |
Always restart unless manually stopped |
Using This Tool
Paste a docker run command to instantly get the equivalent Docker Compose YAML. The tool parses all flags and options and generates a properly formatted Compose service definition.
-> Try the Docker Run to Compose Converter