Docker Compose Converter
Docker Compose Converter
Convert a docker run command to docker-compose.yml — handles ports, volumes, env, restart, networks.
Docker Compose Converter Tool
# Converted from a docker run command at recatools.com/docker-compose-converter/
services:
postgres:
image: postgres:16
environment:
- "POSTGRES_DB=app"
- "POSTGRES_USER=app"
- "POSTGRES_PASSWORD=secret"
ports:
- "5432:5432"
volumes:
- "pgdata:/var/lib/postgresql/data"
restart: always
volumes:
pgdata: {}
The output follows the Compose Specification: no version: line (the specification
lists it as obsolete), every key is one the specification's schema defines, and a $
your shell would have kept literal is written $$ so Compose does not treat it as a
variable. Everything runs in this page; the command is not sent anywhere.
How to use the Docker Compose converter
Paste your docker run command
One line, or several joined with \ line continuations. Quotes are read the way a POSIX shell reads them, so a quoted argument stays one argument. sudo, docker container run and docker create are accepted too.
Review the output
The right pane is a complete compose.yaml. Options come before the image, as docker run requires; everything after the image becomes command, including words that start with a dash. Named volumes and user-defined networks are declared at the top level for you.
Read the notes
Anything the converter could not carry over exactly is listed under the panes. Check items need your attention: a flag with no Compose key, a shell $(...) it cannot run, a second command after &&. Note items explain a choice it made, such as --name becoming container_name or --rm having no service-level equivalent.
Copy or download
Copy the YAML or download it as compose.yaml, the file name the Compose Specification prefers, put it in your project folder and run docker compose up -d.
Docker Compose — when one container becomes a stack
docker run is fine for "give me one container, now". It stops being adequate once your application needs several services that have to find each other. A web app, a database and a cache are three docker run commands to remember, in order, with network names typed by hand and restart behaviour you have to repeat each time. Docker Compose keeps all of that in one YAML file that docker compose up reads.
How the conversion reads your command
Two rules decide most of the output, and both come from how the docker CLI itself parses docker run. First, the options end at the image: the CLI turns off interspersed flag parsing for run, so in docker run nginx -g "daemon off;" the -g is an argument to nginx, not an option to docker. This converter does the same, and everything after the image becomes the service's command. Second, it uses the CLI's own list of which options take a value and which are switches, so -dit is read as three switches, -p8080:80 as a port, and an unknown option cannot quietly swallow the image name.
Quoting is read as a POSIX shell reads it. Single quotes keep everything literal; double quotes still expand $VAR; a backslash escapes the next character. That matters because Compose has its own variable syntax. A $HOME your shell would have expanded stays $HOME, and Compose resolves it from your environment or a .env file at run time. A $ your shell would have kept literal, as in -e 'PASS=a$b', is written $$, which the specification defines as a literal dollar sign
. Shell features with no Compose equivalent, such as $(date), are reported rather than guessed.
The version key, and why this converter does not write one
"Compose v2" means two different things, which is most of why this subject is confusing. It is the name of a file format that introduced the version: '2.x' header, and it is also the name of the Go rewrite of the command, the one you invoke as docker compose rather than docker-compose. The numbers are unrelated.
The file-format numbers have been retired. There is now one document, the Compose Specification, and it is explicit about the header: the top-level
It is not used for validation either: version property is defined by the Compose Specification for backward compatibility. It is only informative you'll receive a warning message that it is obsolete if used.Compose doesn't use
So the output has no version line.version to select an exact schema to validate the Compose file, but prefers the most recent schema when it's implemented.
What does not map one-to-one
--rm has no service-level equivalent: a Compose service describes something meant to exist, so the closest you get is docker compose run --rm <service> for a one-off. --name becomes both the service name, which other services resolve on the network, and container_name, which is what docker run actually set; the specification warns that the second forecloses scaling, Compose does not scale a service beyond one container if the Compose file specifies a
Delete that line if you want replicas. container_name. Attempting to do so results in an error.-i and -t become stdin_open and tty; -d and --attach describe how you ran the command and have no key. --network host, none and container:<name> become network_mode, which the specification does not allow alongside networks.
A handful of options have no Compose service key and are listed in the notes instead: -P/--publish-all, --cpuset-mems, the per-device block-IO limits, the Windows-only IO limits, --volume-driver, and --gpus in any form other than all.
Production gotchas worth knowing
Three catch people out. Every service in the file shares one default network, so any container can reach any other by service name. That is convenient in development and not an isolation boundary; declare named networks and attach services deliberately if you need one. Relative paths resolve against the Compose file, not your shell. The specification says so for env_file, Relative path are resolved from the Compose file's parent folder
, and warns that absolute paths prevent the Compose file from being portable
. And short-form depends_on waits for start, not for readiness. The long form is what you want: condition: service_healthy against a dependency that defines a healthcheck:, or service_completed_successfully for a migration container that must finish before the app boots. docker run has no dependency option, so you add this by hand when you combine services.
Compose Specification rules this converter's output has to live with
There is no longer a "compose v3" schema to target. The Compose Specification is one living document; Compose doesn't use
Choosing a format version is a decision that no longer exists.version to select an exact schema to validate the Compose file, but prefers the most recent schema when it's implemented.
A literal dollar sign is written twice. Compose interpolates $VAR and ${VAR} anywhere in a value, and the specification's escape is $$: You can use a
A password containing $$ (double-dollar sign) when your configuration needs a literal dollar sign.$ pasted unescaped into a Compose file silently loses part of itself.
Services find each other by name. On a shared network, the specification says, other containers can use either the service name or an alias to connect to one of the service's containers
, which is why postgres:5432 works from an app container with no IP address in sight.
Anonymous volumes survive down but are not reused. Docker's docker compose down reference: Anonymous volumes are not removed by default. However, as they don’t have a stable name, they are not automatically mounted by a subsequent
Name a volume if its data matters.up.
Short-form depends_on waits for started, not ready. The long form takes three conditions: service_started, service_healthy (needs a healthcheck: on the dependency), and service_completed_successfully, the one that makes a migration container work.
environment: beats env_file:, and the specification closes the obvious loophole: values in environment override the file, and This holds true even if those values are empty or undefined.
Listing a variable with no value therefore blanks it rather than falling through.
restart: "no" is quoted in the specification's own example, and the reason is spelled out under environment: boolean-looking values should be enclosed in quotes to ensure they are not converted to True or False by the YAML parser.
This converter quotes every value a YAML parser could read as a boolean or null.
Frequently asked questions
--mount (as long syntax), --tmpfs, environment and env files, networks (with aliases and fixed IPs) or network_mode, restart, the --health-* family and --no-healthcheck, entrypoint and command, user, working directory, hostname, DNS, extra hosts, capabilities, security options, devices, sysctls, ulimits, labels, logging, memory and CPU limits, --init, --privileged, --read-only, -i/-t and the rest. The flag list is the docker CLI's own. A flag that has no Compose key is named in the notes, and a word that is not a docker run option at all is reported and skipped without being mistaken for the image.It is only informative you'll receive a warning message that it is obsolete if used.The file is written for
docker compose, which implements the specification and validates against its most recent schema whatever the header says.-v pgdata:/var/lib/postgresql/data) are kept on the service and declared under a top-level volumes: block. Bind mounts (-v ./html:/usr/share/nginx/html, -v /var/run/docker.sock:…) stay on the service only. --mount type=…,source=…,target=… becomes the long syntax, with readonly as read_only: true. An unquoted ~ at the start of a path is your home directory to the shell, so it is written as ${HOME}.$$, Compose's escape for a literal dollar.&&, ; or a pipe, only the first command is converted and a note says so. For a stack, convert each command and combine the service blocks under one services: key.docker compose run --rm <service> for one-off commands, but the long-running service definition itself has no --rm setting.--health-cmd becomes test: ["CMD-SHELL", "…"], the same form the docker CLI builds from it, and --health-interval, --health-timeout, --health-retries, --health-start-period and --health-start-interval map to the matching keys. --no-healthcheck becomes disable: true; combined with a --health-* option it is refused, as docker refuses it.--network, the project's default network, shared by every service in the file. A user-defined network is attached and declared at the top level, which makes Compose create it as <project>_<name>; if it already exists from docker network create, add external: true under it. host, none and container:<name> become network_mode.depends_on with a readiness condition, a shared network, a named volume two services mount. A docker run command describes one container in isolation, so anything a Compose file says about how services relate has nowhere to go.Method & sources
How it computes
Reads a docker run command with POSIX shell quoting and the docker CLI's own flag table: options end at the image, everything after it is the command. Writes compose.yaml with only keys the Compose Specification schema defines and no obsolete version line; a $ the shell kept literal becomes $$. Flags with no Compose key are named in notes.
What this tool implements
- Flag parsing follows docker/cli (cli/command/container opts.go, run.go, create.go): the 15 boolean options and 15 short forms are the CLI's, combined short flags (-dit) and attached values (-p8080:80, --name=x) are read as pflag reads them, and run.go's SetInterspersed(false) is honoured, so every word after the image is the command.
- Output keys are checked in the test suite against the properties of the `service` definition in compose-spec/compose-spec schema/compose-spec.json; the top-level `version` property is not written because the specification lists it as obsolete. Until 2026-11-05 the converter wrote version: "3.8" and the page carried a "Known issue" note telling readers to delete it.
- Shell quoting is POSIX: single quotes literal, double quotes expand $ and honour \ before $ ` " \ and newline, backslash-newline continues a line, # starts a comment, ~ at a word start is ${HOME}. A $ the shell would expand stays a Compose variable; a literal $ is written $$ (12-interpolation.md). $(...), backticks and shell-only parameters are reported, not guessed.
- Mappings that are not one-to-one are stated: --name → container_name (docker run sets the literal name) with the specification's scaling warning quoted; -i/-t → stdin_open/tty; --network host|none|container:X → network_mode, never alongside networks; --health-cmd → ["CMD-SHELL", cmd] as opts.go builds it; --no-healthcheck → disable: true and refused with --health-*; -e NAME without a value stays a bare name, resolved by Compose from the environment as docker run took it from the shell.
- No key is invented: --depends-on, which the old presets used, is not a docker run option and is reported as such. Options with no Compose service key (-P, --cpuset-mems, per-device blkio limits, Windows-only IO limits, --volume-driver, --gpus other than all) are listed in the notes.
- Every value is written as a YAML string that parses back to itself, quoted wherever YAML could read it as a number, a boolean or null (the specification's environment note), so restart: "no" and hostname: "yes" stay strings.
Sources
- The Compose Specification, compose-spec/compose-spec — spec.md (restart, ports, volumes short and long syntax, network_mode, networks and aliases, healthcheck, environment, env_file, container_name, entrypoint, command, tmpfs, ulimits, logging): https://github.com/compose-spec/compose-spec/blob/main/spec.md
- Compose Specification, 04-version-and-name.md (version is obsolete) and 12-interpolation.md ($VAR, ${VAR} and the $$ escape): https://github.com/compose-spec/compose-spec/blob/main/12-interpo…
- Compose Specification JSON schema, schema/compose-spec.json — the service properties the test suite checks every generated key against: https://github.com/compose-spec/compose-spec/blob/main/schema/com…
- docker/cli, cli/command/container/opts.go and run.go — the docker run flag set, boolean flags, short forms, SetInterspersed(false), --entrypoint and --health-cmd handling: https://github.com/docker/cli/tree/master/cli/command/container
- Docker documentation, docker container run reference — the documented option list: https://docs.docker.com/reference/cli/docker/container/run/
- Docker documentation, docker compose down — anonymous volumes are not removed by default and are not reattached by a later up: https://docs.docker.com/reference/cli/docker/compose/down/
What can make this go out of date
- None at runtime. Parsing and YAML generation happen in the page; the command and the generated file are never transmitted, stored or put in the URL.