run.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/api/compose"
  18. "github.com/compose-spec/compose-go/types"
  19. apitypes "github.com/docker/docker/api/types"
  20. "github.com/docker/docker/api/types/container"
  21. "github.com/docker/docker/api/types/filters"
  22. moby "github.com/docker/docker/pkg/stringid"
  23. )
  24. func (s *composeService) RunOneOffContainer(ctx context.Context, project *types.Project, opts compose.RunOptions) (int, error) {
  25. service, err := project.GetService(opts.Service)
  26. if err != nil {
  27. return 0, err
  28. }
  29. applyRunOptions(&service, opts)
  30. slug := moby.GenerateRandomID()
  31. if service.ContainerName == "" {
  32. service.ContainerName = fmt.Sprintf("%s_%s_run_%s", project.Name, service.Name, moby.TruncateID(slug))
  33. }
  34. service.Scale = 1
  35. service.StdinOpen = true
  36. service.Labels = service.Labels.Add(slugLabel, slug)
  37. service.Labels = service.Labels.Add(oneoffLabel, "True")
  38. if err := s.ensureImagesExists(ctx, project); err != nil { // all dependencies already checked, but might miss service img
  39. return 0, err
  40. }
  41. if err := s.waitDependencies(ctx, project, service); err != nil {
  42. return 0, err
  43. }
  44. if err := s.createContainer(ctx, project, service, service.ContainerName, 1, opts.AutoRemove); err != nil {
  45. return 0, err
  46. }
  47. containerID := service.ContainerName
  48. if opts.Detach {
  49. err := s.apiClient.ContainerStart(ctx, containerID, apitypes.ContainerStartOptions{})
  50. if err != nil {
  51. return 0, err
  52. }
  53. fmt.Fprintln(opts.Writer, containerID)
  54. return 0, nil
  55. }
  56. containers, err := s.apiClient.ContainerList(ctx, apitypes.ContainerListOptions{
  57. Filters: filters.NewArgs(
  58. filters.Arg("label", fmt.Sprintf("%s=%s", slugLabel, slug)),
  59. ),
  60. All: true,
  61. })
  62. if err != nil {
  63. return 0, err
  64. }
  65. oneoffContainer := containers[0]
  66. err = s.attachContainerStreams(ctx, oneoffContainer, service.Tty, opts.Reader, opts.Writer)
  67. if err != nil {
  68. return 0, err
  69. }
  70. err = s.apiClient.ContainerStart(ctx, containerID, apitypes.ContainerStartOptions{})
  71. if err != nil {
  72. return 0, err
  73. }
  74. statusC, errC := s.apiClient.ContainerWait(context.Background(), oneoffContainer.ID, container.WaitConditionNotRunning)
  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(service *types.ServiceConfig, opts compose.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. service.Environment.OverrideBy(opts.EnvironmentMap())
  99. }
  100. for k, v := range opts.Labels {
  101. service.Labels.Add(k, v)
  102. }
  103. }