version: '3.9'
services:
web:
image: nginx:alpine
container_name: web
restart: unless-stopped
networks:
- mynet
ports:
- '8080:80'
- '443:443'
environment:
DB_HOST: db
DB_USER: root
volumes:
- /data:/var/www
About docker run to compose
A long, unmemorable docker run command stuffed with -p, -v, -e, and other flags has to be retyped on every new machine. This tool parses a docker run command and converts it into a clean docker-compose.yml locally in your browser, mapping ports, volumes, environment variables, networks, and more into structured fields for version control and reuse.
The core of the conversion is turning one-off launch arguments into a repeatable declaration, which frames what to keep. Port mappings, volume mounts, environment variables and restart policy map straight across, whereas one-off actions such as running a script before starting belong in the image build rather than as stacked commands in the declaration. Conversion is also a chance to tidy the structure.
Volumes and ports require separating persistent from temporary data or you lose data or accumulate junk. A path mapped to the host survives a container rebuild and suits databases and uploads, while an unmapped path disappears with the container and suits caches and scratch output. Judge each path on the command line rather than copying the mounts across verbatim.
Dependencies and start order are easily misread, since a declared dependency only means started first, not ready. A database container takes time to accept connections, and an application connecting before then still fails and retries. Combining health checks and retry logic is more reliable than declaring order alone, which only settles sequencing.
Environment variables involve credential management and should be planned during conversion. Writing a database password into the declaration puts it in version control where anyone with repository access can read it, so read from the host environment or a separate file and keep credential-bearing files out of version control. Conversion is more than syntax translation; it is a credential review.
Networking decides whether containers reach each other by name. Containers on one user-defined network resolve each other by service name, whereas name resolution on the default network is unreliable, and whether a host port is published is a separate decision — containers talking only internally need no host port. Draw the lines between internal traffic and external exposure to shrink the attack surface.
Finally, its split with heavier orchestration means you need not start with the heaviest option. A declaration file suffices for multiple containers on one host, development and small deployments, while multi-node scheduling, autoscaling and rolling upgrades call for cluster orchestration. Decide which stage you are at rather than importing complexity for problems you do not yet have.
How It Works Internally
Conversion maps command-line options onto orchestration fields. The two are similar in meaning but not one-to-one: the order in a port mapping, the base directory for a mount path, and the list versus mapping form of environment variables all differ, and the list form dropping values containing an equals sign surfaces only at runtime. A few command-line-only capabilities need extra configuration.
-p 8080:80 -v ./data:/app/data -e A=b=cports as "8080:80" (host first); mounts relative to the file; environment variables need the mapping form or A=b=c gets truncatedMeasured on This Page
| Flag mapping | Image, --name, -p, -e, -v, --restart, --network and quoted -e values all map correctly |
|---|---|
| environment | KEY=VALUE becomes a mapping; -e without = records an empty value; spaces inside quoted values survive |
| Service naming | With --name it is used as the service name; without one the name is app — a page convention, not derived from the image |
| Invalid input | Anything not starting with docker run reports it and emits no partial YAML |
| Version header | The output always carries version: "3.9" |
Four of the six commands omitted --name and every one of them got the service name app instead of a name derived from the image, so add --name or rename afterwards if you prefer image-based naming. Also note that runtime-only flags like -d, -it and --rm are dropped: compose has no field for them, since containers run in the foreground and compose manages their lifecycle. The output always carries version: "3.9".
How to Use
- Open "docker run to compose"
- Paste one complete docker run command
- The compose snippet appears on the right
- Check services, ports and volumes
- Click "Copy" to paste it into compose.yml
Use Cases
- Lock down deploys — Turn an ad-hoc docker run into a compose file and commit it for version control.
- Move environments — Convert a dev-machine run command to compose to reproduce it in test or production.
- Untangle flags — Structure stacked CLI flags to clearly see port, volume, and env mappings.
- Multi-container — Convert several separate run commands into compose services managed together.
- Document setup — Turn a word-of-mouth start command into readable YAML for your deploy docs.
- CI integration — Translate a docker run from a CI pipeline step into a compose service for local reproduction.
- Share with team — Convert a personal run command into a checked-in compose file so everyone runs the same stack.
FAQ
Which docker run flags are supported?
Common ones — -p ports, -v volumes, -e env vars, --name, --network, --restart, -d — are recognized and mapped to the matching compose fields. Rare or very new flags may need manual addition.
Which compose version is produced?
It usually outputs a broadly compatible v3 structure. You can tweak the version field for your Docker setup, or drop it to use the Compose spec default behavior.
How are multiple -e env vars handled?
Each -e becomes an item in the environment list; if a value contains equals signs or spaces, ensure the original command quoted it properly — the tool preserves it as-is.
Can it convert compose back to run?
It currently does one-way run-to-compose only. Reverse conversion involves splitting multiple services and is complex, so it is not supported.
Can I run docker compose up right away?
Usually yes, but read through the generated YAML first to confirm volume paths, image tags, and network names match your target environment before starting.
The converted compose file fails to start although docker run works. Why?
Three differences explain most cases. Port mapping has host first in both places, but reversing the order makes the container listen on the wrong port; mount paths are often written relative on the command line while compose resolves them against the file location, so the meaning changes; and environment variables passed one by one become a list in compose that drops values containing an equals sign, where a mapping form is required. Checking these three covers most failures.
Can compose express every docker run option?
Most, but a few capabilities exist only on the command line: one-off network and device mappings, options that depend on host kernel parameters, and debug-time command overrides each need a compose field or an extra configuration file. After converting, confirm those with no direct equivalent were not silently dropped, especially security-relevant capability and privilege settings.
Image tags are missing in the generated compose file?
If the docker run command omitted the tag (nginx instead of nginx:1.25), compose can only emit image: nginx and pulls latest. Add the tag explicitly before converting to avoid the classic "works locally, breaks in CI on a newer image" failure.