--use-api-socket is not actually supported by the Docker Engine but is a client-side hack (see https://github.com/docker/cli/blob/master/cli/command/container/create.go#L246) we replicate here by transforming the project model
(project *types.Project)
| 30 | // we replicate here by transforming the project model |
| 31 | |
| 32 | func (s *composeService) useAPISocket(project *types.Project) (*types.Project, error) { |
| 33 | useAPISocket := false |
| 34 | for _, service := range project.Services { |
| 35 | if service.UseAPISocket { |
| 36 | useAPISocket = true |
| 37 | break |
| 38 | } |
| 39 | } |
| 40 | if !useAPISocket { |
| 41 | return project, nil |
| 42 | } |
| 43 | |
| 44 | if s.getContextInfo().ServerOSType() == "windows" { |
| 45 | return nil, errors.New("use_api_socket can't be used with a Windows Docker Engine") |
| 46 | } |
| 47 | |
| 48 | creds, err := s.configFile().GetAllCredentials() |
| 49 | if err != nil { |
| 50 | return nil, fmt.Errorf("resolving credentials failed: %w", err) |
| 51 | } |
| 52 | |
| 53 | newConfig := &configfile.ConfigFile{ |
| 54 | AuthConfigs: creds, |
| 55 | } |
| 56 | var configBuf bytes.Buffer |
| 57 | if err := newConfig.SaveToWriter(&configBuf); err != nil { |
| 58 | return nil, fmt.Errorf("saving creds for API socket: %w", err) |
| 59 | } |
| 60 | |
| 61 | project.Configs["#apisocket"] = types.ConfigObjConfig{ |
| 62 | Content: configBuf.String(), |
| 63 | } |
| 64 | |
| 65 | for name, service := range project.Services { |
| 66 | if !service.UseAPISocket { |
| 67 | continue |
| 68 | } |
| 69 | service.Volumes = append(service.Volumes, types.ServiceVolumeConfig{ |
| 70 | Type: types.VolumeTypeBind, |
| 71 | Source: "/var/run/docker.sock", |
| 72 | Target: "/var/run/docker.sock", |
| 73 | }) |
| 74 | |
| 75 | _, envvarPresent := service.Environment["DOCKER_CONFIG"] |
| 76 | |
| 77 | // If the DOCKER_CONFIG env var is already present, we assume the client knows |
| 78 | // what they're doing and don't inject the creds. |
| 79 | if !envvarPresent { |
| 80 | // Set our special little location for the config file. |
| 81 | path := "/run/secrets/docker" |
| 82 | service.Environment["DOCKER_CONFIG"] = &path |
| 83 | } |
| 84 | |
| 85 | service.Configs = append(service.Configs, types.ServiceConfigObjConfig{ |
| 86 | Source: "#apisocket", |
| 87 | Target: "/run/secrets/docker/config.json", |
| 88 | }) |
| 89 | project.Services[name] = service |
no test coverage detected