Convert docker run to Docker Compose
This docker run to Docker Compose converter reads the flags of one or more docker run commands and writes the matching services: entries in a compose.yaml file. It is useful when a README only shows long run commands, when you want to version your container setup in Git, or when several containers that you start by hand should come up together with one docker compose up.
Why move from docker run to Compose?
A docker run command is easy to paste but hard to maintain: flags get lost in shell history and it is unclear which containers belong together. A Compose file describes the same settings declaratively. Compose also creates a default network, so services reach each other by service name, and it manages named volumes for you.
Example
Input:
docker run --rm -it -p 3000:3000 --network appnet --network-alias api \
--env-file .env -m 512m --cpus 1.5 myorg/api:2.1 npm startOutput:
# --rm has no Compose equivalent (use `docker compose run --rm`).
# Network appnet: Compose creates a new one named <project>_<name>. To reuse one made with docker network create, add "external: true" under it.
services:
api:
image: myorg/api:2.1
stdin_open: true
tty: true
ports:
- "3000:3000"
networks:
appnet:
aliases:
- api
env_file:
- .env
mem_limit: 512m
cpus: 1.5
command:
- npm
- start
networks:
appnet: {}The service name comes from --name when present, otherwise from the image name (myorg/api:2.1 becomes api). Everything after the image is treated as the command. Paste several commands, one after another, to get several services in the same file.
Supported flags
| docker run | Compose key |
|---|---|
-p, --expose | ports (quoted strings), expose |
-v, --mount | volumes; named volumes are also declared at the top level |
-e, --env-file | environment, env_file |
--name, --restart | container_name, restart |
--network, --network-alias | networks with aliases, or network_mode for host, none, bridge and container: |
--health-cmd, --health-interval, etc. | healthcheck with CMD-SHELL |
-m, --cpus, --ulimit, --shm-size | mem_limit, cpus, ulimits, shm_size |
--gpus all | deploy.resources.reservations.devices with the GPU capability |
-it, -w, -u, --entrypoint | stdin_open, tty, working_dir, user, entrypoint |
Labels, capabilities, DNS settings, extra hosts, devices, tmpfs, logging driver and options, sysctls, --privileged, --init, --read-only and --platform are converted too.
Options explained
Environment as key: value map switches environment from a list of KEY=value strings to a YAML mapping, which some teams find easier to read and diff. version: is omitted by default because the current Compose Specification ignores it. Choose 3.8 or 3.9 only if an older tool in your pipeline still requires the key.
Limitations
--rmhas no Compose equivalent; the tool adds a comment suggestingdocker compose run --rmfor one-off containers.-P(publish all exposed ports) is dropped, because Compose needs explicit port mappings.--linkis converted tolinks, but this is a legacy feature. On the default Compose network, services can already reach each other by name, so you can usually delete it.-dis ignored; detached mode is chosen atdocker compose up -dtime.- Commands other than
docker runordocker create(such asdocker build) are skipped and listed in a# Skippedcomment; an error appears only when the input has nodocker runcommand at all. - A named volume or network gets a comment: Compose creates a new one prefixed with the project name, so add
external: trueto reuse one that already exists. $(pwd)at the start of a host path becomes., and a$that the shell would pass on literally (single-quoted or escaped) is written$$, the Compose escape.
Ports are always written as quoted strings. That avoids a classic YAML 1.1 pitfall where an unquoted value like 22:22 can be read as a base-60 number.
Privacy
The conversion happens in your browser, so passwords in -e flags are never uploaded. Still, consider moving secrets into an .env file before committing the YAML. To check or tidy the result, use the YAML validator or YAML formatter, and convert an env file to structured data with ENV to YAML.