Docker Run to Compose Converter

Turn a docker run command into a docker-compose.yml service definition

Input & Settings

2 spaces

docker-compose.yml

Generated docker-compose.yml will appear here

Analysis & Stats

Conversion statistics will appear here

--name
-p
-v
-e
--restart
--network
-w
-u
--entrypoint
-l
--hostname

About this tool

This tool parses a `docker run` command and emits an equivalent `docker-compose.yml` service definition. It maps the common flags — image, container name, published ports, volumes, environment variables, networks, restart policy, workdir, user, entrypoint, labels and command — to their Compose equivalents. The companion Docker Compose Builder (already in DevTools) goes the other direction visually. Use this when migrating one-off `docker run` invocations into a versioned Compose file. Parsing is client-side; no command is executed.

Examples & Use Cases

Web server with port mapping

# docker run command
docker run -d --name web -p 8080:80 nginx:latest

# docker-compose.yml
services:
  web:
    image: nginx:latest
    container_name: web
    ports:
      - "8080:80"

The container name becomes the service key; -p host:container maps to the ports list.

Database with env + volume

# docker run command
docker run -e POSTGRES_PASSWORD=secret -e POSTGRES_DB=app -v pgdata:/var/lib/postgresql/data postgres:15

# docker-compose.yml
services:
  postgres:
    image: postgres:15
    environment:
      POSTGRES_PASSWORD: secret
      POSTGRES_DB: app
    volumes:
      - pgdata:/var/lib/postgresql/data

Environment flags become a key/value map; named volumes map to the volumes list.

Restart policy + network

# docker run command
docker run --restart unless-stopped --network mynet redis:7

# docker-compose.yml
services:
  redis:
    image: redis:7
    networks:
      - mynet
    restart: unless-stopped

--restart maps to the restart field; --network maps to the networks list.

    Examples

    Convert a basic nginx run command

    Input
    docker run -d --name web -p 8080:80 nginx:latest
    Output
    services:
      web:
        image: nginx:latest
        container_name: web
        ports:
          - "8080:80"
        restart: "no"
        command: []

    The container name, published port mapping, and image tag are all mapped. The -d flag is noted; restart defaults to 'no' since no --restart flag was given.

    Convert a command with volumes and environment

    Input
    docker run -d --name db -v pgdata:/var/lib/postgresql/data -e POSTGRES_PASSWORD=secret --restart always postgres:16
    Output
    services:
      db:
        image: postgres:16
        container_name: db
        environment:
          - POSTGRES_PASSWORD=secret
        volumes:
          - pgdata:/var/lib/postgresql/data
        restart: always
    volumes:
      pgdata:

    The named volume pgdata is detected and the builder adds it to the top-level volumes: block. Bind mounts (./data:/app/data) are left as-is and not added to volumes:.

    About this tool

    Docker Run to Compose Converter takes a docker run command and emits a corresponding docker-compose.yml service definition. It parses the command using a quote-aware tokenizer so complex arguments — those containing spaces, equals signs, or multiple flags — are handled correctly before being mapped to their compose equivalents.

    Mapped flags include --name, -p (publish), -v (volume), -e (environment), --restart, --network, -w (workdir), -u (user), --entrypoint, -l (label), --hostname, and the trailing command. Named volumes are automatically collected into the top-level volumes: block.

    The YAML is generated entirely in your browser using js-yaml — no docker command is executed and nothing is uploaded.

    How to use

    1. Paste a docker run command

      Enter or paste a docker run command into the input. The tokenizer handles quoted arguments and complex flag combinations.

    2. Review the generated YAML

      The output shows a docker-compose.yml service block, with named volumes gathered into the top-level volumes: section automatically.

    3. Copy or download

      Copy the YAML or download docker-compose.yml, then run docker compose up to start the service.

    Use cases

    Migrating a docker run script to Compose

    Take an existing shell script full of docker run commands and convert each one into a Compose service, making the stack reproducible with docker compose up.

    Documenting how a container was started

    Capture the exact flags used to run a container as a Compose definition that can be committed to version control alongside the Dockerfile.

    Quick Compose scaffolding

    Sketch out a Compose service by typing the docker run command you already know, then refine the generated YAML in the editor.

    Common mistakes

    Mistake:Omitting quotes around port mappings with a host port.

    Fix:YAML interprets 8080:80 as the number 42240 (8080 / 0.191). The builder quotes port strings automatically, but if you edit the output, always quote host:container mappings.

    Mistake:Using a bind mount path as a named volume.

    Fix:./data:/app/data is a bind mount (host path). data:/app/data is a named volume. The builder detects leading ./ as a bind mount and omits it from volumes:; named volumes are added automatically.

    Frequently asked questions

    References & standards