Skip to content

docker/convert

Convert docker run commands into Docker Compose YAML.

docker/convert turns one or more docker run commands into a Docker Compose document. Commands are parsed as text and never executed, so shell substitutions such as $(...) are copied through literally.

Module
kit.getarcane.app/docker/convert
Reference
pkg.go.dev
Terminal window
go get kit.getarcane.app/docker/convert@latest
import (
"kit.getarcane.app/docker/convert"
converttypes "kit.getarcane.app/docker/convert/types"
)
result, err := convert.Convert(
`docker run -d --name web -p 8080:80 -v data:/usr/share/nginx/html nginx:1.27`,
converttypes.Options{},
)
if err != nil {
return err
}
fmt.Println(string(result.YAML))

Result contains:

Field Description
YAML The rendered Compose file
Project The same document as a parsed compose-go *types.Project
Services Name and image of each generated service
EnvFile .env contents collected from every -e KEY=VALUE flag, or nil when there are none
Warnings Flags that could not be represented in Compose

Set Options.ExistingComposeYAML to merge the new services into an existing file. Options embeds MarshalOptions, so set RenderWarnings to add warnings as # comments at the top of the YAML:

result, err := convert.Convert(input, converttypes.Options{
ExistingComposeYAML: existing,
MarshalOptions: converttypes.MarshalOptions{
RenderWarnings: true,
},
})

Convert is a pipeline of three public steps you can call yourself:

cmds, err := convert.Parse(input) // []RunCommand
doc, err := convert.Build(cmds, converttypes.Options{}) // *Document
yaml, err := convert.Marshal(doc, converttypes.MarshalOptions{}) // []byte

Parse takes only the input string. Build reads ExistingComposeYAML from Options, and Marshal reads RenderWarnings from MarshalOptions.

This lets you inspect or change the intermediate RunCommand (image, name, command, entrypoint, and flags) or Document (services, networks, volumes, and warnings).

Errors wrap one of two sentinels:

switch {
case errors.Is(err, converttypes.ErrParse):
// The input is not a valid docker run command, e.g. no image.
case errors.Is(err, converttypes.ErrConversion):
// Parsed, but could not be represented as Compose.
}