import "github.com/docker/docker/api/client" cli, err := client.NewEnvClient() if err != nil { fmt.Println("Error connecting to Docker daemon:", err) }
import ( "context" "fmt" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/container" "github.com/docker/docker/client" ) func createContainer(cli *client.Client) error { ctx := context.Background() config := &container.Config{ Image: "ubuntu", Cmd: []string{"echo", "hello world"}, } hostConfig := &container.HostConfig{} containerResp, err := cli.ContainerCreate(ctx, config, hostConfig, nil, nil, "my-container") if err != nil { return fmt.Errorf("Error creating container: %s", err) } fmt.Printf("Created container with ID %s\n", containerResp.ID) return nil }
import "github.com/docker/docker/api/client" cli, err := client.NewEnvClient() if err != nil { log.Fatal("Error creating Docker client:", err) } _, err = cli.NetworkCreate(context.Background(), "my-network", types.NetworkCreate{}) if err != nil { apiErr, ok := err.(client.APIError) if ok { log.Fatalf("API error: %s (%d)", apiErr.Message, apiErr.StatusCode) } else { log.Fatalf("Unknown error: %s", err) } }In this example, we use the `cli.NetworkCreate` method to create a new Docker network with the name "my-network". If an error occurs, we check whether it is an `APIError` by testing the `err` object against the `client.APIError` type. If it is an `APIError`, we log its message and status code. If it is not an `APIError`, we simply log the error message.