36 Commits

Author SHA1 Message Date
Sebastiaan van Stijn
189942570a client: enable API-version negotiation by default
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-11-27 13:52:27 +01:00
Sebastiaan van Stijn
c950796596 client: use t.Context in tests
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-11-10 23:23:23 +01:00
Sebastiaan van Stijn
d1f70d4f54 client: deprecate NewClientWithOpts in favor of New
Use a more idiomatic name so that it can be used as `client.New()`.

We should look if we want `New()` to have different / updated defaults
i.e., enable `WithEnv` as default, and have an opt-out and have API-
version negotiation enabled by default (with an opt-out option).

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-10-30 18:09:44 +01:00
Sebastiaan van Stijn
f9082484c9 Merge pull request #51278 from vvoland/client-container-opts2
client_(attach,commit,create,diff): Wrap result and options
2025-10-24 13:41:23 +02:00
Paweł Gronowski
8fb561ca9a client/container_create: Add Image outside of Config
Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
2025-10-23 21:11:30 +02:00
Paweł Gronowski
3340c86db9 client/container_create: Rename ContainerName to Name
Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
2025-10-23 21:11:30 +02:00
Paweł Gronowski
bd31b8b1c7 client_(attach,commit,create,diff): Wrap result and options
Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
2025-10-23 21:11:30 +02:00
Sebastiaan van Stijn
47fd987af2 client: simplify test with mock-responses
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-10-23 17:43:10 +02:00
Sebastiaan van Stijn
9a32a7e0d1 daemon, client: remove version-gate for daemon-side AutoRemove
Support for daemon-side auto-remove was added in API v1.25; on older
versions of the daemon, the client was responsible for removing the
container after it exited (see [moby@6dd8e10])

On API versions < 1.30, it used the events API for this purpose, and
would wait for a "die", "detach" or "detroy" events to know the container
exited, and could be removed or (when attached, but without a TTY) to
get the container's exit-status. (see [cli@38591f2]).

API version 1.24 (docker 1.12) is 9 Years old (July 29, 2016), and API
1.30 (docker 17.06) is 8 Years old (Jun 20, 2017), and long EOL. While
technically, a CLI could negotiate API 1.30 or older, this would only
be in cases where either API version negotiation failed, or the version
was explicitly overridden through `DOCKER_API_VERSION` for testing.

This patch removes the version-gate for daemon-side AutoRemove; version-
specific handling is removed from the client (and a related patch in
the CLI).

[moby@6dd8e10]: 6dd8e10d6e
[cli@38591f2]: 38591f20d0

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-09-30 18:31:22 +02:00
Sebastiaan van Stijn
839c2709af client: WithMockClient: match version behavior of actual client
The WithMockClient option was explicitly resetting the client's API
version (see [1]), which differs from the regular client, which is
initialized with the current API version used by the client (see [2]).

This patch:

- reduces the `WithMockClient` to only set the custom HTTP client, leaving
  other fields un-touched.
- adds a test utility and updates tests to handle the API-version prefix
- removes redundant uses of `WithVersion()` in tests; for most test-cases
  it was used to make sure a current API version is used that supports the
  feature being tested, but there was no test to verify the behavior for
  lower API versions, so we may as well test against "latest".

[1]: 5a582729d8/client/client_mock_test.go (L22-L36)
[2]: 5a582729d8/client/client.go (L167-L190)

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-09-18 11:37:56 +02:00
Sebastiaan van Stijn
0468dac252 client: Client.ContainerCreate: fix panic when passing a nil config
The config is a required argument (to create a container, at least
an image is needed), but the function was missing a check for this,
which would result in a panic if the client was using API v1.44 or
up due to the changes from ee9f0ed895
attempting to [reset the deprecated `MacAddress` field][1].

In practice, this would unlikely be hit, and we didn't hit this in
unit-tests, due to a bug in `WithMockClient`, which initializes the
client with an [empty API version][2], which is different from the
actual client, which [initializes the client with the MaxAPIVersion][3]

