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>
This commit is contained in:
Albin Kerouanton 2023-10-25 14:43:59 +02:00
parent a73dfe68d3
commit 91eee33f62
No known key found for this signature in database
GPG key ID: 630B8E1DCBDB1864
4 changed files with 10 additions and 4 deletions

View file

@ -24,6 +24,7 @@ import (
containerpkg "github.com/docker/docker/container"
"github.com/docker/docker/errdefs"
"github.com/docker/docker/pkg/ioutils"
"github.com/docker/docker/runconfig"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
"golang.org/x/net/websocket"
@ -494,6 +495,9 @@ func (s *containerRouter) postContainersCreate(ctx context.Context, w http.Respo
return err
}
if config == nil {
return errdefs.InvalidParameter(runconfig.ErrEmptyConfig)
}
if hostConfig == nil {
hostConfig = &container.HostConfig{}
}

View file

@ -2,7 +2,6 @@ package daemon // import "github.com/docker/docker/daemon"
import (
"context"
"errors"
"fmt"
"runtime"
"strings"
@ -61,7 +60,7 @@ func (daemon *Daemon) ContainerCreateIgnoreImagesArgsEscaped(ctx context.Context
func (daemon *Daemon) containerCreate(ctx context.Context, daemonCfg *configStore, opts createOpts) (containertypes.CreateResponse, error) {
start := time.Now()
if opts.params.Config == nil {
return containertypes.CreateResponse{}, errdefs.InvalidParameter(errors.New("Config cannot be empty in order to create a container"))
return containertypes.CreateResponse{}, errdefs.InvalidParameter(runconfig.ErrEmptyConfig)
}
// Normalize some defaults. Doing this "ad-hoc" here for now, as there's

View file

@ -12,6 +12,7 @@ import (
"github.com/docker/docker/api"
"github.com/docker/docker/api/types/versions"
"github.com/docker/docker/runconfig"
"github.com/docker/docker/testutil"
"github.com/docker/docker/testutil/request"
"gotest.tools/v3/assert"
@ -82,7 +83,7 @@ func (s *DockerAPISuite) TestAPIErrorJSON(c *testing.T) {
assert.Assert(c, strings.Contains(httpResp.Header.Get("Content-Type"), "application/json"))
b, err := request.ReadBody(body)
assert.NilError(c, err)
assert.Equal(c, getErrorMessage(c, b), "Config cannot be empty in order to create a container")
assert.Equal(c, getErrorMessage(c, b), runconfig.ErrEmptyConfig.Error())
}
func (s *DockerAPISuite) TestAPIErrorPlainText(c *testing.T) {
@ -99,7 +100,7 @@ func (s *DockerAPISuite) TestAPIErrorPlainText(c *testing.T) {
assert.Assert(c, strings.Contains(httpResp.Header.Get("Content-Type"), "text/plain"))
b, err := request.ReadBody(body)
assert.NilError(c, err)
assert.Equal(c, strings.TrimSpace(string(b)), "Config cannot be empty in order to create a container")
assert.Equal(c, strings.TrimSpace(string(b)), runconfig.ErrEmptyConfig.Error())
}
func (s *DockerAPISuite) TestAPIErrorNotFoundJSON(c *testing.T) {

View file

@ -31,6 +31,8 @@ const (
ErrUnsupportedNetworkAndAlias validationError = "network-scoped alias is supported only for containers in user defined networks"
// ErrConflictUTSHostname conflict between the hostname and the UTS mode
ErrConflictUTSHostname validationError = "conflicting options: hostname and the UTS mode"
// ErrEmptyConfig when container config is nil
ErrEmptyConfig validationError = "Config cannot be empty in order to create a container"
)
type validationError string