shellout.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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, env types.Mapping, cmd *exec.Cmd) error {
  26. env = env.Clone()
  27. // remove DOCKER_CLI_PLUGIN... variable so a docker-cli plugin will detect it run standalone
  28. delete(env, manager.ReexecEnvvar)
  29. // propagate opentelemetry context to child process, see https://github.com/open-telemetry/oteps/blob/main/text/0258-env-context-baggage-carriers.md
  30. carrier := propagation.MapCarrier{}
  31. otel.GetTextMapPropagator().Inject(gctx, &carrier)
  32. env.Merge(types.Mapping(carrier))
  33. env["DOCKER_CONTEXT"] = s.dockerCli.CurrentContext()
  34. env["USER_AGENT"] = "compose/" + internal.Version
  35. metadata, err := s.dockerCli.ContextStore().GetMetadata(s.dockerCli.CurrentContext())
  36. if err != nil {
  37. return err
  38. }
  39. endpoint, err := docker.EndpointFromContext(metadata)
  40. if err != nil {
  41. return err
  42. }
  43. actualHost := s.dockerCli.DockerEndpoint().Host
  44. if endpoint.Host != actualHost {
  45. // We are running with `--host` or `DOCKER_HOST` which overrides selected context
  46. env["DOCKER_HOST"] = actualHost
  47. }
  48. cmd.Env = env.Values()
  49. return nil
  50. }