This patch updates the function to return an error if a nil config is
passed.

[1]: 5a582729d8/client/container_create.go (L72-L75)
[2]: 5a582729d8/client/client_mock_test.go (L22-L36)
[3]: 5a582729d8/client/client.go (L167-L190)

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-09-17 22:51:24 +02:00
Paweł Gronowski
bc1d436aa9 client/container_test: Use functional option to create mock client
Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
2025-08-29 15:17:22 +02:00
Sebastiaan van Stijn
98790830eb Deprecate api/types/strslice.StrSlice and remove its use
The strslice.StrSlice type is a string-slice with a custom JSON Unmarshal
function to provide backward-compatibility with older API requests from
before docker 1.7 (see [moby@17d6f00] and [moby@ea4a067]), which used a
string instead of an array of strings for some fields (Cmd, Entrypoint).

We no longer support those API versions, and we no longer support pulling
v1 images that may contain such a config, so we can deprecate the type
and remove its use.

[moby@17d6f00]: 17d6f00ec2
[moby@ea4a067]: ea4a06740b

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-08-07 17:15:49 -05:00
Derek McGowan
afd6487b2e Create github.com/moby/moby/api module
Signed-off-by: Derek McGowan <derek@mcg.dev>
2025-07-21 09:30:05 -07:00
Sebastiaan van Stijn
4856e8ffad client: remove // import comments
These comments were added to enforce using the correct import path for
our packages ("github.com/docker/docker", not "github.com/moby/moby").
However, when working in go module mode (not GOPATH / vendor), they have
no effect, so their impact is limited.

Remove these imports in preparation of migrating our code to become an
actual go module.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-05-30 15:59:10 +02:00
Paweł Gronowski
c75ca8ef10 client/container: use containerd errdefs checks
Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
2025-05-19 20:32:23 +02:00
Paweł Gronowski
051dae4fdc client/container: use gotest.tools-style asserts
Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
2025-05-19 12:04:05 +02:00
avoidaway
694c01620b chore: fix some function names in comment
Signed-off-by: avoidaway <cmoman@126.com>
2024-12-09 21:05:29 +08:00
Sebastiaan van Stijn
5bdbc2f026 client: ContainerCreate: normalize CapAdd, CapDrop capabilities
Before this change, capabilities would be sent un-normalized, un-sorted,
and could contain duplicates;

    docker create --name foo --cap-add SYS_ADMIN --cap-add sys_admin --cap-add cap_sys_admin --cap-add ALL busybox
    docker container inspect --format '{{json .HostConfig.CapAdd }}' foo
    ["SYS_ADMIN","sys_admin","cap_sys_admin","ALL"]

After this change, capabilities are sent in their normalized form, sorted,
and with duplicates removed;

    docker create --name foo --cap-add SYS_ADMIN --cap-add sys_admin --cap-add cap_sys_admin --cap-add ALL busybox
    docker container inspect --format '{{json .HostConfig.CapAdd }}' foo
    ["ALL", "CAP_SYS_ADMIN"]

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-09-26 17:11:13 +02:00
Sebastiaan van Stijn
be248d18b5 client.ContainerCreate: use container.CreateRequest instead of local type
This function used a locally defined `configWrapper`, which was identical
to the `container.CreateRequest`, with the exception of `CreateRequest`
defining `omitempty` for HostConfig and NetworkingConfig, but this should
not impact our use as the same type is used to handle the request on the
daemon side.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-09-26 15:30:16 +02:00
Sebastiaan van Stijn
6aea26b431 client: fix connection-errors being shadowed by API version mismatch errors
Commit e6907243af applied a fix for situations
where the client was configured with API-version negotiation, but did not yet
negotiate a version.

However, the checkVersion() function that was implemented copied the semantics
of cli.NegotiateAPIVersion, which ignored connection failures with the
assumption that connection errors would still surface further down.

However, when using the result of a failed negotiation for NewVersionError,
an API version mismatch error would be produced, masking the actual connection
error.

