Skip to content

Commit

Permalink
[docker compat] Don't overwrite the NetworkMode from "default" to "br…
Browse files Browse the repository at this point in the history
…idge" if containers.conf specifies a non-default configuration.

Fixes containers#16915 (only the part about docker client).

Signed-off-by: Romain Geissler <romain.geissler@amadeus.com>
  • Loading branch information
Romain-Geissler-1A committed Jan 10, 2023
1 parent 7c95a5d commit 1638cc5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
8 changes: 6 additions & 2 deletions pkg/api/handlers/compat/containers_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/containers/podman/v4/pkg/rootless"
"github.com/containers/podman/v4/pkg/specgen"
"github.com/containers/podman/v4/pkg/specgenutil"
"github.com/containers/podman/v4/pkg/util"
"github.com/containers/storage"
"github.com/docker/docker/api/types/mount"
"github.com/gorilla/schema"
Expand Down Expand Up @@ -262,9 +263,12 @@ func cliOpts(cc handlers.CreateContainerConfig, rtc *config.Config) (*entities.C
}

// special case for NetworkMode, the podman default is slirp4netns for
// rootless but for better docker compat we want bridge.
// rootless but for better docker compat we want bridge. Do this only we
// the default config in containers.conf wasn't overriden to use another
// value than the default one: private.
netmode := string(cc.HostConfig.NetworkMode)
if netmode == "" || netmode == "default" {
configDefaultNetNS := util.DefaultContainerConfig().Containers.NetNS
if (netmode == "" || netmode == "default") && (configDefaultNetNS == "" || configDefaultNetNS == string(specgen.Default) || configDefaultNetNS == string(specgen.Private)) {
netmode = "bridge"
}

Expand Down
18 changes: 18 additions & 0 deletions test/apiv2/20-containers.at
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,24 @@ t GET containers/$cid/json 200 \

t DELETE containers/$cid?v=true 204

# test create with default netns="host"
stop_service
CONTAINERS_CONF=$TESTS_DIR/containers.no_hosts.conf start_service

# check that the default docker netns "default" is rewritten to "host"
# when the containers.conf explicitly uses "host"
t POST containers/create Image=$IMAGE HostConfig='{"NetworkMode":"default"}' 201 \
.Id~[0-9a-f]\\{64\\}
cid=$(jq -r '.Id' <<<"$output")
t GET containers/$cid/json 200 \
.HostConfig.NetworkMode="host"

t DELETE containers/$cid?v=true 204

# Restart with the default containers.conf for next tests.
stop_service
start_service

# Test Compat Create with healthcheck, check default values
t POST containers/create Image=$IMAGE Cmd='["top"]' Healthcheck='{"Test":["true"]}' 201 \
.Id~[0-9a-f]\\{64\\}
Expand Down
2 changes: 2 additions & 0 deletions test/apiv2/containers.host-netns.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[containers]
netns="host"

0 comments on commit 1638cc5

Please sign in to comment.