Skip to content

Docker Run to Docker Compose Converter

Convert docker run commands to docker-compose.yml format with proper YAML structure and service configuration.

Docker Run to Docker Compose Converter tool

Loading...

Example

Input: docker run -d --name web -p 8080:80 -v ./html:/usr/share/nginx/html --restart unless-stopped nginx:alpine

Result: services: web: with image nginx:alpine, container_name web, ports '8080:80', volumes ./html:/usr/share/nginx/html, restart unless-stopped

Each flag maps to its Compose key (-p to ports, -v to volumes, --restart to restart). -d is dropped because docker compose up -d handles detaching.

docker run Flags and Their Compose Keys

docker run flagCompose key
-p, --publishports
-v, --volumevolumes
-e, --envenvironment
--env-fileenv_file
--namecontainer_name
--restartrestart
-h, --hostnamehostname
-w, --workdirworking_dir
-u, --useruser
-l, --labellabels
--network (named)networks, declared as external
--network host, none or bridgenetwork_mode
--cpus, -m / --memorydeploy.resources.limits
-i, -tstdin_open, tty
-d, --rm, -Pdropped

Mappings taken from this converter's flag tables.

  1. Paste your complete docker run command (including all flags like -p, -v, -e, --network) into the input area.
  2. The tool automatically parses the command and maps each flag to its docker-compose.yml equivalent.
  3. Review the generated YAML output to verify the service name, ports, volumes, and environment variables are correct.
  4. For multi-container setups, paste additional docker run commands on separate lines to generate a multi-service Compose file.
  5. Click the Copy button above the output to copy the complete docker-compose.yml content to your clipboard.
  6. Save the output as a docker-compose.yml file in your project directory and run it with docker compose up.

From docker run to docker-compose.yml

This converter turns docker run commands into a docker-compose.yml file. Each flag maps to its Compose key: -p becomes ports, -v becomes volumes, -e becomes environment and --restart becomes restart. The result follows the current Compose Specification, so it has no top-level version key.

A long docker run command is hard to read, easy to mistype and lives only in your shell history. A Compose file describes the same container as YAML that you can commit to Git, review and start with one command: docker compose up -d. It also makes it easy to add more services later, such as a database next to your app.

The parser reads the command the way a shell would. It understands single and double quotes, backslash line breaks, --flag=value forms and combined short flags such as -it. Anything after the image name becomes the command, kept as a list so arguments with spaces survive.

Flags such as -d, --rm and -P have no Compose equivalent and are dropped. --cpus and --memory go under deploy.resources.limits. --network host becomes network_mode, while a named network is listed under networks and declared at the bottom as external. Named volumes, as opposed to host paths, get a top-level volumes section.

Paste several commands, one per line or joined with &&, to get one service per command. Service names come from --name or the image name, with -2 or -3 added if two clash. Flags the tool does not map, such as --gpus, --mount or --log-opt, are skipped, so compare the output with your original command before you rely on it. To tidy the YAML afterwards, use the YAML Formatter.

Sources

Frequently Asked Questions

It maps ports (-p), expose, volumes (-v), environment (-e), env files, labels, name, hostname, restart, workdir, entrypoint, user, network, cpus, memory, shm-size, cap-add, cap-drop, device, add-host, dns, and the switches -i, -t, --privileged, --init and --read-only. Other flags are recognized so they do not confuse the parser, but they are left out of the output.
The top-level version key is obsolete in the Compose Specification, and current Docker Compose ignores it and prints a warning when it is present. Leaving it out keeps the file valid for the docker compose plugin. Older docker-compose 1.x installs may still expect it, in which case add version: "3.8" at the top.
They control how you run the container, not what the container is. Detached mode is chosen when you start the stack, with docker compose up -d. Compose manages container removal itself: docker compose down stops and removes the containers. So neither flag has a place in the file.
When docker run uses --network mynet, that network must already exist. The converter declares it as external so Compose uses the existing network instead of creating a new one with a project prefix. If you want Compose to create and own the network, delete the external: true line.
The --name value is used when present, otherwise the image name without its registry path or tag, so ghcr.io/org/api:1.2 becomes api. Characters Compose does not allow are replaced with hyphens. If two commands give the same name, the second becomes name-2, the third name-3, and so on.
No. docker run always starts from an image that already exists, so there is no build information to convert, and the output uses image: with the name from your command. If you build the image yourself, add a build: section that points to the folder with your Dockerfile, such as build: . next to or instead of image:.

Related Tools

YAML Formatter and Viewer

Format, validate, and visualize YAML documents with tree view and customizable indentation.

Developer Tools

JSON to YAML Converter

Convert JSON documents to YAML format with syntax validation, clean formatting, and instant preview.

Developer Tools

Crontab Generator

Visually build cron expressions with dropdowns, see human-readable descriptions, and preview the next 5 run times.

DevOps Tools

Git Cheatsheet

Searchable interactive Git command reference organized by workflow - branching, merging, rebasing, and more.

DevOps Tools

Regex Cheatsheet

Interactive regex syntax reference with descriptions, examples, and a try-it-live tester for each pattern.

DevOps Tools

Chmod Calculator

Convert between symbolic and numeric Unix file permissions with a visual chmod calculator.

Developer Tools