Merge pull request #51001 from thaJeztah/fix_create_panic

client: Client.ContainerCreate: fix panic when passing a nil config
This commit is contained in:
Sebastiaan van Stijn
2025-09-18 11:34:57 +02:00
committed by GitHub
3 changed files with 23 additions and 9 deletions

View File

@@ -9,6 +9,7 @@ import (
"sort"
"strings"
cerrdefs "github.com/containerd/errdefs"
"github.com/moby/moby/api/types/container"
"github.com/moby/moby/api/types/network"
"github.com/moby/moby/api/types/versions"
@@ -18,6 +19,10 @@ import (
// ContainerCreate creates a new container based on the given configuration.
// It can be associated with a name, but it's not mandatory.
func (cli *Client) ContainerCreate(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, platform *ocispec.Platform, containerName string) (container.CreateResponse, error) {
if config == nil {
return container.CreateResponse{}, cerrdefs.ErrInvalidArgument.WithMessage("config is nil")
}
var response container.CreateResponse
// Make sure we negotiated (if the client is configured to do so),
@@ -29,13 +34,13 @@ func (cli *Client) ContainerCreate(ctx context.Context, config *container.Config
return response, err
}
if err := cli.NewVersionError(ctx, "1.25", "stop timeout"); config != nil && config.StopTimeout != nil && err != nil {
if err := cli.NewVersionError(ctx, "1.25", "stop timeout"); config.StopTimeout != nil && err != nil {
return response, err
}
if err := cli.NewVersionError(ctx, "1.41", "specify container image platform"); platform != nil && err != nil {
return response, err
}
if err := cli.NewVersionError(ctx, "1.44", "specify health-check start interval"); config != nil && config.Healthcheck != nil && config.Healthcheck.StartInterval != 0 && err != nil {
if err := cli.NewVersionError(ctx, "1.44", "specify health-check start interval"); config.Healthcheck != nil && config.Healthcheck.StartInterval != 0 && err != nil {
return response, err
}
if err := cli.NewVersionError(ctx, "1.44", "specify mac-address per network"); hasEndpointSpecificMacAddress(networkingConfig) && err != nil {

View File

@@ -23,6 +23,10 @@ func TestContainerCreateError(t *testing.T) {
assert.NilError(t, err)
_, err = client.ContainerCreate(context.Background(), nil, nil, nil, nil, "nothing")
assert.Error(t, err, "config is nil")
assert.Check(t, is.ErrorType(err, cerrdefs.IsInvalidArgument))
_, err = client.ContainerCreate(context.Background(), &container.Config{}, nil, nil, nil, "nothing")
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
// 404 doesn't automatically means an unknown image
@@ -31,7 +35,7 @@ func TestContainerCreateError(t *testing.T) {
)
assert.NilError(t, err)
_, err = client.ContainerCreate(context.Background(), nil, nil, nil, nil, "nothing")
_, err = client.ContainerCreate(context.Background(), &container.Config{}, nil, nil, nil, "nothing")
assert.Check(t, is.ErrorType(err, cerrdefs.IsNotFound))
}
@@ -70,7 +74,7 @@ func TestContainerCreateWithName(t *testing.T) {
)
assert.NilError(t, err)
r, err := client.ContainerCreate(context.Background(), nil, nil, nil, nil, "container_name")
r, err := client.ContainerCreate(context.Background(), &container.Config{}, nil, nil, nil, "container_name")
assert.NilError(t, err)
assert.Check(t, is.Equal(r.ID, "container_id"))
}
@@ -115,7 +119,7 @@ func TestContainerCreateAutoRemove(t *testing.T) {
)
assert.NilError(t, err)
_, err = client.ContainerCreate(context.Background(), nil, &container.HostConfig{AutoRemove: true}, nil, nil, "")
_, err = client.ContainerCreate(context.Background(), &container.Config{}, &container.HostConfig{AutoRemove: true}, nil, nil, "")
assert.NilError(t, err)
})
}
@@ -129,7 +133,7 @@ func TestContainerCreateConnectionError(t *testing.T) {
client, err := NewClientWithOpts(WithAPIVersionNegotiation(), WithHost("tcp://no-such-host.invalid"))
assert.NilError(t, err)
_, err = client.ContainerCreate(context.Background(), nil, nil, nil, nil, "")
_, err = client.ContainerCreate(context.Background(), &container.Config{}, nil, nil, nil, "")
assert.Check(t, is.ErrorType(err, IsErrConnectionFailed))
}
@@ -179,6 +183,6 @@ func TestContainerCreateCapabilities(t *testing.T) {
)
assert.NilError(t, err)
_, err = client.ContainerCreate(context.Background(), nil, &container.HostConfig{CapAdd: inputCaps, CapDrop: inputCaps}, nil, nil, "")
_, err = client.ContainerCreate(context.Background(), &container.Config{}, &container.HostConfig{CapAdd: inputCaps, CapDrop: inputCaps}, nil, nil, "")
assert.NilError(t, err)
}

View File

@@ -9,6 +9,7 @@ import (
"sort"
"strings"
cerrdefs "github.com/containerd/errdefs"
"github.com/moby/moby/api/types/container"
"github.com/moby/moby/api/types/network"
"github.com/moby/moby/api/types/versions"
@@ -18,6 +19,10 @@ import (
// ContainerCreate creates a new container based on the given configuration.
// It can be associated with a name, but it's not mandatory.
func (cli *Client) ContainerCreate(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, platform *ocispec.Platform, containerName string) (container.CreateResponse, error) {
if config == nil {
return container.CreateResponse{}, cerrdefs.ErrInvalidArgument.WithMessage("config is nil")
}
var response container.CreateResponse
// Make sure we negotiated (if the client is configured to do so),
@@ -29,13 +34,13 @@ func (cli *Client) ContainerCreate(ctx context.Context, config *container.Config
return response, err
}
if err := cli.NewVersionError(ctx, "1.25", "stop timeout"); config != nil && config.StopTimeout != nil && err != nil {
if err := cli.NewVersionError(ctx, "1.25", "stop timeout"); config.StopTimeout != nil && err != nil {
return response, err
}
if err := cli.NewVersionError(ctx, "1.41", "specify container image platform"); platform != nil && err != nil {
return response, err
}
if err := cli.NewVersionError(ctx, "1.44", "specify health-check start interval"); config != nil && config.Healthcheck != nil && config.Healthcheck.StartInterval != 0 && err != nil {
if err := cli.NewVersionError(ctx, "1.44", "specify health-check start interval"); config.Healthcheck != nil && config.Healthcheck.StartInterval != 0 && err != nil {
return response, err
}
if err := cli.NewVersionError(ctx, "1.44", "specify mac-address per network"); hasEndpointSpecificMacAddress(networkingConfig) && err != nil {