Docker Run to Compose Converter
Convert docker run commands to docker-compose.yml format. Supports ports, volumes, environment variables, networks, restart policies, and 30+ flags.
Multi-line commands with backslash continuation (\) are supported.
Flags -d, --rm, -it, and --init are runtime-only and are not included in the output.
When a docker run → Compose converter is the right tool
Use this page to turn a docker run command into a Compose services: snippet. Paste the command and click Convert — the result does not update as you type, except that the Nginx, PostgreSQL, and Redis example buttons fill the input and convert immediately. Nothing is uploaded.
After you have a snippet, Docker Compose Formatter can pretty-print it. The converter does not emit a complete compose project (no top-level networks or volumes).
Use cases
Migrating a documented docker run line
The Nginx example maps -p, -v, --name, and --restart, and drops -d. The PostgreSQL example keeps environment variables and the pgdata:… volume string, but it does not add a top-level volumes: entry for pgdata.
Turning a one-liner into a starting Compose file
Service name comes from --name when present, otherwise from the last path segment of the image before a tag, with characters outside a-z0-9_- replaced by _ (or app if that leaves nothing).
How conversion works
Backslash-newline continuations are flattened, then the command is tokenized with support for single and double quotes. Parsing starts at the first run token. Supported flags must be written as --flag value (a space, not an equals sign). --network mynet becomes network_mode: "mynet", not a Compose networks: list. --mount is copied as a raw volume string, not Compose long syntax.
docker run -d --name nginx -p 80:80 -p 443:443 \
-v /data/nginx:/usr/share/nginx/html:ro --restart unless-stopped nginx:alpine
services:
nginx:
image: nginx:alpine
container_name: nginx
restart: unless-stopped
ports:
- "80:80"
- "443:443"
volumes:
- "/data/nginx:/usr/share/nginx/html:ro"Write long flags with a space before the value, and keep command arguments from looking like docker flags if you need them in command:.
What this tool does not do
It does not implement every docker run flag, recognize --flag=value, emit top-level networks or volumes, or treat dashed tokens after the image as command arguments. Conversion stays in your browser.
How to use Docker Run to Compose Converter
- Paste a command that includes
run, or click an example button. - Click Convert if you pasted by hand, then read the YAML snippet.
- Copy the output, add any top-level networks or volumes you need, and format it if you want.
Frequently asked questions
How does Docker Run to Compose work?
Paste a command that contains a run token (docker run … or run …) and click Convert. The first non-flag token is the image; later non-flag tokens are joined as command. The output is a services: snippet only — there is no version key and no top-level networks or volumes, even when a named volume such as pgdata:… is used. Example buttons fill a command and convert it automatically.
Which flags are supported, and which are ignored?
Common flags such as -p/--publish, -v/--volume, --mount (copied as a raw string), -e/--env, --env-file, --name, -h/--hostname, --restart, --network/--net (as network_mode, omitted when the value is bridge), -u/--user, -w/--workdir, --entrypoint, -l/--label, capabilities, --privileged, --add-host, --dns, --device, --sysctl, --security-opt, -m/--memory, -c/--cpu-shares, --pid, --ipc, and healthcheck flags are recognized when written as --flag value. Runtime flags -d/--detach, -i/--interactive, -t/--tty, --rm, --init, --no-healthcheck, --read-only, and -P/--publish-all are ignored. -it is treated as ignored -i with a leftover t.
Why did --appendonly or --health-cmd=… disappear?
Two parser limits matter. Long options in the --flag=value form are not recognized and are skipped, so --health-cmd="curl -f localhost" does not produce a healthcheck (write --health-cmd "curl -f localhost" instead). After the image name, a token that starts with - is still parsed as a docker run flag, not as a command argument — the built-in Redis example therefore keeps command: "redis-server" and drops --appendonly yes.
Are backslash continuations supported? Is my command uploaded?
Yes, a backslash immediately before a newline is treated as a line continuation. Quoted arguments are tokenized and the quotes themselves are removed. Conversion runs in your browser. The input must include a run token; pasting only flags and an image name yields No "run" command found.
Related tools and guides
References
- docker run has many more flags than this converter maps. Unknown long options that take a value are consumed and dropped.
- The output is a
services:snippet. Add top-levelnetworksorvolumesyourself if you need named resources, then optionally format the file with the Compose formatter linked above.