What does converting docker run to docker-compose do?
Every project documentation page tells you to start a container with a long docker run command: a few -p port mappings, some -e variables, a -v volume, a restart policy. It works, but the command lives in your shell history and in a README. A docker-compose.yml holds the same configuration as a file you can commit, review, change and start again with one command. This converter reads a docker run line and writes the equivalent compose file.
The two formats describe the same thing in different words. -p 80:80 becomes an entry under ports, -e KEY=value an entry under environment, -v an entry under volumes, --restart becomes restart, --name becomes container_name, and --network becomes a networks section. The tool understands most of the flags people use in practice, including the less obvious ones such as healthchecks, resource limits, capabilities, devices, DNS and labels. The name of the service is taken from the image.
Some flags have no meaning in a compose file, and the converter says so instead of guessing. -d and --rm describe how you run the command, not what the container is. Flags it does not know are left out and listed in a warning. Conversion runs in your browser; your commands, which often contain passwords in -e, are not uploaded. To check or tidy the YAML afterwards, use the YAML formatter.
How to convert docker run to docker-compose
- Paste your command in the first box. It opens with an example (an nginx container with a port, a socket volume, a restart policy and a log option), so you see the result before typing. You can paste several commands, one per line or separated by
;or&&; each one becomes a service in the same file. Line continuations with a backslash, a backtick (PowerShell) or a caret (cmd) are joined, and curly quotes copied from a blog are straightened. - The compose file appears below as you type. Choose two or four spaces of indentation.
- Read the notices under the result. A yellow box lists options that could not be translated. A blue box, good to know, lists things that were handled for you, such as
-dbeing dropped or$(pwd)being written as.. - To add settings that the command does not carry, open the extras: a restart policy used only when the command has none, a time zone passed as the
TZvariable, an external network to join, log-size limits (json-file, 10m, 3 files), and Traefik labels with host, container port, entrypoint and certificate resolver. - To add the new services to a compose file you already have, open the option for an existing file and paste it. The services are added, volumes and networks are not duplicated. If the existing file is not valid YAML, a message says why.
- Copy the result, or download it as
docker-compose.yml.
Need a free port for the left side of a mapping? Draw one with the random port generator. Passwords for -e variables are better generated than invented: use the token generator.
Examples
The default example
Input:
docker run -p 80:80 -v /var/run/docker.sock:/tmp/docker.sock:ro --restart always --log-opt max-size=1g nginxOutput:
services:
nginx:
image: nginx
ports:
- "80:80"
volumes:
- "/var/run/docker.sock:/tmp/docker.sock:ro"
restart: always
logging:
options:
max-size: 1gA database with a named volume
Input:
docker run -d --name db -e POSTGRES_PASSWORD=secret -e POSTGRES_USER=app -p 5432:5432 -v pgdata:/var/lib/postgresql/data --restart unless-stopped postgres:16Output. The named volume is declared at the end, which compose requires:
services:
db:
image: postgres:16
container_name: db
environment:
- POSTGRES_PASSWORD=secret
- POSTGRES_USER=app
ports:
- "5432:5432"
volumes:
- "pgdata:/var/lib/postgresql/data"
restart: unless-stopped
volumes:
pgdata: nullA bind mount and an existing network
Input: docker run -d --name web -p 8080:80 -v $(pwd)/html:/usr/share/nginx/html:ro --network app-net --rm nginx:alpine. The shell substitution is rewritten as a relative path and the network is declared as external:
services:
web:
image: nginx:alpine
container_name: web
ports:
- "8080:80"
volumes:
- "./html:/usr/share/nginx/html:ro"
networks:
- app-net
networks:
app-net:
external: trueNotices: $(pwd)/html was written as .; --rm has no equivalent (use docker compose run --rm for one-off runs); -d is a choice made when you start the stack.
Two commands, two services
Two lines, docker run -d --name redis -p 6379:6379 redis:7 and docker run -d --name api -p 3000:3000 -e REDIS_URL=redis://redis:6379 --link redis ghcr.io/acme/api:1.2, produce a file with redis and api under services, with links: - redis on the api. If two commands would give the same service name, the second gets -2 and a notice says so.
Extras: restart, time zone, logs and Traefik
With restart policy unless-stopped, time zone America/Sao_Paulo, log rotation, an external network proxy and Traefik for app.example.com on port 8080, the command docker run -d --name app ghcr.io/acme/app:1.0 becomes:
services:
app:
image: ghcr.io/acme/app:1.0
container_name: app
environment:
- TZ=America/Sao_Paulo
restart: unless-stopped
networks:
- proxy
labels:
- traefik.enable=true
- traefik.http.routers.app.rule=Host(`app.example.com`)
- traefik.http.routers.app.entrypoints=websecure
- traefik.http.routers.app.tls.certresolver=letsencrypt
- traefik.http.services.app.loadbalancer.server.port=8080
logging:
driver: json-file
options:
max-size: 10m
max-file: "3"
networks:
proxy:
external: trueCommon errors and how to fix them
- Paste a docker run command: the box is empty.
- This is not a docker run command: no image was found. Start with
docker runand end with the image name. For exampledocker run -e A=1has options but no image. - The flag -v needs a value after it: a flag at the end of the line has nothing after it. Add the value or remove the flag.
- A quote was opened and never closed:
-e "A=1 nginxopens a double quote that is never closed. Close it with the same kind of quote. - The compose file to merge into could not be read: the existing file is not valid YAML, such as
services: [oops. Fix it or empty that box. - A flag is not known, so it was left out: the converter warns with the flag name. If the flag takes a value, check that the service image is right, because the value may have been read as the image.
Frequently asked questions
How do I convert docker run to docker-compose?
Paste the command and copy the YAML. Save it as docker-compose.yml (or compose.yaml) and start it with docker compose up -d.
Is the result identical to the docker run command?
For the options the tool understands, yes: the container is configured in the same way. Things that only describe how you launch it, such as -d and --rm, are not part of a compose file.
Where do named volumes go?
Under a top-level volumes section, which the converter adds for you. Without it, compose refuses a named volume.
Can I convert several containers at once?
Yes. One command per line, or separated by ; or &&. Each becomes a service.
How to run the result
Bash
docker compose config # validate the file and print it resolved
docker compose up -d # start the stack in the background
docker compose logs -f db # follow one service
docker compose down # stop and remove the containers