Skip to content

Commit 23e26f4

Browse files
committed
cli/command/container: createContainer(): return container-ID
This function returned the whole response, but we already handled the warnings included in the response as part of the function. All consumers of this function only used the container-ID, so let's simplify and return just that (it's a non-exported func, so we can change the signature again if we really need it). Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent c2c6fbe commit 23e26f4

3 files changed

Lines changed: 26 additions & 26 deletions

File tree

cli/command/container/create.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,11 @@ func runCreate(dockerCli command.Cli, flags *pflag.FlagSet, options *createOptio
104104
reportError(dockerCli.Err(), "create", err.Error(), true)
105105
return cli.StatusError{StatusCode: 125}
106106
}
107-
response, err := createContainer(context.Background(), dockerCli, containerCfg, options)
107+
id, err := createContainer(context.Background(), dockerCli, containerCfg, options)
108108
if err != nil {
109109
return err
110110
}
111-
fmt.Fprintln(dockerCli.Out(), response.ID)
111+
_, _ = fmt.Fprintln(dockerCli.Out(), id)
112112
return nil
113113
}
114114

@@ -185,7 +185,7 @@ func newCIDFile(path string) (*cidFile, error) {
185185
}
186186

187187
//nolint:gocyclo
188-
func createContainer(ctx context.Context, dockerCli command.Cli, containerCfg *containerConfig, opts *createOptions) (*container.CreateResponse, error) {
188+
func createContainer(ctx context.Context, dockerCli command.Cli, containerCfg *containerConfig, opts *createOptions) (containerID string, err error) {
189189
config := containerCfg.Config
190190
hostConfig := containerCfg.HostConfig
191191
networkingConfig := containerCfg.NetworkingConfig
@@ -200,13 +200,13 @@ func createContainer(ctx context.Context, dockerCli command.Cli, containerCfg *c
200200

201201
containerIDFile, err := newCIDFile(hostConfig.ContainerIDFile)
202202
if err != nil {
203-
return nil, err
203+
return "", err
204204
}
205205
defer containerIDFile.Close()
206206

207207
ref, err := reference.ParseAnyReference(config.Image)
208208
if err != nil {
209-
return nil, err
209+
return "", err
210210
}
211211
if named, ok := ref.(reference.Named); ok {
212212
namedRef = reference.TagNameOnly(named)
@@ -215,7 +215,7 @@ func createContainer(ctx context.Context, dockerCli command.Cli, containerCfg *c
215215
var err error
216216
trustedRef, err = image.TrustedReference(ctx, dockerCli, taggedRef)
217217
if err != nil {
218-
return nil, err
218+
return "", err
219219
}
220220
config.Image = reference.FamiliarString(trustedRef)
221221
}
@@ -239,14 +239,14 @@ func createContainer(ctx context.Context, dockerCli command.Cli, containerCfg *c
239239
if opts.platform != "" && versions.GreaterThanOrEqualTo(dockerCli.Client().ClientVersion(), "1.41") {
240240
p, err := platforms.Parse(opts.platform)
241241
if err != nil {
242-
return nil, errors.Wrap(err, "error parsing specified platform")
242+
return "", errors.Wrap(err, "error parsing specified platform")
243243
}
244244
platform = &p
245245
}
246246

247247
if opts.pull == PullImageAlways {
248248
if err := pullAndTagImage(); err != nil {
249-
return nil, err
249+
return "", err
250250
}
251251
}
252252

@@ -262,24 +262,24 @@ func createContainer(ctx context.Context, dockerCli command.Cli, containerCfg *c
262262
}
263263

264264
if err := pullAndTagImage(); err != nil {
265-
return nil, err
265+
return "", err
266266
}
267267

268268
var retryErr error
269269
response, retryErr = dockerCli.Client().ContainerCreate(ctx, config, hostConfig, networkingConfig, platform, opts.name)
270270
if retryErr != nil {
271-
return nil, retryErr
271+
return "", retryErr
272272
}
273273
} else {
274-
return nil, err
274+
return "", err
275275
}
276276
}
277277

278278
for _, w := range response.Warnings {
279-
fmt.Fprintf(dockerCli.Err(), "WARNING: %s\n", w)
279+
_, _ = fmt.Fprintf(dockerCli.Err(), "WARNING: %s\n", w)
280280
}
281281
err = containerIDFile.Write(response.ID)
282-
return &response, err
282+
return response.ID, err
283283
}
284284

285285
func warnOnOomKillDisable(hostConfig container.HostConfig, stderr io.Writer) {

cli/command/container/create_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,18 +93,18 @@ func TestCreateContainerImagePullPolicy(t *testing.T) {
9393
cases := []struct {
9494
PullPolicy string
9595
ExpectedPulls int
96-
ExpectedBody container.CreateResponse
96+
ExpectedID string
9797
ExpectedErrMsg string
9898
ResponseCounter int
9999
}{
100100
{
101101
PullPolicy: PullImageMissing,
102102
ExpectedPulls: 1,
103-
ExpectedBody: container.CreateResponse{ID: containerID},
103+
ExpectedID: containerID,
104104
}, {
105105
PullPolicy: PullImageAlways,
106106
ExpectedPulls: 1,
107-
ExpectedBody: container.CreateResponse{ID: containerID},
107+
ExpectedID: containerID,
108108
ResponseCounter: 1, // This lets us return a container on the first pull
109109
}, {
110110
PullPolicy: PullImageNever,
@@ -142,7 +142,7 @@ func TestCreateContainerImagePullPolicy(t *testing.T) {
142142
},
143143
}
144144
fakeCLI := test.NewFakeCli(client)
145-
body, err := createContainer(context.Background(), fakeCLI, config, &createOptions{
145+
id, err := createContainer(context.Background(), fakeCLI, config, &createOptions{
146146
name: "name",
147147
platform: runtime.GOOS,
148148
untrusted: true,
@@ -153,7 +153,7 @@ func TestCreateContainerImagePullPolicy(t *testing.T) {
153153
assert.Check(t, is.ErrorContains(err, tc.ExpectedErrMsg))
154154
} else {
155155
assert.Check(t, err)
156-
assert.Check(t, is.DeepEqual(tc.ExpectedBody, *body))
156+
assert.Check(t, is.Equal(tc.ExpectedID, id))
157157
}
158158

159159
assert.Check(t, is.Equal(tc.ExpectedPulls, pullCounter))

cli/command/container/run.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,14 @@ func runContainer(dockerCli command.Cli, opts *runOptions, copts *containerOptio
144144
ctx, cancelFun := context.WithCancel(context.Background())
145145
defer cancelFun()
146146

147-
createResponse, err := createContainer(ctx, dockerCli, containerCfg, &opts.createOptions)
147+
containerID, err := createContainer(ctx, dockerCli, containerCfg, &opts.createOptions)
148148
if err != nil {
149149
reportError(stderr, "run", err.Error(), true)
150150
return runStartContainerErr(err)
151151
}
152152
if opts.sigProxy {
153153
sigc := notifyAllSignals()
154-
go ForwardAllSignals(ctx, dockerCli, createResponse.ID, sigc)
154+
go ForwardAllSignals(ctx, dockerCli, containerID, sigc)
155155
defer signal.StopCatch(sigc)
156156
}
157157

@@ -164,7 +164,7 @@ func runContainer(dockerCli command.Cli, opts *runOptions, copts *containerOptio
164164
waitDisplayID = make(chan struct{})
165165
go func() {
166166
defer close(waitDisplayID)
167-
fmt.Fprintln(stdout, createResponse.ID)
167+
_, _ = fmt.Fprintln(stdout, containerID)
168168
}()
169169
}
170170
attach := config.AttachStdin || config.AttachStdout || config.AttachStderr
@@ -173,17 +173,17 @@ func runContainer(dockerCli command.Cli, opts *runOptions, copts *containerOptio
173173
dockerCli.ConfigFile().DetachKeys = opts.detachKeys
174174
}
175175

176-
closeFn, err := attachContainer(ctx, dockerCli, &errCh, config, createResponse.ID)
176+
closeFn, err := attachContainer(ctx, dockerCli, &errCh, config, containerID)
177177
if err != nil {
178178
return err
179179
}
180180
defer closeFn()
181181
}
182182

183-
statusChan := waitExitOrRemoved(ctx, dockerCli, createResponse.ID, copts.autoRemove)
183+
statusChan := waitExitOrRemoved(ctx, dockerCli, containerID, copts.autoRemove)
184184

185185
// start the container
186-
if err := client.ContainerStart(ctx, createResponse.ID, types.ContainerStartOptions{}); err != nil {
186+
if err := client.ContainerStart(ctx, containerID, types.ContainerStartOptions{}); err != nil {
187187
// If we have hijackedIOStreamer, we should notify
188188
// hijackedIOStreamer we are going to exit and wait
189189
// to avoid the terminal are not restored.
@@ -201,8 +201,8 @@ func runContainer(dockerCli command.Cli, opts *runOptions, copts *containerOptio
201201
}
202202

203203
if (config.AttachStdin || config.AttachStdout || config.AttachStderr) && config.Tty && dockerCli.Out().IsTerminal() {
204-
if err := MonitorTtySize(ctx, dockerCli, createResponse.ID, false); err != nil {
205-
fmt.Fprintln(stderr, "Error monitoring TTY size:", err)
204+
if err := MonitorTtySize(ctx, dockerCli, containerID, false); err != nil {
205+
_, _ = fmt.Fprintln(stderr, "Error monitoring TTY size:", err)
206206
}
207207
}
208208

0 commit comments

Comments
 (0)