apiSocket.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package compose
  14. import (
  15. "bytes"
  16. "errors"
  17. "fmt"
  18. "github.com/compose-spec/compose-go/v2/types"
  19. "github.com/docker/cli/cli/config/configfile"
  20. )
  21. // --use-api-socket is not actually supported by the Docker Engine
  22. // but is a client-side hack (see https://github.com/docker/cli/blob/master/cli/command/container/create.go#L246)
  23. // we replicate here by transforming the project model
  24. func (s *composeService) useAPISocket(project *types.Project) (*types.Project, error) {
  25. useAPISocket := false
  26. for _, service := range project.Services {
  27. if service.UseAPISocket {
  28. useAPISocket = true
  29. break
  30. }
  31. }
  32. if !useAPISocket {
  33. return project, nil
  34. }
  35. if s.dockerCli.ServerInfo().OSType == "windows" {
  36. return nil, errors.New("use_api_socket can't be used with a Windows Docker Engine")
  37. }
  38. creds, err := s.dockerCli.ConfigFile().GetAllCredentials()
  39. if err != nil {
  40. return nil, fmt.Errorf("resolving credentials failed: %w", err)
  41. }
  42. newConfig := &configfile.ConfigFile{
  43. AuthConfigs: creds,
  44. }
  45. var configBuf bytes.Buffer
  46. if err := newConfig.SaveToWriter(&configBuf); err != nil {
  47. return nil, fmt.Errorf("saving creds for API socket: %w", err)
  48. }
  49. project.Configs["#apisocket"] = types.ConfigObjConfig{
  50. Content: configBuf.String(),
  51. }
  52. for name, service := range project.Services {
  53. if !service.UseAPISocket {
  54. continue
  55. }
  56. service.Volumes = append(service.Volumes, types.ServiceVolumeConfig{
  57. Type: types.VolumeTypeBind,
  58. Source: "/var/run/docker.sock",
  59. Target: "/var/run/docker.sock",
  60. })
  61. _, envvarPresent := service.Environment["DOCKER_CONFIG"]
  62. // If the DOCKER_CONFIG env var is already present, we assume the client knows
  63. // what they're doing and don't inject the creds.
  64. if !envvarPresent {
  65. // Set our special little location for the config file.
  66. path := "/run/secrets/docker"
  67. service.Environment["DOCKER_CONFIG"] = &path
  68. }
  69. service.Configs = append(service.Configs, types.ServiceConfigObjConfig{
  70. Source: "#apisocket",
  71. Target: "/run/secrets/docker/config.json",
  72. })
  73. project.Services[name] = service
  74. }
  75. return project, nil
  76. }