正在加载,请稍候…

Convert docker run to docker-compose: A Practical Guide

Convert docker run commands to docker-compose.yml. Understand volumes, ports, environment variables.

What Is Docker Compose?

Docker Compose is a tool for defining and running multi-container Docker applications. Using a YAML configuration file (docker-compose.yml), you can configure all your application's services, networks, and volumes in one place, then start everything with a single command: docker compose up.

docker run vs. docker-compose.yml

When working with Docker, you often start by building commands manually using docker run. As applications grow more complex, these commands become unwieldy. Docker Compose provides a declarative alternative.

The docker run Command

docker run -d \
  --name postgres \
  -e POSTGRES_USER=myuser \
  -e POSTGRES_PASSWORD=mypassword \
  -e POSTGRES_DB=mydb \
  -p 5432:5432 \
  -v postgres_data:/var/lib/postgresql/data \
  --restart unless-stopped \
  postgres:15

The Equivalent docker-compose.yml

version: '3.8'
services:
  postgres:
    image: postgres:15
    container_name: postgres
    environment:
      POSTGRES_USER: myuser
      POSTGRES_PASSWORD: mypassword
      POSTGRES_DB: mydb
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
    restart: unless-stopped

volumes:
  postgres_data:

Key Docker Compose Concepts

Services

Each container in your application is defined as a service. Services can depend on each other, share networks, and communicate by service name.

Networks

By default, Compose creates a single network for your app. All services can communicate using service names as hostnames. You can define multiple networks to isolate services.

Volumes

Named volumes persist data beyond container lifecycle. Bind mounts map host directories into containers for development.

Environment Variables

Can be defined inline, loaded from .env files, or passed through the host environment.

Multi-Service Application Example

A full web application stack:

version: '3.8'

services:
  nginx:
    image: nginx:alpine
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
    depends_on:
      - app

  app:
    build: .
    environment:
      DATABASE_URL: postgresql://user:pass@db:5432/mydb
      REDIS_URL: redis://redis:6379
    depends_on:
      - db
      - redis

  db:
    image: postgres:15
    environment:
      POSTGRES_PASSWORD: pass
    volumes:
      - db_data:/var/lib/postgresql/data

  redis:
    image: redis:7-alpine

volumes:
  db_data:

Common docker run Flags and Their Compose Equivalents

docker run flag Compose key
-d (background by default)
--name container_name
-e VAR=val environment: VAR: val
-p 80:80 ports: - "80:80"
-v vol:/path volumes: - vol:/path
--restart restart: unless-stopped
--network networks:
--link depends_on: (use service names instead)

Docker Compose Commands

docker compose up -d          # Start all services in background
docker compose down           # Stop and remove containers
docker compose logs -f        # Follow service logs
docker compose ps             # List running services
docker compose exec app bash  # Shell into a service
docker compose build          # Rebuild service images
docker compose pull           # Pull latest images

Using the Docker Run to Compose Converter

Our tool:

  1. Paste a docker run command — handles all common flags
  2. Generate docker-compose.yml — proper YAML structure
  3. Multi-command support — convert multiple run commands to a complete compose file
  4. Network inference — suggests network configurations based on linked containers
  5. Copy YAML — ready to save as docker-compose.yml

Use it when migrating from manual Docker commands to a managed Compose workflow, documenting existing container configurations, or sharing service configurations with teammates.