This patch changes the signature of checkVersion to return unexpected errors,
including failures to connect to the API.

Before this patch:

    docker -H unix:///no/such/socket.sock secret ls
    "secret list" requires API version 1.25, but the Docker daemon API version is 1.24

With this patch applied:

    docker -H unix:///no/such/socket.sock secret ls
    Cannot connect to the Docker daemon at unix:///no/such/socket.sock. Is the docker daemon running?

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-02-23 15:17:10 +01:00
Sebastiaan van Stijn
66ff1e063e client: update error-assertions in tests
- use is.ErrorType
- replace uses of client.IsErrNotFound for errdefs.IsNotFound, as
  the client no longer returns the old error-type.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-05-10 22:13:38 +02:00
Sebastiaan van Stijn
41b96bff55 update uses of container.ContainerCreateCreatedBody to CreateResponse
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-04-28 22:39:20 +02:00
Eng Zer Jun
c55a4ac779 refactor: move from io/ioutil to io and os package
The io/ioutil package has been deprecated in Go 1.16. This commit
replaces the existing io/ioutil functions with their new definitions in
io and os packages.

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
2021-08-27 14:56:57 +08:00
Brian Goff
7a9cb29fb9 Accept platform spec on container create
This enables image lookup when creating a container to fail when the
reference exists but it is for the wrong platform. This prevents trying
to run an image for the wrong platform, as can be the case with, for
example binfmt_misc+qemu.

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
2020-03-20 16:10:36 -07:00
Sebastiaan van Stijn
de10c7d013 client: reduce string-matching in tests
These checks were redundant, as we were not expecting
a specific string, just that a server-error or authentication
error was returned.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2019-10-13 17:00:40 +02:00
Sebastiaan van Stijn
161e0a90a6 Update tests to check returned errors
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2019-03-16 00:40:55 +01:00
Kir Kolyshkin
7d62e40f7e Switch from x/net/context -> context
Since Go 1.7, context is a standard package. Since Go 1.9, everything
that is provided by "x/net/context" is a couple of type aliases to
types in "context".

Many vendored packages still use x/net/context, so vendor entry remains
for now.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2018-04-23 13:52:44 -07:00
Daniel Nephin
4f0d95fa6e Add canonical import comment
Signed-off-by: Daniel Nephin <dnephin@docker.com>
2018-02-05 16:51:57 -05:00
Sebastiaan van Stijn
919726b5db Replace uses of deprecated IsErr...Notfound()
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2017-10-03 12:05:03 +02:00
Aaron.L.Xu
f8a4047796 why there are so many mistakes in our repo (up to /cmd)
Signed-off-by: Aaron.L.Xu <likexu@harmonycloud.cn>
2017-02-17 00:32:48 +08:00
Sebastiaan van Stijn
0ea7b143b0 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-15 01:59:57 +01:00
lixiaobing10051267
925c1427a8 add error information to distinguish different test scene
Signed-off-by: lixiaobing10051267 <li.xiaobing1@zte.com.cn>
2016-11-04 17:16:11 +08:00
Daniel Nephin
bad849fc82 Generate container create response from swagger spec.
Signed-off-by: Daniel Nephin <dnephin@docker.com>
2016-10-31 11:16:02 -04:00
Stephen J Day
9a072adff3 client: remove transport package
This package doesn't really seem to do anything of real interest.
Removing it and replacing with a few helper functions. Most of this was
maintaining a fork of ctxhttp to support a mock that was unnecessary.

We could probably do with a further refactor of the client interface.
There is a lot of confusion of between transport, http layer and
application layer that makes for some awkward code. This change
improves the situation to the point where no breaking changes are
introduced.

Signed-off-by: Stephen J Day <stephen.day@docker.com>
2016-09-19 11:19:55 -07:00
Michael Crosby
7c36a1af03 Move engine-api client package
This moves the engine-api client package to `/docker/docker/client`.

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
2016-09-07 11:05:58 -07:00