Commit Graph

496 Commits

Author SHA1 Message Date
Matthieu MOREL
6d737371b8 fix comparison rule from errorlint
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>

Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
2025-06-13 08:26:56 +00:00
Sebastiaan van Stijn
027355d7b3 container/stream: TestRaceUnbuffered: put unused testing.T to use
Some linters were complaining about the testing.T not being used; put
it to use to silence the linter.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-06-10 13:15:05 +02:00
Matthieu MOREL
bc9ec5fc02 fix emptyStringTest from go-critic
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
2025-06-07 09:57:59 +02:00
Sebastiaan van Stijn
89aa33001e container: 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:11 +02:00
Matthieu MOREL
364d8d8b31 container: replace uses of errdefs package
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
2025-05-28 05:38:15 +00:00
Sebastiaan van Stijn
ede73684c4 Merge pull request #49968 from thaJeztah/dont_persist_removal_in_progress
container: don't persist State.RemovalInProgress on disk
2025-05-16 18:31:52 +02:00
Sebastiaan van Stijn
76b24759f0 container: Snapshot.Health: change type to container.HealthStatus
container.HealthStatus is a pseudo-type (alias for string) that was
introduced in 1e4bb14bcd.

Changing this field to use that type as a potential stepping-stone
towards making that type a distinct type.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-05-15 12:17:08 +02:00
Sebastiaan van Stijn
854f3f62db container: don't persist State.RemovalInProgress on disk
State.RemovalInProgress was originally non-exported when it was added in
[40945fc186][1], adding a comment that the
field should not be persisted to disk.

But when moved to a separate package in [6bb0d1816a][2],
it was was exported, without adding `json:"-"`. As a result, it's now persisted
to disk;

    cat /var/lib/docker/containers/e493924a99cad918cda8048f967032729105ee072d563d734125cec46e1b5885/config.v2.json | jq .State
    {
      "Running": true,
      "Paused": false,
      "Restarting": false,
      "OOMKilled": false,
      "RemovalInProgress": false,
      "Dead": false,
      "Pid": 5053,
      "ExitCode": 0,
      "Error": "",
      "StartedAt": "2025-05-13T12:12:15.115512564Z",
      "FinishedAt": "0001-01-01T00:00:00Z",
      "Health": null
    }

Note that this type is used internally, and (while similar) is not used for
API responses;

    docker inspect e493924a99cad918cda8048f967032729105ee072d563d734125cec46e1b5885 | jq .[].State
    {
      "Status": "running",
      "Running": true,
      "Paused": false,
      "Restarting": false,
      "OOMKilled": false,
      "Dead": false,
      "Pid": 5053,
      "ExitCode": 0,
      "Error": "",
      "StartedAt": "2025-05-13T12:12:15.115512564Z",
      "FinishedAt": "0001-01-01T00:00:00Z"
    }

However, interestingly, [`daemon.restore`][3] does take this field into account
while restoring containers, which seems that it depends on the field being
persisted to disk. That logic was added in [ce72473197][4].
That logic may be redundant if we no longer persist to disk, as the `State.Dead`
is already set when cleaning up a container in [`daemon.cleanupContainer`][5].

