Skip to content

Commit 9e929db

Browse files
Merge pull request #21987 from TomSweeneyRedHat/dev/tsweeney/pickDOcker
Backport two docker CLI compatibility fixes
2 parents c82fdc8 + 56ca3b8 commit 9e929db

File tree

3 files changed

+43
-8
lines changed

3 files changed

+43
-8
lines changed

libpod/container_internal_common.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import (
4848
"github.com/containers/storage/pkg/archive"
4949
"github.com/containers/storage/pkg/idtools"
5050
"github.com/containers/storage/pkg/lockfile"
51+
"github.com/containers/storage/pkg/unshare"
5152
stypes "github.com/containers/storage/types"
5253
securejoin "github.com/cyphar/filepath-securejoin"
5354
runcuser "github.com/opencontainers/runc/libcontainer/user"
@@ -633,14 +634,15 @@ func (c *Container) generateSpec(ctx context.Context) (s *spec.Spec, cleanupFunc
633634
nofileSet := false
634635
nprocSet := false
635636
isRootless := rootless.IsRootless()
636-
if isRootless {
637-
if g.Config.Process != nil && g.Config.Process.OOMScoreAdj != nil {
638-
var err error
639-
*g.Config.Process.OOMScoreAdj, err = maybeClampOOMScoreAdj(*g.Config.Process.OOMScoreAdj)
640-
if err != nil {
641-
return nil, nil, err
642-
}
637+
isRunningInUserNs := unshare.IsRootless()
638+
if isRunningInUserNs && g.Config.Process != nil && g.Config.Process.OOMScoreAdj != nil {
639+
var err error
640+
*g.Config.Process.OOMScoreAdj, err = maybeClampOOMScoreAdj(*g.Config.Process.OOMScoreAdj)
641+
if err != nil {
642+
return nil, nil, err
643643
}
644+
}
645+
if isRootless {
644646
for _, rlimit := range c.config.Spec.Process.Rlimits {
645647
if rlimit.Type == "RLIMIT_NOFILE" {
646648
nofileSet = true

pkg/api/handlers/compat/containers_create.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,17 @@ func cliOpts(cc handlers.CreateContainerConfig, rtc *config.Config) (*entities.C
365365
}
366366
}
367367

368-
networks[netName] = netOpts
368+
// Report configuration error in case bridge mode is not used.
369+
if !nsmode.IsBridge() && (len(netOpts.Aliases) > 0 || len(netOpts.StaticIPs) > 0 || len(netOpts.StaticMAC) > 0) {
370+
return nil, nil, fmt.Errorf("networks and static ip/mac address can only be used with Bridge mode networking")
371+
} else if nsmode.IsBridge() {
372+
// Docker CLI now always sends the end point config when using the default (bridge) mode
373+
// however podman configuration doesn't expect this to define this at all when not in bridge
374+
// mode and the podman server config might override the default network mode to something
375+
// else than bridge. So adapt to the podman expectation and define custom end point config
376+
// only when really using the bridge mode.
377+
networks[netName] = netOpts
378+
}
369379
}
370380

371381
netInfo.Networks = networks

test/apiv2/20-containers.at

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,29 @@ t GET containers/$cid/json 200 \
527527

528528
t DELETE containers/$cid?v=true 204
529529

530+
# test create container like Docker >= 25 cli: NetworkMode="default" but EndpointsConfig struct is explictly set and netns="host"
531+
t POST containers/create \
532+
Image=$IMAGE \
533+
HostConfig='{"NetworkMode":"default"}' \
534+
NetworkingConfig='{"EndpointsConfig":{"default":{"IPAMConfig":null,"Links":null,"Aliases":null,"MacAddress":"","NetworkID":"","EndpointID":"","Gateway":"","IPAddress":"","IPPrefixLen":0,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"DriverOpts":null,"DNSNames":null}}}' \
535+
201 \
536+
.Id~[0-9a-f]\\{64\\}
537+
cid=$(jq -r '.Id' <<<"$output")
538+
t GET containers/$cid/json 200 \
539+
.HostConfig.NetworkMode="host"
540+
541+
t DELETE containers/$cid?v=true 204
542+
543+
# test creating a container fails with netns="hosts" on podman side but keep using the default network mode
544+
# on docker CLI side and trying to use --ip 1.2.3.4 which is only valid for the bridge network mode (docker CLI
545+
# will assume the default is the bridge mode, so it's valid from docker CLI point of view).
546+
t POST containers/create \
547+
Image=$IMAGE \
548+
HostConfig='{"NetworkMode":"default"}' \
549+
NetworkingConfig='{"EndpointsConfig":{"default":{"IPAMConfig":null,"Links":null,"Aliases":null,"MacAddress":"","NetworkID":"","EndpointID":"","Gateway":"","IPAddress":"1.2.3.4","IPPrefixLen":0,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"DriverOpts":null,"DNSNames":null}}}' \
550+
500 \
551+
.cause="networks and static ip/mac address can only be used with Bridge mode networking"
552+
530553
# Restart with the default containers.conf for next tests.
531554
stop_service
532555
start_service

0 commit comments

Comments
 (0)