go-composegen is a Go library that generates a Docker Compose file from existing Docker containers. This library inspects running containers, gathers configuration details, and creates a docker-compose.yml file that can be used to redeploy the containers. It supports filtering containers based on names, generating associated networks and volumes, and exporting the result in YAML format.
- Generate Docker Compose: Create a
docker-compose.ymlfile from currently running Docker containers. - Customizable Filtering: Filter containers by name using regular expressions.
- Network and Volume Support: Automatically includes network and volume configurations for each container.
- Flexible Volume Handling: Choose to include bind mounts, Docker volumes, or both.
- YAML Export: Outputs the Compose configuration in YAML format.
- Import ComposeGen in your Go project:
import "github.com/nodeharbor/go-composegen"- Use the
GenerateComposeFilefunction to generate a Compose file:
composeFile, err := composegen.GenerateComposeFile(cli, true, "my-filter", true)
if err != nil {
log.Fatalf("Error generating compose file: %v", err)
}
fmt.Println(composeFile)cli: Docker client.includeAllContainers: Whether to include all containers' network information.containerFilter: Regular expression to filter containers by name.createVolumes: Whether to include volumes.
cli, _ := client.NewClientWithOpts(client.FromEnv)
composeFile, err := composegen.GenerateComposeFile(cli, false, "", true)
if err != nil {
log.Fatalf("Error: %v", err)
}
fmt.Println(composeFile)This will generate a Docker Compose file for all running containers and include any volumes used.
To install the library, use go get:
go get github.com/nodeharbor/go-composegenFor any inquiries or support, please contact the maintainer at:
hello@nodeharbor.io
This project is licensed under the MIT License.