[1]: 40945fc186
[2]: 6bb0d1816a (diff-60173e67d15f3085dd09956b3ffa83566ae25fec61cfe08ddd2e1c37223e3be7R24)
[3]: d42d79dceb/daemon/daemon.go (L498-L514)
[4]: ce72473197
[5]: 294f0c36e4/daemon/delete.go (L124-L126)

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-05-13 18:09:34 +02:00
Sebastiaan van Stijn
44b653ef99 container: deprecate IsValidStateString
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-05-13 15:03:42 +02:00
Sebastiaan van Stijn
b811829595 api/types/container: add ContainerState and const for container state
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-05-13 14:49:04 +02:00
Paweł Gronowski
0d6d4456ea Merge pull request #49958 from thaJeztah/container_state_defer
container: use defer for locks
2025-05-13 11:02:25 +00:00
Paweł Gronowski
7828a65aeb Merge pull request #49956 from thaJeztah/container_state_godoc
container: update GoDoc for State
2025-05-13 10:18:37 +00:00
Sebastiaan van Stijn
f84694ebdc container: use defer for locks
Makes the code slightly more idiomatic. These paths avoided uses of
defer because they came with an overhead in older versions of Go,
but this overhead should now be neglectible.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-05-12 13:17:18 +02:00
Paweł Gronowski
23825bc1fd Merge pull request #49893 from thaJeztah/move_more_container_types
container: deprecate IsValidHealthString
2025-05-12 10:51:10 +00:00
Sebastiaan van Stijn
89ee292709 container: update GoDoc for State
Better describe that various states are not mutually exclusive.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-05-12 10:28:20 +02:00
Sebastiaan van Stijn
7957a28859 container: remove GoDoc for deprecated aliases.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-05-08 21:59:08 +02:00
Sebastiaan van Stijn
fe403362b4 container: State.Wait(): don't use deprecated type
The container.WaitCondition type was deprecated in
100102108b, but this use of the
deprecated alias was left behind.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-05-08 21:56:07 +02:00
Sebastiaan van Stijn
df662ebc59 container: deprecate IsValidHealthString
Introduce a ValidateHealthStatus utility in api/types/container to
validate if a given HealthState is valid.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-05-08 16:52:52 +02:00
Rob Murray
8d5177b229 Merge pull request #49876 from thaJeztah/pseudo_type_healthstatus
api/types/container: define HealthStatus "pseudo" type
2025-04-28 13:43:50 +01:00
Sebastiaan van Stijn
533f0a0f75 Merge pull request #49877 from thaJeztah/pkg_less_output_vars
pkg/*: reduce named (error)-returns and some minor linting-fixes
2025-04-28 14:41:14 +02:00
Paweł Gronowski
9ee1716c41 Merge pull request #49874 from dmcgowan/container-types-to-api
Use container status values from api
2025-04-28 12:31:46 +00:00
Paweł Gronowski
1437cf558e Merge pull request #49878 from thaJeztah/api_builder_client_less_output_vars
api, builder, client, container: reduce named (error)-returns, naked returns and some minor linting-fixes
2025-04-28 12:15:38 +00:00
Sebastiaan van Stijn
f193ff1317 pkg/broadcaster: remove named err-returns
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-04-28 14:14:17 +02:00
Derek McGowan
100102108b Use container status values from api
Alias and deprecate the status types and constants from the root
container package. The root container package is intended for use
within the daemon and no the api package.

Signed-off-by: Derek McGowan <derek@mcg.dev>
2025-04-26 07:58:09 -07:00
Sebastiaan van Stijn
3b4e21081f container: remove unused named-returns
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-04-26 15:47:11 +02:00
Sebastiaan van Stijn
1e4bb14bcd api/types/container: define HealthStatus "pseudo" type
It currently is an alias for string, but may become a distinct type in future.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-04-26 15:26:26 +02:00
Sebastiaan van Stijn
c690e0076a use consts for health-status in tests
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-04-26 15:05:26 +02:00
Sebastiaan van Stijn
7c52c4d92e update go:build tags to go1.23 to align with vendor.mod
Go maintainers started to unconditionally update the minimum go version
for golang.org/x/ dependencies to go1.23, which means that we'll no longer
be able to support any version below that when updating those dependencies;

> all: upgrade go directive to at least 1.23.0 [generated]
>
> By now Go 1.24.0 has been released, and Go 1.22 is no longer supported
> per the Go Release Policy (https://go.dev/doc/devel/release#policy).
>
> For golang/go#69095.

This updates our minimum version to go1.23, as we won't be able to maintain
compatibility with older versions because of the above.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-04-17 15:43:19 +02:00
Sebastiaan van Stijn
97688e8d06 container: Container.SetupWorkingDirectory: remove use of pkg/idtools
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-04-10 12:30:34 +02:00
Sebastiaan van Stijn
d8a5e8928b replace uses of idtools.MkdirAllAndChown, MkdirAllAndChownNew
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-04-09 12:04:42 +02:00
Derek McGowan
d0154d3e59 Update to use github.com/moby/go-archive
Update use of idtools to moby/user for archive and other deprecated uses

Signed-off-by: Derek McGowan <derek@mcg.dev>
2025-04-08 17:35:05 -07:00
Sebastiaan van Stijn
6422ff2804 deprecate pkg/atomicwriter, migrate to github.com/moby/sys/atomicwriter
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-04-04 23:07:00 +02:00
Paweł Gronowski
003bf197d7 container: Update to any from interface{}
Only files that have go build version enforcing tag

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
2025-03-25 13:38:24 +01:00
Paweł Gronowski
95bf53fb6c container/stream: Don't log error when streams are properly closed
This fixes unnecessary errors being written to the daemon log after
copying container streams failed due the streams being closed explicitly:

time="2025-03-06T13:20:53.473232423Z" level=error msg="copy stream failed" error="io: read/write on closed pipe" stream=stdin

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
2025-03-06 14:28:17 +01:00
Sebastiaan van Stijn
220b3c591f container/stream/bytespipe: fix naked returns, output vars
Remove output variables, and use explicit returns

    container/stream/bytespipe/bytespipe.go:165:2: naked return in func `Read` with 37 lines of code (nakedret)
        return
        ^

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-03-04 13:55:44 +01:00
Sebastiaan van Stijn
60782e6d39 container: fix some errors on Windows
While going through some logs from CI, I noticed this log-entry on Windows,
produced as part of a test;

    2025-02-25T03:23:17.6584227Z [Error] Handler for POST /v1.48/containers/b47b1e632188426d6d42a4be04f9a3cc1eca40cfed9536d277011052af0b04f5/update returned error: Cannot update container b47b1e632188426d6d42a4be04f9a3cc1eca40cfed9536d277011052af0b04f5: Restart policy cannot be updated because AutoRemove is enabled for the container

While updating is an error for the user, it's not an error in the daemon,
so we should return the correct error-type (and avoid logging it as an
error in daemon logs).

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-02-25 17:53:25 +01:00
Sebastiaan van Stijn
529a56d31a container: Container.StopSignal: fix handling of invalid signals
Commit 0e50d946a2 introduced a feature to
allow a custom stop-signal to be set. As part of this, existing code to
parse the signal was extracted to `signal.ParseSignal()`, which accepts
a string either containing a numeric value or a named signal.

When failing to parse the given signal, it returns an error and a magic
"-1" signal. The changes in 0e50d946a2 used
the error when creating a container, but for existing container configs,
it would ignore the error and instead check if the signal was "0", in
which case it would fall back to use the default stop-signal (SIGTERM).

Given that  `signal.ParseSignal()` returns "-1" (not "0") for invalid
signals, this would result in the failure going undetected and "-1"
being used instead of the intended default (SIGTERM).

In practice, this issues would unlikely be encountered, as custom signals
are validated when creating the container, but it would be possible for
an image to contain an invalid signal, which would be used by the container
as default.

This patch updates the logic to only use the custom value if no error is
produced and a non-zero, positive signal is returned.

A test-case was added that would fail before this patch:

    go test -v -run TestContainerStopSignal
    === RUN   TestContainerStopSignal
        container_test.go:34: assertion failed: signal -1 (s syscall.Signal) != terminated (defaultStopSignal syscall.Signal)
    --- FAIL: TestContainerStopSignal (0.00s)

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-02-15 18:55:03 +01:00
Sebastiaan van Stijn
ef0fe1ac00 container: define defaultStopSignal as a syscall.Signal
"SIGTERM" is defined both for Windows and Linux, so we can define the
signal to use as a syscall.Signal, instead of parsing it from a string
whenever we need to use the default.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-02-15 18:29:35 +01:00
Sebastiaan van Stijn
0a4c4c9e82 container: some cleanups in view tests
- use t.TempDir() instead of TestMain creating a directory to make
  tests self-contained.
- fix some unhandled errors, and missing assertions for error-types
- assert with gotest.tools, but kept the Benchmark tests as-is for now,
  to make sure gotest.tools doesn't impact the results.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-02-15 16:43:53 +01:00
Sebastiaan van Stijn
49f2320bc7 container: some cleanups in tests
- use t.TempDir()
- use t.Name() instead of hard-coding name
- assert with gotest.tools
- fix some unhandled errors

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-02-15 16:43:53 +01:00
Sebastiaan van Stijn
07aa4d96ea container: TestReplaceAndAppendEnvVars: assert with gotest.tools
Assert the actual results match the expected one, which should make the
test more complete, and reduces some noise by removing a `t.Log`.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-02-15 16:43:52 +01:00
Sebastiaan van Stijn
0914e97df2 container: rename test file
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-02-15 16:43:52 +01:00
Jonathan A. Sternberg
acc4d45278 container: ensure image manifest is deep copied from the container into the snapshot
Signed-off-by: Jonathan A. Sternberg <jonathan.sternberg@docker.com>
2025-02-14 09:29:50 -06:00
Sebastiaan van Stijn
6ee1558852 Merge pull request #49417 from thaJeztah/fix_linting_step1
Fix linting issues to allow enabling more linters
2025-02-13 23:58:24 +01:00
Jonathan A. Sternberg
927e07e46e API: add Platform (OS and Architecture) to /containers/json
Adds platform information to containers (for `docker ps`).

Signed-off-by: Jonathan A. Sternberg <jonathan.sternberg@docker.com>
2025-02-13 14:52:21 -06:00
Sebastiaan van Stijn
b343d235a0 container: State.conditionAlreadyMet: make switch exhaustive (exhaustive)
Adding a `default` statement, and not an explicit "WaitConditionNextExit",
so that disabling the "default-signifies-exhaustive" linter option will
make it show up.

    container/state.go:237:2: missing cases in switch of type container.WaitCondition: container.WaitConditionNextExit (exhaustive)
        switch condition {
        ^

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-02-09 13:22:51 +01:00
Sebastiaan van Stijn
d50492a70d container/stream: avoid allocations with (*bytes.Buffer).WriteString (mirror)
container/stream/unbuffered_test.go:139:3: avoid allocations with (*bytes.Buffer).WriteString (mirror)
            buf.Write([]byte(testLine + "\n"))
            ^
    container/stream/unbuffered_test.go:142:2: avoid allocations with (*bytes.Buffer).WriteString (mirror)
        buf.Write([]byte(testLine))
        ^

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-02-09 13:22:46 +01:00
Tianon Gravi
f8187c0214 Error on invalid requests for writable-cgroups
This makes `WritableCgroups` a pointer so we can error when it's specified in invalid configurations (both rootless and user namespaces).

Signed-off-by: Tianon Gravi <admwiggin@gmail.com>
2025-01-16 15:59:06 -08:00
Vincent Batts
e3cdd59a82 daemon/&container/: enable --security-opt writable-cgroups=true as an option
Fixes #42040
Closes #42043

Rather than making cgroups read-write by default, instead have a flag
for making it possible.

Since these security options are passed through the cli to daemon API,
no changes are needed to docker-cli.

Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
2025-01-16 12:58:17 -08:00
Derek McGowan
0aa8fe0bf9 Update to containerd v2.0.2, buildkit v0.19.0-rc2
Update buildkit version to commit which uses 2.0

Signed-off-by: Derek McGowan <derek@mcg.dev>
2025-01-15 14:09:30 +01:00