Docker run to Docker compose converter
What is a Docker Run To Docker Compose Converter?
A long docker run command with ports, volumes and environment variables is hard to read, easy to mistype and impossible to version properly. Docker Compose describes the same container in a YAML file you can commit, review and start with one command. This converter turns your existing docker run commands into a Compose service definition.
How to use it
- Paste one or more
docker runcommands. - Copy the generated
docker-compose.yml. - Run
docker compose up -din the same folder.
Example
Input:
docker run -d --name db -p 5432:5432 -e POSTGRES_PASSWORD=secret -v pgdata:/var/lib/postgresql/data --restart unless-stopped postgres:16Output:
services:
db:
image: postgres:16
container_name: db
ports:
- 5432:5432
environment:
- POSTGRES_PASSWORD=secret
volumes:
- pgdata:/var/lib/postgresql/data
restart: unless-stopped
volumes:
pgdata:Flag mapping
-pbecomesports,-vbecomesvolumes,-ebecomesenvironment.--namebecomescontainer_name,--restartbecomesrestart.--networkbecomesnetworks;-dis implied bydocker compose up -d.
After converting
- Move secrets out of the file into an
.envfile or Docker secrets. - Remove
container_nameif you want to scale a service. - Add
depends_onwith health checks when one service needs another. - Put services that talk to each other in the same Compose file; they can reach each other by service name.
Frequently Asked Questions
Why use Docker Compose instead of docker run?
Compose files are readable, version-controlled and can start several related containers with one command.
Is the version key still needed in docker-compose.yml?
No. Modern Docker Compose ignores the top-level version key, so it can be omitted.
How do I keep passwords out of the compose file?
Use an .env file with variable substitution, or Docker secrets, and keep the .env file out of version control.
Can I convert several docker run commands at once?
Yes. Each command becomes a service in the same compose file.