run.go 3.6 KB

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