2020-12-03 09:24:15 +01:00
|
|
|
/*
|
|
|
|
Copyright 2020 Docker Compose CLI authors
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package compose
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-07-18 22:51:54 +02:00
|
|
|
"errors"
|
2020-12-03 09:24:15 +01:00
|
|
|
"fmt"
|
2023-05-30 15:16:22 +02:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2025-05-27 14:27:45 +02:00
|
|
|
"slices"
|
2022-03-15 15:46:27 +01:00
|
|
|
|
2023-11-08 10:19:24 +01:00
|
|
|
"github.com/compose-spec/compose-go/v2/types"
|
2022-03-09 17:52:30 +01:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
cmd "github.com/docker/cli/cli/command/container"
|
2021-09-15 14:33:40 +02:00
|
|
|
"github.com/docker/compose/v2/pkg/api"
|
2021-06-14 16:26:14 +02:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2020-12-03 09:24:15 +01:00
|
|
|
)
|
|
|
|
|
2021-06-14 16:26:14 +02:00
|
|
|
func (s *composeService) RunOneOffContainer(ctx context.Context, project *types.Project, opts api.RunOptions) (int, error) {
|
2021-08-24 10:50:44 +02:00
|
|
|
containerID, err := s.prepareRun(ctx, project, opts)
|
2021-02-23 10:36:23 +01:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2020-12-03 09:24:15 +01:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:16:22 +02:00
|
|
|
// remove cancellable context signal handler so we can forward signals to container without compose to exit
|
|
|
|
signal.Reset()
|
|
|
|
|
|
|
|
sigc := make(chan os.Signal, 128)
|
|
|
|
signal.Notify(sigc)
|
2023-12-04 10:12:20 -06:00
|
|
|
go cmd.ForwardAllSignals(ctx, s.apiClient(), containerID, sigc)
|
2023-05-30 15:16:22 +02:00
|
|
|
defer signal.Stop(sigc)
|
|
|
|
|
2023-12-18 17:14:08 +00:00
|
|
|
err = cmd.RunStart(ctx, s.dockerCli, &cmd.StartOptions{
|
2023-07-18 22:51:54 +02:00
|
|
|
OpenStdin: !opts.Detach && opts.Interactive,
|
|
|
|
Attach: !opts.Detach,
|
|
|
|
Containers: []string{containerID},
|
|
|
|
})
|
|
|
|
var stErr cli.StatusError
|
|
|
|
if errors.As(err, &stErr) {
|
|
|
|
return stErr.StatusCode, nil
|
2021-09-01 16:41:17 +02:00
|
|
|
}
|
2022-03-09 17:52:30 +01:00
|
|
|
return 0, err
|
2021-09-01 16:41:17 +02:00
|
|
|
}
|
|
|
|
|
2021-08-24 10:50:44 +02:00
|
|
|
func (s *composeService) prepareRun(ctx context.Context, project *types.Project, opts api.RunOptions) (string, error) {
|
2021-07-20 11:39:29 +02:00
|
|
|
service, err := project.GetService(opts.Service)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
applyRunOptions(project, &service, opts)
|
|
|
|
|
2023-01-13 19:07:44 +01:00
|
|
|
if err := s.stdin().CheckTty(opts.Interactive, service.Tty); err != nil {
|
2022-03-15 15:46:27 +01:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2021-07-20 11:39:29 +02:00
|
|
|
slug := stringid.GenerateRandomID()
|
|
|
|
if service.ContainerName == "" {
|
2023-03-08 16:51:50 +00:00
|
|
|
service.ContainerName = fmt.Sprintf("%[1]s%[4]s%[2]s%[4]srun%[4]s%[3]s", project.Name, service.Name, stringid.TruncateID(slug), api.Separator)
|
2020-12-03 09:24:15 +01:00
|
|
|
}
|
2023-11-08 10:19:24 +01:00
|
|
|
one := 1
|
|
|
|
service.Scale = &one
|
2021-07-20 11:39:29 +02:00
|
|
|
service.Restart = ""
|
|
|
|
if service.Deploy != nil {
|
|
|
|
service.Deploy.RestartPolicy = nil
|
|
|
|
}
|
2021-11-24 09:11:27 +01:00
|
|
|
service.CustomLabels = service.CustomLabels.
|
|
|
|
Add(api.SlugLabel, slug).
|
|
|
|
Add(api.OneoffLabel, "True")
|
2021-02-12 17:33:59 +01:00
|
|
|
|
2023-08-30 08:47:09 -04:00
|
|
|
if err := s.ensureImagesExists(ctx, project, opts.Build, opts.QuietPull); err != nil { // all dependencies already checked, but might miss service img
|
2021-07-20 11:39:29 +02:00
|
|
|
return "", err
|
|
|
|
}
|
2021-10-13 12:20:24 +02:00
|
|
|
|
|
|
|
observedState, err := s.getContainers(ctx, project.Name, oneOffInclude, true)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2023-01-26 09:58:30 +01:00
|
|
|
if !opts.NoDeps {
|
2024-09-23 16:05:53 +02:00
|
|
|
if err := s.waitDependencies(ctx, project, service.Name, service.DependsOn, observedState, 0); err != nil {
|
2023-01-26 09:58:30 +01:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
2023-06-28 11:27:58 -04:00
|
|
|
createOpts := createOptions{
|
|
|
|
AutoRemove: opts.AutoRemove,
|
|
|
|
AttachStdin: opts.Interactive,
|
|
|
|
UseNetworkAliases: opts.UseNetworkAliases,
|
|
|
|
Labels: mergeLabels(service.Labels, service.CustomLabels),
|
|
|
|
}
|
|
|
|
|
2024-10-21 12:01:50 +01:00
|
|
|
err = newConvergence(project.ServiceNames(), observedState, nil, nil, s).resolveServiceReferences(&service)
|
2023-09-14 11:35:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2024-10-21 15:37:42 +02:00
|
|
|
created, err := s.createContainer(ctx, project, service, service.ContainerName, -1, createOpts)
|
2021-07-20 11:39:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-03-09 17:52:30 +01:00
|
|
|
return created.ID, nil
|
2021-06-29 10:16:30 +02:00
|
|
|
}
|
|
|
|
|
2021-06-14 16:26:14 +02:00
|
|
|
func applyRunOptions(project *types.Project, service *types.ServiceConfig, opts api.RunOptions) {
|
2021-02-23 10:36:23 +01:00
|
|
|
service.Tty = opts.Tty
|
2022-03-09 17:52:30 +01:00
|
|
|
service.StdinOpen = opts.Interactive
|
2021-02-23 10:36:23 +01:00
|
|
|
service.ContainerName = opts.Name
|
|
|
|
|
|
|
|
if len(opts.Command) > 0 {
|
|
|
|
service.Command = opts.Command
|
|
|
|
}
|
fix linting issues with golangci-lint 1.60.2
pkg/watch/watcher_darwin.go:96:16: Error return value of `d.stream.Start` is not checked (errcheck)
d.stream.Start()
^
pkg/prompt/prompt.go:97:12: Error return value of `fmt.Fprint` is not checked (errcheck)
fmt.Fprint(u.stdout, message)
^
pkg/prompt/prompt.go:99:12: Error return value of `fmt.Scanln` is not checked (errcheck)
fmt.Scanln(&answer)
^
cmd/formatter/logs.go:118:15: Error return value of `fmt.Fprintf` is not checked (errcheck)
fmt.Fprintf(w, "%s%s%s\n", p.prefix, timestamp, line)
^
cmd/formatter/logs.go:120:15: Error return value of `fmt.Fprintf` is not checked (errcheck)
fmt.Fprintf(w, "%s%s\n", p.prefix, line)
^
pkg/progress/json.go:67:15: Error return value of `fmt.Fprintln` is not checked (errcheck)
fmt.Fprintln(p.out, string(marshal))
^
pkg/progress/json.go:87:15: Error return value of `fmt.Fprintln` is not checked (errcheck)
fmt.Fprintln(p.out, string(marshal))
^
pkg/progress/plain.go:47:14: Error return value of `fmt.Fprintln` is not checked (errcheck)
fmt.Fprintln(p.out, prefix, e.ID, e.Text, e.StatusText)
^
pkg/progress/tty.go:162:12: Error return value of `fmt.Fprint` is not checked (errcheck)
fmt.Fprint(w.out, b.Column(0).ANSI)
^
pkg/progress/tty.go:165:12: Error return value of `fmt.Fprint` is not checked (errcheck)
fmt.Fprint(w.out, aec.Hide)
^
pkg/compose/attach.go:53:13: Error return value of `fmt.Fprintf` is not checked (errcheck)
fmt.Fprintf(s.stdout(), "Attaching to %s\n", strings.Join(names, ", "))
^
pkg/compose/compose.go:194:6: emptyStringTest: replace `len(dependencies) > 0` with `dependencies != ""` (gocritic)
if len(dependencies) > 0 {
^
pkg/compose/convergence.go:461:2: builtinShadow: shadowing of predeclared identifier: max (gocritic)
max := 0
^
pkg/compose/run.go:127:5: emptyStringTest: replace `len(opts.User) > 0` with `opts.User != ""` (gocritic)
if len(opts.User) > 0 {
^
pkg/compose/run.go:139:5: emptyStringTest: replace `len(opts.WorkingDir) > 0` with `opts.WorkingDir != ""` (gocritic)
if len(opts.WorkingDir) > 0 {
^
pkg/compose/viz.go:91:8: emptyStringTest: replace `len(portConfig.HostIP) > 0` with `portConfig.HostIP != ""` (gocritic)
if len(portConfig.HostIP) > 0 {
^
cmd/compatibility/convert.go:66:6: emptyStringTest: replace `len(arg) > 0` with `arg != ""` (gocritic)
if len(arg) > 0 && arg[0] != '-' {
^
pkg/e2e/watch_test.go:208:25: printf: non-constant format string in call to gotest.tools/v3/poll.Continue (govet)
return poll.Continue(res.Combined())
^
pkg/e2e/watch_test.go:290:25: printf: non-constant format string in call to gotest.tools/v3/poll.Continue (govet)
return poll.Continue(r.Combined())
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-09-11 12:52:55 +02:00
|
|
|
if opts.User != "" {
|
2021-02-23 10:36:23 +01:00
|
|
|
service.User = opts.User
|
|
|
|
}
|
2023-06-20 09:03:55 +02:00
|
|
|
|
2023-06-08 11:22:44 +02:00
|
|
|
if len(opts.CapAdd) > 0 {
|
|
|
|
service.CapAdd = append(service.CapAdd, opts.CapAdd...)
|
2025-05-27 14:27:45 +02:00
|
|
|
service.CapDrop = slices.DeleteFunc(service.CapDrop, func(e string) bool { return slices.Contains(opts.CapAdd, e) })
|
2023-06-08 11:22:44 +02:00
|
|
|
}
|
|
|
|
if len(opts.CapDrop) > 0 {
|
|
|
|
service.CapDrop = append(service.CapDrop, opts.CapDrop...)
|
2025-05-27 14:27:45 +02:00
|
|
|
service.CapAdd = slices.DeleteFunc(service.CapAdd, func(e string) bool { return slices.Contains(opts.CapDrop, e) })
|
2023-06-08 11:22:44 +02:00
|
|
|
}
|
fix linting issues with golangci-lint 1.60.2
pkg/watch/watcher_darwin.go:96:16: Error return value of `d.stream.Start` is not checked (errcheck)
d.stream.Start()
^
pkg/prompt/prompt.go:97:12: Error return value of `fmt.Fprint` is not checked (errcheck)
fmt.Fprint(u.stdout, message)
^
pkg/prompt/prompt.go:99:12: Error return value of `fmt.Scanln` is not checked (errcheck)
fmt.Scanln(&answer)
^
cmd/formatter/logs.go:118:15: Error return value of `fmt.Fprintf` is not checked (errcheck)
fmt.Fprintf(w, "%s%s%s\n", p.prefix, timestamp, line)
^
cmd/formatter/logs.go:120:15: Error return value of `fmt.Fprintf` is not checked (errcheck)
fmt.Fprintf(w, "%s%s\n", p.prefix, line)
^
pkg/progress/json.go:67:15: Error return value of `fmt.Fprintln` is not checked (errcheck)
fmt.Fprintln(p.out, string(marshal))
^
pkg/progress/json.go:87:15: Error return value of `fmt.Fprintln` is not checked (errcheck)
fmt.Fprintln(p.out, string(marshal))
^
pkg/progress/plain.go:47:14: Error return value of `fmt.Fprintln` is not checked (errcheck)
fmt.Fprintln(p.out, prefix, e.ID, e.Text, e.StatusText)
^
pkg/progress/tty.go:162:12: Error return value of `fmt.Fprint` is not checked (errcheck)
fmt.Fprint(w.out, b.Column(0).ANSI)
^
pkg/progress/tty.go:165:12: Error return value of `fmt.Fprint` is not checked (errcheck)
fmt.Fprint(w.out, aec.Hide)
^
pkg/compose/attach.go:53:13: Error return value of `fmt.Fprintf` is not checked (errcheck)
fmt.Fprintf(s.stdout(), "Attaching to %s\n", strings.Join(names, ", "))
^
pkg/compose/compose.go:194:6: emptyStringTest: replace `len(dependencies) > 0` with `dependencies != ""` (gocritic)
if len(dependencies) > 0 {
^
pkg/compose/convergence.go:461:2: builtinShadow: shadowing of predeclared identifier: max (gocritic)
max := 0
^
pkg/compose/run.go:127:5: emptyStringTest: replace `len(opts.User) > 0` with `opts.User != ""` (gocritic)
if len(opts.User) > 0 {
^
pkg/compose/run.go:139:5: emptyStringTest: replace `len(opts.WorkingDir) > 0` with `opts.WorkingDir != ""` (gocritic)
if len(opts.WorkingDir) > 0 {
^
pkg/compose/viz.go:91:8: emptyStringTest: replace `len(portConfig.HostIP) > 0` with `portConfig.HostIP != ""` (gocritic)
if len(portConfig.HostIP) > 0 {
^
cmd/compatibility/convert.go:66:6: emptyStringTest: replace `len(arg) > 0` with `arg != ""` (gocritic)
if len(arg) > 0 && arg[0] != '-' {
^
pkg/e2e/watch_test.go:208:25: printf: non-constant format string in call to gotest.tools/v3/poll.Continue (govet)
return poll.Continue(res.Combined())
^
pkg/e2e/watch_test.go:290:25: printf: non-constant format string in call to gotest.tools/v3/poll.Continue (govet)
return poll.Continue(r.Combined())
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-09-11 12:52:55 +02:00
|
|
|
if opts.WorkingDir != "" {
|
2021-02-23 10:36:23 +01:00
|
|
|
service.WorkingDir = opts.WorkingDir
|
|
|
|
}
|
2021-09-08 09:07:18 +02:00
|
|
|
if opts.Entrypoint != nil {
|
2021-02-23 10:36:23 +01:00
|
|
|
service.Entrypoint = opts.Entrypoint
|
2022-09-26 18:08:14 +02:00
|
|
|
if len(opts.Command) == 0 {
|
|
|
|
service.Command = []string{}
|
|
|
|
}
|
2021-02-23 10:36:23 +01:00
|
|
|
}
|
|
|
|
if len(opts.Environment) > 0 {
|
2022-06-08 00:44:12 +02:00
|
|
|
cmdEnv := types.NewMappingWithEquals(opts.Environment)
|
|
|
|
serviceOverrideEnv := cmdEnv.Resolve(func(s string) (string, bool) {
|
2022-08-03 03:56:09 +09:00
|
|
|
v, ok := envResolver(project.Environment)(s)
|
2021-05-03 21:32:51 -03:00
|
|
|
return v, ok
|
|
|
|
}).RemoveEmpty()
|
2025-03-24 13:36:37 +01:00
|
|
|
if service.Environment == nil {
|
|
|
|
service.Environment = types.MappingWithEquals{}
|
|
|
|
}
|
2022-06-08 00:44:12 +02:00
|
|
|
service.Environment.OverrideBy(serviceOverrideEnv)
|
2021-02-23 10:36:23 +01:00
|
|
|
}
|
|
|
|
for k, v := range opts.Labels {
|
2021-09-23 12:00:35 +02:00
|
|
|
service.Labels = service.Labels.Add(k, v)
|
2021-02-23 10:36:23 +01:00
|
|
|
}
|
|
|
|
}
|