123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- package main
- import (
- "context"
- "os"
- "github.com/docker/docker/api/types"
- "github.com/docker/docker/api/types/container"
- "github.com/docker/docker/client"
- )
- func startContainer(cfg *container.Config, hostCfg *container.HostConfig, name string) (string, error) {
- c, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
- if err != nil {
- return "", err
- }
- defer c.Close()
- if !isDarwin {
- hostCfg.NetworkMode = "host"
- }
- container, err := c.ContainerCreate(context.Background(), cfg, hostCfg, nil, nil, name)
- if err != nil {
- return "", err
- }
- if err = c.ContainerStart(context.Background(), container.ID, types.ContainerStartOptions{}); err != nil {
- return "", err
- }
- response, err := c.ContainerAttach(context.Background(), container.ID, types.ContainerAttachOptions{
- Stdout: true,
- Stderr: true,
- Logs: true,
- })
- if err != nil {
- return "", err
- }
- go func() {
- response.Reader.WriteTo(os.Stderr)
- }()
- return container.ID, nil
- }
- func cleanContainer(id string) error {
- c, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
- if err != nil {
- return err
- }
- defer c.Close()
- removeOpts := types.ContainerRemoveOptions{Force: true}
- return c.ContainerRemove(context.Background(), id, removeOpts)
- }
|