Skip to main content
Calnex LogoCalnex
Developer Tools

Docker to Compose Converter

Convert a Docker run command into an equivalent docker-compose.yml service definition, preserving common flags such as ports, volumes, environment variables, restart policy, network mode, labels, and command arguments.

Parameters

Results

docker-compose.yml
version: '3.8'
services:
  nginx_web:
    image: nginx:latest
    container_name: nginx_web
    restart: always
    ports:
      - "8080:80"
    volumes:
      - /my/custom/nginx.conf:/etc/nginx/nginx.conf:ro
    environment:
      - NGINX_PORT=80

This tool converts a Docker run command into a docker-compose.yml file. It supports common flags like --name, -p, -v, -e, --restart, --network, --label, and command arguments. The output uses the selected Compose version and names the service after the container name or 'app' if no name is given. It is useful for developers migrating from ad-hoc containers to declarative Compose configurations.

Which Docker run flags are converted?

The converter recognizes -d/--detach, --name, -p/--publish, -v/--volume, -e/--env, -l/--label, --network, and --restart. It also collects any non-flag tokens after the image as the command array.

Flags like --env-file, --link, --user, -u, --workdir, and -w are skipped along with their values. Unknown flags are skipped on their own, without consuming a following value. This means some Docker run options will not appear in the generated Compose file.

How is the service name determined?

If the Docker run command includes --name, that value becomes the service name and the container_name field in the YAML. If --name is absent, the service name defaults to 'app' and no container_name is set.

For example, docker run --name web nginx produces a service named web with container_name: web, while docker run nginx produces a service named app without container_name.

What are the limitations of the converter?

The converter does not handle all Docker run options. It ignores flags such as --env-file, --link, --user, --workdir, and any unknown flags. It also does not support multi-line commands with backslash continuations perfectly; a backslash followed by a newline is replaced with a space, which may alter quoted arguments.

Environment variables are output as a list of KEY=VALUE strings. If an environment variable value contains an equals sign, the converter splits on the first equals sign and preserves the rest, so KEY=a=b becomes KEY=a=b.

Examples and interpretation (5)

Convert a typical nginx run command with port, volume, environment, and restart

Docker Run Command
docker run -d --name nginx_web -p 8080:80 -v /my/custom/nginx.conf:/etc/nginx/nginx.conf:ro -e NGINX_PORT=80 --restart always nginx:latest
Compose Version
Version 3.8
  1. Input: docker run -d --name nginx_web -p 8080:80 -v /my/custom/nginx.conf:/etc/nginx/nginx.conf:ro -e NGINX_PORT=80 --restart always nginx:latest
  2. Select Compose version 3.8.
  3. The converter parses the flags and generates YAML whose service takes its name from the container name, with the nginx:latest image, the same container name, restart set to always, the 8080:80 port mapping, the read-only configuration file volume, and the port environment variable set to 80.
docker-compose.yml
version: '3.8' services: nginx_web: image: nginx:latest container_name: nginx_web restart: always ports: - "8080:80" volumes: - /my/custom/nginx.conf:/etc/nginx/nginx.conf:ro environment: - NGINX_PORT=80

The output is a valid Compose file for version 3.8. Note that the volume path is used as-is; if it is a host path, it must exist on the Docker host.

Convert a database container with a named volume and a root password variable

Docker Run Command
docker run --name db -e MYSQL_ROOT_PASSWORD=secret -v dbdata:/var/lib/mysql mysql:8.0
Compose Version
Version 3
  1. Input: docker run --name db -e MYSQL_ROOT_PASSWORD=secret -v dbdata:/var/lib/mysql mysql:8.0
  2. Select Compose version 3.
  3. The converter outputs a service named db with image mysql:8.0, the container name db, the volume dbdata:/var/lib/mysql, and the root password environment variable carried over as a list entry.
docker-compose.yml
version: '3' services: db: image: mysql:8.0 container_name: db volumes: - dbdata:/var/lib/mysql environment: - MYSQL_ROOT_PASSWORD=secret

The named volume dbdata is preserved. In Compose, named volumes must be declared in a top-level volumes section if you want to customize them, but the converter does not add that section.

Convert a command with multiple ports and a label

Docker Run Command
docker run -d -p 80:80 -p 443:443 --label com.example.foo=bar nginx
Compose Version
Version 2
  1. Input: docker run -d -p 80:80 -p 443:443 --label com.example.foo=bar nginx
  2. Select Compose version 2.
  3. The converter outputs service app (no --name) with image nginx, ports 80:80 and 443:443, and label com.example.foo=bar.
docker-compose.yml
version: '2' services: app: image: nginx ports: - "80:80" - "443:443" labels: - "com.example.foo=bar"

Because no --name is given, the service name is 'app'. The label is quoted in the YAML to avoid parsing issues.

Convert a command with network mode and restart policy

Docker Run Command
docker run --network host --restart unless-stopped redis:7
Compose Version
Version 3.8
  1. Input: docker run --network host --restart unless-stopped redis:7
  2. Select Compose version 3.8.
  3. The converter outputs a service named app with image redis:7, the restart policy unless-stopped, and the network mode set to host.
docker-compose.yml
version: '3.8' services: app: image: redis:7 restart: unless-stopped network_mode: host

Network mode host is supported in Compose, but it may behave differently on Docker Desktop for Mac and Windows.

Convert a command with arguments

Docker Run Command
docker run -it ubuntu bash -c 'echo hello'
Compose Version
Version 3.8
  1. Input: docker run -it ubuntu bash -c 'echo hello'
  2. Select Compose version 3.8.
  3. The converter outputs service app with image ubuntu and command ["bash", "echo hello"].
docker-compose.yml
version: '3.8' services: app: image: ubuntu command: ["bash", "echo hello"]

The combined -it flag is ignored because it is not one of the recognized flags. The parser treats -c as an unknown flag and skips only the flag itself, so the quoted words echo hello stay together as a single entry in the command list next to bash.

How to use

  1. Enter the full Docker run command in the Docker Run Command text area.
  2. Select the desired Compose version from the dropdown (3.8, 3, or 2).
  3. The converter generates the YAML from the command using the selected Compose version.
  4. Copy the resulting docker-compose.yml content.
  5. Save it to a file named docker-compose.yml in your project directory.
  6. Run docker compose up to start the service.

Frequently asked questions

Does the converter support Docker Compose v2 (without version field)?

No, the converter always includes a version field. You can select version 2, 3, or 3.8, but it will not omit the version line for the modern Compose specification.

What happens if I enter a command that does not start with 'docker run'?

The tool returns an error message: 'Command must start with "docker run".' It does not attempt to parse other Docker commands like docker create or docker exec.

Can I convert a command with a multi-line backslash continuation?

The tokenizer replaces a backslash followed by a newline with a space, so the command is treated as a single line. This may work for simple cases but can break quoted arguments that span lines.

How are environment variables with special characters handled?

Environment variables are output as a list of KEY=VALUE strings. If the value contains an equals sign, the converter splits on the first equals sign and keeps the rest, so KEY=a=b becomes KEY=a=b. Values with spaces are not quoted in the output.

Does the converter add a top-level volumes section for named volumes?

No, it only adds the volume reference under the service. You must manually add a top-level volumes section if you need to configure the named volume.

What is the default service name if --name is not provided?

The default service name is 'app'. The generated YAML will have services: app: and no container_name field.

Complementary Calculators