· 5 min read
How to Convert a Docker Run Command to Compose
Heshan Fernando
Co-founder & COO
You found a docker run command in a project’s README — a long line with a dozen -p, -e, and -v flags — and you want it as a docker-compose.yml instead, since that’s how the rest of your project’s services are defined. Translating that flag-by-flag by hand means knowing exactly which YAML key each Docker CLI flag maps to: -p becomes ports, -e becomes environment, -v becomes volumes, and so on, each with its own slightly different syntax in YAML form.
It’s not conceptually hard, but a long docker run command with many flags gives you a lot of individual mappings to get exactly right, and a single mistranslated flag (wrong port order, a volume mount reversed) produces a compose file that looks plausible but doesn’t actually behave the same way.
What converting docker run to compose actually involves
Every docker run flag has a direct equivalent in Docker Compose’s YAML schema: -p host:container maps to a ports list entry, -e KEY=value maps to an environment list or map, -v host:container maps to a volumes list entry, and flags like --name, --restart, and --network each map to their own top-level key under the service definition. The conversion is mechanical once you know every mapping, which is exactly the kind of translation task that’s easy to automate and tedious to do by hand for a command with many flags.
Getting the mapping direction right matters — -v host:container and -p host:container both follow a host-then-container ordering, and reversing that order in the YAML produces a compose file that runs but binds ports or mounts volumes backwards from what was intended.
Why people get stuck here
- Long commands have many flags to translate individually. A
docker runcommand with a dozen flags means a dozen individual translations, each a small opportunity for a mistake. - YAML syntax for lists and maps isn’t always obvious. Environment variables, in particular, can be expressed as either a YAML list or a map in Compose, and getting the indentation and format right by hand is a common source of a broken file.
- Host-versus-container ordering is easy to flip. Both port mappings and volume mounts follow a host:container convention, and accidentally reversing that order produces a compose file that starts fine but doesn’t actually work as intended.
- Some flags have no direct one-line equivalent. A few
docker runoptions translate into slightly more involved YAML structures in Compose, which isn’t obvious if you’re translating flag by flag from memory.
What a good docker run to compose converter looks like
Maps every common flag correctly
Ports, environment variables, and volumes are the most common flags, and the converter needs to translate all three (along with other common ones) into their correct YAML equivalents.
Produces valid, correctly indented YAML
Since YAML is whitespace-sensitive, the generated file needs correct indentation throughout, not just approximately-right structure that a human then has to fix.
Preserves the original ordering and intent
Host-then-container ordering for ports and volumes needs to carry through accurately, so the converted file behaves identically to running the original command directly.
Common mistakes to avoid
- Reversing host and container values in a port or volume mapping during manual translation, producing a compose file that runs but doesn’t route traffic or mount files as intended.
- Getting YAML indentation wrong, which can silently change which service a setting applies to or make the file invalid entirely.
- Missing a less common flag during manual translation because it wasn’t as obviously familiar as
-p,-e, or-v. - Not testing the converted compose file against the original command’s actual behavior before relying on it in a real project.
- Forgetting that some
docker runflags (like one-off flags meant for interactive use) don’t have a meaningful Compose equivalent and shouldn’t be translated at all.
How to do it with Docker Run to Compose
Online Tool Store’s Docker Run to Compose converts a pasted docker run command into an equivalent docker-compose.yml, entirely in your browser.
- Paste your
docker runcommand. - Get the converted
docker-compose.yml, with ports, environment variables, and volumes mapped correctly. - Review the generated YAML against the original command to confirm nothing was missed.
- Drop it into your project alongside your other compose service definitions.
Because it maps flags to their exact YAML equivalents automatically, it removes the tedious, error-prone part of a long command’s manual translation.
Frequently asked questions
Will the converted compose file behave exactly like the original docker run command?
For the flags it translates, yes — the goal is a functionally equivalent service definition. Some very specific or interactive-only flags don’t have a meaningful Compose equivalent and may need manual handling if your original command used them.
Can I add this converted service to an existing docker-compose.yml with other services?
Yes — the converted output is a single service definition, which you can add under the services key of an existing compose file alongside other services already defined there.
Why does my environment variable formatting look different from what I expected?
Docker Compose supports environment variables as either a YAML list (- KEY=value) or a map (KEY: value) — the exact format the converter uses is a stylistic choice that’s functionally equivalent either way, so check your project’s existing convention if consistency matters.
Final thought
Translating a docker run command’s flags into Compose YAML by hand is mechanical work with a lot of small opportunities to flip an order or misplace an indent. Convert it directly, then read through the result once to confirm it matches your original command’s intent.