Docker Run to Docker Compose Converter
Convert docker run commands into docker-compose.yaml format.
Docker Run Command
Overview
The Docker Run to Docker Compose tool is an online utility built with Blazor (C#) that converts a docker run command into an equivalent Docker Compose YAML configuration.
This tool is designed for developers and DevOps engineers who want to migrate from one-off docker run commands to a more maintainable, version-controlled, and reusable docker-compose.yml setup.
What the Tool Does
This tool analyzes a docker run command and translates it into a Docker Compose service definition by:
- Parsing common
docker runflags and arguments - Mapping them to their corresponding Docker Compose keys
- Generating a valid YAML configuration
- Making container setups easier to maintain and share
The output can be used directly as a docker-compose.yml file or as part of a larger Compose configuration.
How to Use the Tool
Enter a
docker runCommand- Paste a full
docker runcommand into the input field
- Paste a full
Generate Docker Compose
- Click the Generate button
Copy the Output
- The equivalent Docker Compose configuration will appear in YAML format
- Copy and save it as
docker-compose.yml
Key Terms Explained
docker run
docker run is a command used to start a container from an image with specific runtime options such as ports, volumes, environment variables, and restart policies.
Example:
docker run -d \
--name web \
-p 8080:80 \
-v ./data:/var/www/html \
-e APP_ENV=production \
nginx
Docker Compose
Docker Compose is a tool for defining and running multi-container Docker applications using a YAML file. It simplifies container configuration and orchestration.
docker-compose.yml
A YAML file that defines services, networks, and volumes for Docker containers.
Example Conversion
Input: docker run Command
docker run -d \
--name myapp \
-p 3000:3000 \
-v ./app:/app \
-e NODE_ENV=production \
--restart unless-stopped \
node:18
Output: Docker Compose (YAML)
version: "3.9"
services:
myapp:
image: node:18
container_name: myapp
ports:
- "3000:3000"
volumes:
- ./app:/app
environment:
NODE_ENV: production
restart: unless-stopped
Common Flag Mapping
| docker run Flag | Docker Compose Equivalent |
|---|---|
--name |
container_name |
-p, --publish |
ports |
-v, --volume |
volumes |
-e, --env |
environment |
--restart |
restart |
-d |
(implicit in docker compose up -d) |
Example: Implementing the Conversion Logic in C#
Below is a simplified example showing how you might parse a docker run command and generate a Docker Compose YAML structure in C#.
1. Conversion Function
using System.Text;
public static class DockerRunConverter
{
public static string ConvertToCompose(
string image,
string serviceName,
string port,
string volume,
string envKey,
string envValue,
string restartPolicy)
{
var yaml = new StringBuilder();
yaml.AppendLine("version: \"3.9\"");
yaml.AppendLine();
yaml.AppendLine("services:");
yaml.AppendLine($" {serviceName}:");
yaml.AppendLine($" image: {image}");
yaml.AppendLine($" container_name: {serviceName}");
if (!string.IsNullOrEmpty(port))
{
yaml.AppendLine(" ports:");
yaml.AppendLine($" - \"{port}\"");
}
if (!string.IsNullOrEmpty(volume))
{
yaml.AppendLine(" volumes:");
yaml.AppendLine($" - {volume}");
}
if (!string.IsNullOrEmpty(envKey))
{
yaml.AppendLine(" environment:");
yaml.AppendLine($" {envKey}: {envValue}");
}
if (!string.IsNullOrEmpty(restartPolicy))
{
yaml.AppendLine($" restart: {restartPolicy}");
}
return yaml.ToString();
}
}
2. Usage Example
string yaml = DockerRunConverter.ConvertToCompose(
image: "nginx",
serviceName: "web",
port: "8080:80",
volume: "./html:/usr/share/nginx/html",
envKey: "APP_ENV",
envValue: "production",
restartPolicy: "always"
);
Why Convert to Docker Compose?
- Easier to maintain and version control
- Better suited for multi-container setups
- Cleaner configuration compared to long CLI commands
- Ideal for CI/CD pipelines and team collaboration
Common Use Cases
- Migrating existing Docker commands to Compose
- Sharing container setups with teammates
- Creating reproducible development environments
- Preparing services for orchestration
Conclusion
The Docker Run to Docker Compose tool simplifies the transition from ad-hoc container commands to structured, reusable Docker Compose configurations. Whether you use the online Blazor-based tool or implement the conversion logic yourself in C#, it helps improve maintainability and clarity in containerized workflows.