run.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. "fmt"
  17. "io"
  18. "github.com/docker/compose-cli/pkg/api"
  19. "github.com/compose-spec/compose-go/types"
  20. moby "github.com/docker/docker/api/types"
  21. "github.com/docker/docker/api/types/container"
  22. "github.com/docker/docker/pkg/ioutils"
  23. "github.com/docker/docker/pkg/stringid"
  24. "github.com/moby/term"
  25. )
  26. func (s *composeService) RunOneOffContainer(ctx context.Context, project *types.Project, opts api.RunOptions) (int, error) {
  27. observedState, err := s.getContainers(ctx, project.Name, oneOffInclude, true)
  28. if err != nil {
  29. return 0, err
  30. }
  31. service, err := project.GetService(opts.Service)
  32. if err != nil {
  33. return 0, err
  34. }
  35. applyRunOptions(project, &service, opts)
  36. slug := stringid.GenerateRandomID()
  37. if service.ContainerName == "" {
  38. service.ContainerName = fmt.Sprintf("%s_%s_run_%s", project.Name, service.Name, stringid.TruncateID(slug))
  39. }
  40. service.Scale = 1
  41. service.StdinOpen = true
  42. service.Restart = ""
  43. if service.Deploy != nil {
  44. service.Deploy.RestartPolicy = nil
  45. }
  46. service.Labels = service.Labels.Add(api.SlugLabel, slug)
  47. service.Labels = service.Labels.Add(api.OneoffLabel, "True")
  48. if err := s.ensureImagesExists(ctx, project, observedState, false); err != nil { // all dependencies already checked, but might miss service img
  49. return 0, err
  50. }
  51. if err := s.waitDependencies(ctx, project, service); err != nil {
  52. return 0, err
  53. }
  54. created, err := s.createContainer(ctx, project, service, service.ContainerName, 1, opts.AutoRemove, opts.UseNetworkAliases)
  55. if err != nil {
  56. return 0, err
  57. }
  58. containerID := created.ID
  59. if opts.Detach {
  60. err := s.apiClient.ContainerStart(ctx, containerID, moby.ContainerStartOptions{})
  61. if err != nil {
  62. return 0, err
  63. }
  64. fmt.Fprintln(opts.Stdout, containerID)
  65. return 0, nil
  66. }
  67. r, err := s.getEscapeKeyProxy(opts.Stdin)
  68. if err != nil {
  69. return 0, err
  70. }
  71. restore, detachC, err := s.attachContainerStreams(ctx, containerID, service.Tty, r, opts.Stdout, opts.Stderr)
  72. if err != nil {
  73. return 0, err
  74. }
  75. defer restore()
  76. statusC, errC := s.apiClient.ContainerWait(context.Background(), containerID, container.WaitConditionNextExit)
  77. err = s.apiClient.ContainerStart(ctx, containerID, moby.ContainerStartOptions{})
  78. if err != nil {
  79. return 0, err
  80. }
  81. s.monitorTTySize(ctx, containerID, s.apiClient.ContainerResize)
  82. select {
  83. case status := <-statusC:
  84. return int(status.StatusCode), nil
  85. case <-detachC:
  86. return 0, nil
  87. case err := <-errC:
  88. return 0, err
  89. }
  90. }
  91. func (s *composeService) getEscapeKeyProxy(r io.ReadCloser) (io.ReadCloser, error) {
  92. var escapeKeys = []byte{16, 17}
  93. if s.configFile.DetachKeys != "" {
  94. customEscapeKeys, err := term.ToBytes(s.configFile.DetachKeys)
  95. if err != nil {
  96. return nil, err
  97. }
  98. escapeKeys = customEscapeKeys
  99. }
  100. return ioutils.NewReadCloserWrapper(term.NewEscapeProxy(r, escapeKeys), r.Close), nil
  101. }
  102. func applyRunOptions(project *types.Project, service *types.ServiceConfig, opts api.RunOptions) {
  103. service.Tty = opts.Tty
  104. service.ContainerName = opts.Name
  105. if len(opts.Command) > 0 {
  106. service.Command = opts.Command
  107. }
  108. if len(opts.User) > 0 {
  109. service.User = opts.User
  110. }
  111. if len(opts.WorkingDir) > 0 {
  112. service.WorkingDir = opts.WorkingDir
  113. }
  114. if len(opts.Entrypoint) > 0 {
  115. service.Entrypoint = opts.Entrypoint
  116. }
  117. if len(opts.Environment) > 0 {
  118. env := types.NewMappingWithEquals(opts.Environment)
  119. projectEnv := env.Resolve(func(s string) (string, bool) {
  120. v, ok := project.Environment[s]
  121. return v, ok
  122. }).RemoveEmpty()
  123. service.Environment.OverrideBy(projectEnv)
  124. }
  125. for k, v := range opts.Labels {
  126. service.Labels.Add(k, v)
  127. }
  128. }