You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello @feeeei
If you debug step by step your code, you'll see that Compose isn't able to retrieve your container here and this because the projectFilter(project.Name)is looking for containers having a specific Compose label com.docker.compose.project.
Compose relies on labels being set on containers to track resources, and those labels are stored in the Project structure, you can see an exemple here
Compose isn't build to be use as an API, we don't recommend to programmatically building types.Project, the best option is to use ToProject function which will create this types.Project for you.
Something like this might be a helpful building block. Will still need to work out how to configure it properly, it seems the naming and services have some role in identifying existing deploys? WIP.
package main
import (
"context"
"log/slog"
"os"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/flags"
cmdCompose "github.com/docker/compose/v2/cmd/compose"
"github.com/docker/compose/v2/pkg/api"
"github.com/docker/compose/v2/pkg/compose"
)
func composeFunc() {
// Create a Docker client
cli, err := command.NewDockerCli()
if err != nil {
slog.Error(err.Error())
os.Exit(1)
}
// Initialize the Docker client with the default options
err = cli.Initialize(flags.NewClientOptions())
if err != nil {
slog.Error(err.Error())
os.Exit(1)
}
// Create the compose API service instance with the Docker cli
apiService := compose.NewComposeService(cli)
// Create a default project options struct
projectOptions := cmdCompose.ProjectOptions{}
// Turn projectOptions into a project with default values
projectType, err := projectOptions.ToProject(cli, []string{})
if err != nil {
slog.Error(err.Error())
os.Exit(1)
}
// Set the compose file to use
projectType.ComposeFiles = []string{"./docker-compose-1.yml"}
// Create a default options struct for the up command
upOptions := api.UpOptions{}
// Run the up command
err = apiService.Up(context.Background(), projectType, upOptions)
if err != nil {
slog.Error(err.Error())
os.Exit(1)
}
}
Description
I intend to use compose api.Service to create a container, but I receive an error message: "err: no container found for project 'hello': not found."
The expectation is to create successfully, and I anticipate that the RecreateForce parameter will take effect upon repeated execution.
Steps To Reproduce
This is my minimal reproduction code.
Compose Version
Docker Environment
Anything else?
The text was updated successfully, but these errors were encountered: