shellout.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. "context"
  16. "os/exec"
  17. "github.com/compose-spec/compose-go/v2/types"
  18. "github.com/docker/cli/cli-plugins/manager"
  19. "github.com/docker/cli/cli/context/docker"
  20. "github.com/docker/compose/v2/internal"
  21. "go.opentelemetry.io/otel"
  22. "go.opentelemetry.io/otel/propagation"
  23. )
  24. // prepareShellOut prepare a shell-out command to be ran by Compose
  25. func (s *composeService) prepareShellOut(gctx context.Context, project *types.Project, cmd *exec.Cmd) error {
  26. // exec command with same environment Compose is running
  27. env := types.NewMapping(project.Environment.Values())
  28. // remove DOCKER_CLI_PLUGIN... variable so a docker-cli plugin will detect it run standalone
  29. delete(env, manager.ReexecEnvvar)
  30. // propagate opentelemetry context to child process, see https://github.com/open-telemetry/oteps/blob/main/text/0258-env-context-baggage-carriers.md
  31. carrier := propagation.MapCarrier{}
  32. otel.GetTextMapPropagator().Inject(gctx, &carrier)
  33. env.Merge(types.Mapping(carrier))
  34. env["DOCKER_CONTEXT"] = s.dockerCli.CurrentContext()
  35. env["USER_AGENT"] = "compose/" + internal.Version
  36. metadata, err := s.dockerCli.ContextStore().GetMetadata(s.dockerCli.CurrentContext())
  37. if err != nil {
  38. return err
  39. }
  40. endpoint, err := docker.EndpointFromContext(metadata)
  41. if err != nil {
  42. return err
  43. }
  44. actualHost := s.dockerCli.DockerEndpoint().Host
  45. if endpoint.Host != actualHost {
  46. // We are running with `--host` or `DOCKER_HOST` which overrides selected context
  47. env["DOCKER_HOST"] = actualHost
  48. }
  49. cmd.Env = env.Values()
  50. return nil
  51. }