2018-02-05 21:05:59 +00:00
|
|
|
package client // import "github.com/docker/docker/client"
|
2016-09-06 18:46:37 +00:00
|
|
|
|
|
|
|
import (
|
2018-04-19 22:30:59 +00:00
|
|
|
"context"
|
2016-09-06 18:46:37 +00:00
|
|
|
"encoding/json"
|
|
|
|
"net/url"
|
2021-07-12 10:27:30 +00:00
|
|
|
"path"
|
2016-09-06 18:46:37 +00:00
|
|
|
|
|
|
|
"github.com/docker/docker/api/types/container"
|
|
|
|
"github.com/docker/docker/api/types/network"
|
Don't use AutoRemove on older daemons
Docker 1.13 moves the `--rm` flag to the daemon,
through an AutoRemove option in HostConfig.
When using API 1.24 and under, AutoRemove should not be
used, even if the daemon is version 1.13 or above and
"supports" this feature.
This patch fixes a situation where an 1.13 client,
talking to an 1.13 daemon, but using the 1.24 API
version, still set the AutoRemove property.
As a result, both the client _and_ the daemon
were attempting to remove the container, resulting
in an error:
ERRO[0000] error removing container: Error response from daemon:
removal of container ce0976ad22495c7cbe9487752ea32721a282164862db036b2f3377bd07461c3a
is already in progress
In addition, the validation of conflicting options
is moved from `docker run` to `opts.parse()`, so
that conflicting options are also detected when
running `docker create` and `docker start` separately.
To resolve the issue, the `AutoRemove` option is now
always set to `false` both by the client and the
daemon, if API version 1.24 or under is used.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2017-01-13 00:05:39 +00:00
|
|
|
"github.com/docker/docker/api/types/versions"
|
2023-05-08 09:57:52 +00:00
|
|
|
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
2016-09-06 18:46:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type configWrapper struct {
|
|
|
|
*container.Config
|
|
|
|
HostConfig *container.HostConfig
|
|
|
|
NetworkingConfig *network.NetworkingConfig
|
|
|
|
}
|
|
|
|
|
2021-02-16 15:07:44 +00:00
|
|
|
// ContainerCreate creates a new container based on the given configuration.
|
2016-09-06 18:46:37 +00:00
|
|
|
// It can be associated with a name, but it's not mandatory.
|
2023-05-08 09:57:52 +00:00
|
|
|
func (cli *Client) ContainerCreate(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, platform *ocispec.Platform, containerName string) (container.CreateResponse, error) {
|
2022-03-05 22:58:46 +00:00
|
|
|
var response container.CreateResponse
|
2016-11-03 00:43:32 +00:00
|
|
|
|
2023-09-12 12:08:54 +00:00
|
|
|
// Make sure we negotiated (if the client is configured to do so),
|
|
|
|
// as code below contains API-version specific handling of options.
|
|
|
|
//
|
|
|
|
// Normally, version-negotiation (if enabled) would not happen until
|
|
|
|
// the API request is made.
|
2024-02-23 11:20:06 +00:00
|
|
|
if err := cli.checkVersion(ctx); err != nil {
|
|
|
|
return response, err
|
|
|
|
}
|
2023-09-12 12:08:54 +00:00
|
|
|
|
|
|
|
if err := cli.NewVersionError(ctx, "1.25", "stop timeout"); config != nil && config.StopTimeout != nil && err != nil {
|
2016-11-03 00:43:32 +00:00
|
|
|
return response, err
|
|
|
|
}
|
2023-09-12 12:08:54 +00:00
|
|
|
if err := cli.NewVersionError(ctx, "1.41", "specify container image platform"); platform != nil && err != nil {
|
2020-03-19 20:54:48 +00:00
|
|
|
return response, err
|
|
|
|
}
|
2023-09-12 12:08:54 +00:00
|
|
|
if err := cli.NewVersionError(ctx, "1.44", "specify health-check start interval"); config != nil && config.Healthcheck != nil && config.Healthcheck.StartInterval != 0 && err != nil {
|
2020-05-02 21:06:18 +00:00
|
|
|
return response, err
|
|
|
|
}
|
2023-07-04 10:40:35 +00:00
|
|
|
if err := cli.NewVersionError(ctx, "1.44", "specify mac-address per network"); hasEndpointSpecificMacAddress(networkingConfig) && err != nil {
|
2023-07-04 21:48:58 +00:00
|
|
|
return response, err
|
|
|
|
}
|
2020-03-19 20:54:48 +00:00
|
|
|
|
2022-06-05 23:27:06 +00:00
|
|
|
if hostConfig != nil {
|
|
|
|
if versions.LessThan(cli.ClientVersion(), "1.25") {
|
|
|
|
// When using API 1.24 and under, the client is responsible for removing the container
|
|
|
|
hostConfig.AutoRemove = false
|
|
|
|
}
|
|
|
|
if versions.GreaterThanOrEqualTo(cli.ClientVersion(), "1.42") || versions.LessThan(cli.ClientVersion(), "1.40") {
|
|
|
|
// KernelMemory was added in API 1.40, and deprecated in API 1.42
|
|
|
|
hostConfig.KernelMemory = 0
|
|
|
|
}
|
|
|
|
if platform != nil && platform.OS == "linux" && versions.LessThan(cli.ClientVersion(), "1.42") {
|
|
|
|
// When using API under 1.42, the Linux daemon doesn't respect the ConsoleSize
|
|
|
|
hostConfig.ConsoleSize = [2]uint{0, 0}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-04 10:40:35 +00:00
|
|
|
// Since API 1.44, the container-wide MacAddress is deprecated and will trigger a WARNING if it's specified.
|
|
|
|
if versions.GreaterThanOrEqualTo(cli.ClientVersion(), "1.44") {
|
|
|
|
config.MacAddress = "" //nolint:staticcheck // ignore SA1019: field is deprecated, but still used on API < v1.44.
|
|
|
|
}
|
|
|
|
|
2016-09-06 18:46:37 +00:00
|
|
|
query := url.Values{}
|
2021-07-12 10:27:30 +00:00
|
|
|
if p := formatPlatform(platform); p != "" {
|
|
|
|
query.Set("platform", p)
|
2020-03-19 20:54:48 +00:00
|
|
|
}
|
|
|
|
|
2016-09-06 18:46:37 +00:00
|
|
|
if containerName != "" {
|
|
|
|
query.Set("name", containerName)
|
|
|
|
}
|
|
|
|
|
|
|
|
body := configWrapper{
|
|
|
|
Config: config,
|
|
|
|
HostConfig: hostConfig,
|
|
|
|
NetworkingConfig: networkingConfig,
|
|
|
|
}
|
|
|
|
|
|
|
|
serverResp, err := cli.post(ctx, "/containers/create", query, body, nil)
|
2019-02-11 12:26:12 +00:00
|
|
|
defer ensureReaderClosed(serverResp)
|
2016-09-06 18:46:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return response, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewDecoder(serverResp.body).Decode(&response)
|
|
|
|
return response, err
|
|
|
|
}
|
2021-07-12 10:27:30 +00:00
|
|
|
|
|
|
|
// formatPlatform returns a formatted string representing platform (e.g. linux/arm/v7).
|
|
|
|
//
|
|
|
|
// Similar to containerd's platforms.Format(), but does allow components to be
|
|
|
|
// omitted (e.g. pass "architecture" only, without "os":
|
|
|
|
// https://github.com/containerd/containerd/blob/v1.5.2/platforms/platforms.go#L243-L263
|
2023-05-08 09:57:52 +00:00
|
|
|
func formatPlatform(platform *ocispec.Platform) string {
|
2021-07-12 10:27:30 +00:00
|
|
|
if platform == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return path.Join(platform.OS, platform.Architecture, platform.Variant)
|
|
|
|
}
|
2023-07-04 21:48:58 +00:00
|
|
|
|
|
|
|
// hasEndpointSpecificMacAddress checks whether one of the endpoint in networkingConfig has a MacAddress defined.
|
|
|
|
func hasEndpointSpecificMacAddress(networkingConfig *network.NetworkingConfig) bool {
|
|
|
|
if networkingConfig == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, endpoint := range networkingConfig.EndpointsConfig {
|
|
|
|
if endpoint.MacAddress != "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|