Skip to content

Commit 9f774c3

Browse files
authored
Merge pull request #6759 from thaJeztah/container_create_cleanups
cli/command/container: create: assorted cleanups and linting fixes
2 parents d083829 + ed566e7 commit 9f774c3

File tree

1 file changed

+22
-30
lines changed

1 file changed

+22
-30
lines changed

cli/command/container/create.go

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ func runCreate(ctx context.Context, dockerCLI command.Cli, flags *pflag.FlagSet,
132132
return nil
133133
}
134134

135-
// FIXME(thaJeztah): this is the only code-path that uses APIClient.ImageCreate. Rewrite this to use the regular "pull" code (or vice-versa).
136135
func pullImage(ctx context.Context, dockerCLI command.Cli, img string, options *createOptions) error {
137136
encodedAuth, err := command.RetrieveAuthTokenFromImage(dockerCLI.ConfigFile(), img)
138137
if err != nil {
@@ -212,7 +211,7 @@ func newCIDFile(cidPath string) (*cidFile, error) {
212211
}
213212

214213
//nolint:gocyclo
215-
func createContainer(ctx context.Context, dockerCli command.Cli, containerCfg *containerConfig, options *createOptions) (containerID string, err error) {
214+
func createContainer(ctx context.Context, dockerCLI command.Cli, containerCfg *containerConfig, options *createOptions) (containerID string, _ error) {
216215
config := containerCfg.Config
217216
hostConfig := containerCfg.HostConfig
218217
networkingConfig := containerCfg.NetworkingConfig
@@ -221,8 +220,7 @@ func createContainer(ctx context.Context, dockerCli command.Cli, containerCfg *c
221220

222221
// TODO(thaJeztah): add a platform option-type / flag-type.
223222
if options.platform != "" {
224-
_, err = platforms.Parse(options.platform)
225-
if err != nil {
223+
if _, err := platforms.Parse(options.platform); err != nil {
226224
return "", err
227225
}
228226
}
@@ -248,10 +246,11 @@ func createContainer(ctx context.Context, dockerCli command.Cli, containerCfg *c
248246

249247
if options.useAPISocket {
250248
// We'll create two new mounts to handle this flag:
249+
//
251250
// 1. Mount the actual docker socket.
252-
// 2. A synthezised ~/.docker/config.json with resolved tokens.
251+
// 2. A synthesized ~/.docker/config.json with resolved tokens.
253252

254-
if dockerCli.ServerInfo().OSType == "windows" {
253+
if dockerCLI.ServerInfo().OSType == "windows" {
255254
return "", errors.New("flag --use-api-socket can't be used with a Windows Docker Engine")
256255
}
257256

@@ -286,18 +285,18 @@ func createContainer(ctx context.Context, dockerCli command.Cli, containerCfg *c
286285
})
287286
*/
288287

289-
var envvarPresent bool
290-
for _, envvar := range containerCfg.Config.Env {
291-
if strings.HasPrefix(envvar, "DOCKER_CONFIG=") {
292-
envvarPresent = true
288+
var envVarPresent bool
289+
for _, envVar := range containerCfg.Config.Env {
290+
if strings.HasPrefix(envVar, "DOCKER_CONFIG=") {
291+
envVarPresent = true
293292
}
294293
}
295294

296295
// If the DOCKER_CONFIG env var is already present, we assume the client knows
297296
// what they're doing and don't inject the creds.
298-
if !envvarPresent {
297+
if !envVarPresent {
299298
// Resolve this here for later, ensuring we error our before we create the container.
300-
creds, err := readCredentials(dockerCli)
299+
creds, err := readCredentials(dockerCLI)
301300
if err != nil {
302301
return "", fmt.Errorf("resolving credentials failed: %w", err)
303302
}
@@ -319,22 +318,15 @@ func createContainer(ctx context.Context, dockerCli command.Cli, containerCfg *c
319318
platform = &p
320319
}
321320

322-
pullAndTagImage := func() error {
323-
if err := pullImage(ctx, dockerCli, config.Image, options); err != nil {
324-
return err
325-
}
326-
return nil
327-
}
328-
329321
if options.pull == PullImageAlways {
330-
if err := pullAndTagImage(); err != nil {
322+
if err := pullImage(ctx, dockerCLI, config.Image, options); err != nil {
331323
return "", err
332324
}
333325
}
334326

335-
hostConfig.ConsoleSize[0], hostConfig.ConsoleSize[1] = dockerCli.Out().GetTtySize()
327+
hostConfig.ConsoleSize[0], hostConfig.ConsoleSize[1] = dockerCLI.Out().GetTtySize()
336328

337-
response, err := dockerCli.Client().ContainerCreate(ctx, client.ContainerCreateOptions{
329+
response, err := dockerCLI.Client().ContainerCreate(ctx, client.ContainerCreateOptions{
338330
Name: options.name,
339331
// Image: config.Image, // TODO(thaJeztah): pass image-ref separate
340332
Platform: platform,
@@ -347,15 +339,15 @@ func createContainer(ctx context.Context, dockerCli command.Cli, containerCfg *c
347339
if errdefs.IsNotFound(err) && namedRef != nil && options.pull == PullImageMissing {
348340
if !options.quiet {
349341
// we don't want to write to stdout anything apart from container.ID
350-
_, _ = fmt.Fprintf(dockerCli.Err(), "Unable to find image '%s' locally\n", reference.FamiliarString(namedRef))
342+
_, _ = fmt.Fprintf(dockerCLI.Err(), "Unable to find image '%s' locally\n", reference.FamiliarString(namedRef))
351343
}
352344

353-
if err := pullAndTagImage(); err != nil {
345+
if err := pullImage(ctx, dockerCLI, config.Image, options); err != nil {
354346
return "", err
355347
}
356348

357349
var retryErr error
358-
response, retryErr = dockerCli.Client().ContainerCreate(ctx, client.ContainerCreateOptions{
350+
response, retryErr = dockerCLI.Client().ContainerCreate(ctx, client.ContainerCreateOptions{
359351
Name: options.name,
360352
// Image: config.Image, // TODO(thaJeztah): pass image-ref separate
361353
Platform: platform,
@@ -371,24 +363,23 @@ func createContainer(ctx context.Context, dockerCli command.Cli, containerCfg *c
371363
}
372364
}
373365

374-
containerID = response.ID
375366
for _, w := range response.Warnings {
376-
_, _ = fmt.Fprintln(dockerCli.Err(), "WARNING:", w)
367+
_, _ = fmt.Fprintln(dockerCLI.Err(), "WARNING:", w)
377368
}
378-
err = containerIDFile.Write(containerID)
369+
err = containerIDFile.Write(response.ID)
379370

380371
if options.useAPISocket && len(apiSocketCreds) > 0 {
381372
// Create a new config file with just the auth.
382373
newConfig := &configfile.ConfigFile{
383374
AuthConfigs: apiSocketCreds,
384375
}
385376

386-
if err := copyDockerConfigIntoContainer(ctx, dockerCli.Client(), containerID, dockerConfigPathInContainer, newConfig); err != nil {
377+
if err := copyDockerConfigIntoContainer(ctx, dockerCLI.Client(), response.ID, dockerConfigPathInContainer, newConfig); err != nil {
387378
return "", fmt.Errorf("injecting docker config.json into container failed: %w", err)
388379
}
389380
}
390381

391-
return containerID, err
382+
return response.ID, err
392383
}
393384

394385
func validatePullOpt(val string) error {
@@ -428,6 +419,7 @@ func copyDockerConfigIntoContainer(ctx context.Context, apiClient client.APIClie
428419
})
429420

430421
if _, err := io.Copy(tarWriter, &configBuf); err != nil {
422+
_ = tarWriter.Close()
431423
return fmt.Errorf("writing config to tar file for config copy: %w", err)
432424
}
433425

0 commit comments

Comments
 (0)