Browse code

api: ContainerCreate: return an error when config is nil

The same error is already returned by `(*Daemon).containerCreate()` but
since this function is also called by the cluster executor, the error
has to be duplicated.

Doing that allows to remove a nil check on container config in
`postContainersCreate`.

Signed-off-by: Albin Kerouanton <albinker@gmail.com>

Albin Kerouanton authored on 2023/10/25 21:43:59
Showing 4 changed files
... ...
@@ -24,6 +24,7 @@ import (
24 24
 	containerpkg "github.com/docker/docker/container"
25 25
 	"github.com/docker/docker/errdefs"
26 26
 	"github.com/docker/docker/pkg/ioutils"
27
+	"github.com/docker/docker/runconfig"
27 28
 	ocispec "github.com/opencontainers/image-spec/specs-go/v1"
28 29
 	"github.com/pkg/errors"
29 30
 	"golang.org/x/net/websocket"
... ...
@@ -494,6 +495,9 @@ func (s *containerRouter) postContainersCreate(ctx context.Context, w http.Respo
494 494
 		return err
495 495
 	}
496 496
 
497
+	if config == nil {
498
+		return errdefs.InvalidParameter(runconfig.ErrEmptyConfig)
499
+	}
497 500
 	if hostConfig == nil {
498 501
 		hostConfig = &container.HostConfig{}
499 502
 	}
... ...
@@ -2,7 +2,6 @@ package daemon // import "github.com/docker/docker/daemon"
2 2
 
3 3
 import (
4 4
 	"context"
5
-	"errors"
6 5
 	"fmt"
7 6
 	"runtime"
8 7
 	"strings"
... ...
@@ -61,7 +60,7 @@ func (daemon *Daemon) ContainerCreateIgnoreImagesArgsEscaped(ctx context.Context
61 61
 func (daemon *Daemon) containerCreate(ctx context.Context, daemonCfg *configStore, opts createOpts) (containertypes.CreateResponse, error) {
62 62
 	start := time.Now()
63 63
 	if opts.params.Config == nil {
64
-		return containertypes.CreateResponse{}, errdefs.InvalidParameter(errors.New("Config cannot be empty in order to create a container"))
64
+		return containertypes.CreateResponse{}, errdefs.InvalidParameter(runconfig.ErrEmptyConfig)
65 65
 	}
66 66
 
67 67
 	// Normalize some defaults. Doing this "ad-hoc" here for now, as there's
... ...
@@ -12,6 +12,7 @@ import (
12 12
 
13 13
 	"github.com/docker/docker/api"
14 14
 	"github.com/docker/docker/api/types/versions"
15
+	"github.com/docker/docker/runconfig"
15 16
 	"github.com/docker/docker/testutil"
16 17
 	"github.com/docker/docker/testutil/request"
17 18
 	"gotest.tools/v3/assert"
... ...
@@ -82,7 +83,7 @@ func (s *DockerAPISuite) TestAPIErrorJSON(c *testing.T) {
82 82
 	assert.Assert(c, strings.Contains(httpResp.Header.Get("Content-Type"), "application/json"))
83 83
 	b, err := request.ReadBody(body)
84 84
 	assert.NilError(c, err)
85
-	assert.Equal(c, getErrorMessage(c, b), "Config cannot be empty in order to create a container")
85
+	assert.Equal(c, getErrorMessage(c, b), runconfig.ErrEmptyConfig.Error())
86 86
 }
87 87
 
88 88
 func (s *DockerAPISuite) TestAPIErrorPlainText(c *testing.T) {
... ...
@@ -99,7 +100,7 @@ func (s *DockerAPISuite) TestAPIErrorPlainText(c *testing.T) {
99 99
 	assert.Assert(c, strings.Contains(httpResp.Header.Get("Content-Type"), "text/plain"))
100 100
 	b, err := request.ReadBody(body)
101 101
 	assert.NilError(c, err)
102
-	assert.Equal(c, strings.TrimSpace(string(b)), "Config cannot be empty in order to create a container")
102
+	assert.Equal(c, strings.TrimSpace(string(b)), runconfig.ErrEmptyConfig.Error())
103 103
 }
104 104
 
105 105
 func (s *DockerAPISuite) TestAPIErrorNotFoundJSON(c *testing.T) {
... ...
@@ -31,6 +31,8 @@ const (
31 31
 	ErrUnsupportedNetworkAndAlias validationError = "network-scoped alias is supported only for containers in user defined networks"
32 32
 	// ErrConflictUTSHostname conflict between the hostname and the UTS mode
33 33
 	ErrConflictUTSHostname validationError = "conflicting options: hostname and the UTS mode"
34
+	// ErrEmptyConfig when container config is nil
35
+	ErrEmptyConfig validationError = "Config cannot be empty in order to create a container"
34 36
 )
35 37
 
36 38
 type validationError string