1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
package main
import (
"encoding/json"
"fmt"
"os"
"github.com/containers/libpod/cmd/podman/cliconfig"
"github.com/containers/libpod/cmd/podman/libpodruntime"
"github.com/containers/libpod/libpod"
cc "github.com/containers/libpod/pkg/spec"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var (
startCommand cliconfig.StartValues
startDescription = `
podman start
Starts one or more containers. The container name or ID can be used.
`
_startCommand = &cobra.Command{
Use: "start",
Short: "Start one or more containers",
Long: startDescription,
RunE: func(cmd *cobra.Command, args []string) error {
startCommand.InputArgs = args
startCommand.GlobalFlags = MainGlobalOpts
return startCmd(&startCommand)
},
Example: "CONTAINER-NAME [CONTAINER-NAME ...]",
}
)
func init() {
startCommand.Command = _startCommand
flags := startCommand.Flags()
flags.BoolVarP(&startCommand.Attach, "attach", "a", false, "Attach container's STDOUT and STDERR")
flags.StringVar(&startCommand.DetachKeys, "detach-keys", "", "Override the key sequence for detaching a container. Format is a single character [a-Z] or ctrl-<value> where <value> is one of: a-z, @, ^, [, , or _")
flags.BoolVarP(&startCommand.Interactive, "interactive", "i", false, "Keep STDIN open even if not attached")
flags.BoolVarP(&startCommand.Latest, "latest", "l", false, "Act on the latest container podman is aware of")
flags.BoolVar(&startCommand.SigProxy, "sig-proxy", true, "Proxy received signals to the process (default true if attaching, false otherwise)")
}
func startCmd(c *cliconfig.StartValues) error {
args := c.InputArgs
if len(args) < 1 && !c.Latest {
return errors.Errorf("you must provide at least one container name or id")
}
attach := c.Attach
if len(args) > 1 && attach {
return errors.Errorf("you cannot start and attach multiple containers at once")
}
sigProxy := c.SigProxy
if sigProxy && !attach {
if c.Flag("sig-proxy").Changed {
return errors.Wrapf(libpod.ErrInvalidArg, "you cannot use sig-proxy without --attach")
} else {
sigProxy = false
}
}
runtime, err := libpodruntime.GetRuntime(&c.PodmanCommand)
if err != nil {
return errors.Wrapf(err, "error creating libpod runtime")
}
defer runtime.Shutdown(false)
if c.Latest {
lastCtr, err := runtime.GetLatestContainer()
if err != nil {
return errors.Wrapf(err, "unable to get latest container")
}
args = append(args, lastCtr.ID())
}
ctx := getContext()
var lastError error
for _, container := range args {
ctr, err := runtime.LookupContainer(container)
if err != nil {
if lastError != nil {
fmt.Fprintln(os.Stderr, lastError)
}
lastError = errors.Wrapf(err, "unable to find container %s", container)
continue
}
ctrState, err := ctr.State()
if err != nil {
return errors.Wrapf(err, "unable to get container state")
}
ctrRunning := ctrState == libpod.ContainerStateRunning
if attach {
inputStream := os.Stdin
if !c.Interactive {
inputStream = nil
}
// attach to the container and also start it not already running
err = startAttachCtr(ctr, os.Stdout, os.Stderr, inputStream, c.DetachKeys, sigProxy, !ctrRunning)
if ctrRunning {
return err
}
if err != nil {
return errors.Wrapf(err, "unable to start container %s", ctr.ID())
}
if ecode, err := ctr.Wait(); err != nil {
logrus.Errorf("unable to get exit code of container %s: %q", ctr.ID(), err)
} else {
exitCode = int(ecode)
}
return ctr.Cleanup(ctx)
}
if ctrRunning {
fmt.Println(ctr.ID())
continue
}
// Handle non-attach start
if err := ctr.Start(ctx); err != nil {
var createArtifact cc.CreateConfig
artifact, artifactErr := ctr.GetArtifact("create-config")
if artifactErr == nil {
if jsonErr := json.Unmarshal(artifact, &createArtifact); jsonErr != nil {
logrus.Errorf("unable to detect if container %s should be deleted", ctr.ID())
}
if createArtifact.Rm {
if rmErr := runtime.RemoveContainer(ctx, ctr, true); rmErr != nil {
logrus.Errorf("unable to remove container %s after it failed to start", ctr.ID())
}
}
}
if lastError != nil {
fmt.Fprintln(os.Stderr, lastError)
}
lastError = errors.Wrapf(err, "unable to start container %q", container)
continue
}
fmt.Println(container)
}
return lastError
}
|