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
go.getarcane.app/docker/convert- Reference
- pkg.go.dev
go get go.getarcane.app/docker/convert@latestConvert in one call
Section titled “Convert in one call”import ( "go.getarcane.app/docker/convert" converttypes "go.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, and
Options.RenderWarnings to add warnings as # comments at the top of the YAML.
Step by step
Section titled “Step by step”Convert is a pipeline of three public steps you can call yourself:
cmds, err := convert.Parse(input, converttypes.ParseOptions{}) // []RunCommanddoc, err := convert.Build(cmds, converttypes.Options{}) // *Documentyaml, err := convert.Marshal(doc, converttypes.MarshalOptions{}) // []byteThis lets you inspect or change the intermediate RunCommand (image, name, command, entrypoint,
and flags) or Document (services, networks, volumes, and warnings).
Errors
Section titled “Errors”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.}