compose-go
compose-go copied to clipboard
Label not added when lables field is not defined in `docker-compose.yml`
When docker compose file does not have labels field defined, adding labels does not work, here is the example of the code:
package main
import (
"context"
"fmt"
"log"
"github.com/compose-spec/compose-go/v2/cli"
)
func main() {
composeFilePath := "./docker-compose-error.yml"
projectName := "my_project"
ctx := context.Background()
options, err := cli.NewProjectOptions(
[]string{composeFilePath},
cli.WithOsEnv,
cli.WithDotEnv,
cli.WithName(projectName),
)
if err != nil {
log.Fatal(err)
}
project, err := cli.ProjectFromOptions(ctx, options)
if err != nil {
log.Fatal(err)
}
// Add test: "true" label to services
for _, svc := range project.Services {
svc.Labels = svc.Labels.Add("test", "true")
}
// Print all labels of postgres service
fmt.Println(project.Services["postgres"].Labels)
}
With the follwing docker-compose-error.yml
:
services:
postgres:
environment:
- POSTGRES_DB=goby_test
- POSTGRES_USER=postgres
image: 'postgres:9.3.17'
ports:
- '5432:5432'
The ouptut of this program is map[]
, while it should be map[test:true]
as we added the test=true
label.