How to Convert docker run to Docker Compose (Free Online Tool)
Written by Muhammad Ali · Senior Full Stack Software Engineer
Personal website: codedbymali.comLearn how to turn any docker run command into a docker-compose.yml file, flag by flag, and convert it instantly with our free in-browser converter.
You found a project on GitHub, the README says "run this", and you paste a long docker run command full of -p, -v and -e flags. It works — until you reboot, forget the flags, or need to add a second container. That is the moment to move to Docker Compose.
This guide shows how each docker run flag maps to a docker-compose.yml key, and how to do the conversion in one click with our free Docker Run to Docker Compose Converter.
Why use Docker Compose instead of docker run?
- It is saved in a file. No more digging through shell history for the exact command.
- One command for everything.
docker compose up -dstarts all your containers;docker compose downstops them. - Easy to share and version. Commit
docker-compose.ymlto Git so your team (or future you) gets the same setup. - Multiple containers, one network. An app, a database and a cache can talk to each other by service name.
Example: the same container, two ways
docker run:
docker run -d \
--name web \
-p 8080:80 \
-v ./html:/usr/share/nginx/html:ro \
-e TZ=Asia/Karachi \
--restart unless-stopped \
nginx:alpine
docker-compose.yml:
services:
web:
image: nginx:alpine
container_name: web
ports:
- "8080:80"
volumes:
- ./html:/usr/share/nginx/html:ro
environment:
- TZ=Asia/Karachi
restart: unless-stopped
Start it with docker compose up -d.
docker run flags → compose keys (cheat sheet)
| docker run flag | docker-compose.yml key |
|---|---|
--name web |
container_name: web |
-p 8080:80 |
ports: ["8080:80"] |
-v ./data:/data |
volumes: ["./data:/data"] |
-e KEY=value |
environment: ["KEY=value"] |
--env-file .env |
env_file: .env |
--restart always |
restart: always |
--network mynet |
networks: [mynet] |
--cap-add NET_ADMIN |
cap_add: [NET_ADMIN] |
--device /dev/dri |
devices: ["/dev/dri"] |
-l key=value |
labels: ["key=value"] |
--health-cmd "..." |
healthcheck: { test: ... } |
-d |
not needed (use docker compose up -d) |
--rm |
not needed (compose manages container lifecycle) |
Convert it in one click
- Open the Docker Run to Docker Compose Converter.
- Paste one or more
docker runcommands (multi-line commands with\are fine). - Copy or download your
docker-compose.yml.
Want to keep secrets out of the file? Turn on "Extract env vars to .env" and the tool creates a separate .env file and uses ${KEY} in the compose file.
Need to go the other way? Switch to compose → docker run to turn a service back into a single command — handy for quick tests.
Is it safe to paste commands with passwords?
Yes. The converter runs 100% in your browser. Nothing is uploaded to a server, so API keys and database passwords in your -e flags never leave your computer.
Common mistakes when converting by hand
- Unquoted ports. Write
"8080:80"in quotes — YAML can read somexx:yyvalues as numbers. - Relative volume paths. In compose,
./datais relative to the folder of thedocker-compose.yml, not where you ran the command. - The old
version:key. Modern Docker Compose ignores it, so you can leave it out. - Forgetting networks. Containers in the same compose file share a default network, so you often don't need
--networkat all.
FAQ
Can I convert several docker run commands into one compose file? Yes. Paste them all; each becomes its own service in one file.
Which Docker Compose version does the output use?
The current Compose Specification (no version: key by default). You can add it back with one toggle.
What if a flag isn't supported? The tool lists any flag it could not convert, so nothing is silently lost.
Try it now: Docker Run to Docker Compose Converter →
Related tools: YAML ↔ JSON Converter, YAML Validator, Case Converter, Line Sorter.