compat API: allow MacAddress on container config

docker-compose sets the mac address in the container config and not the
network endpoint config. This is ugly when you have more than one
network, in this case docker just chooses the first network.

Fixes #16411

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
This commit is contained in:
Paul Holzinger
2022-12-05 16:52:36 +01:00
parent f223bbab62
commit 2f5025a2d7
3 changed files with 17 additions and 1 deletions

View File

@ -287,6 +287,9 @@ func cliOpts(cc handlers.CreateContainerConfig, rtc *config.Config) (*entities.C
NoHosts: rtc.Containers.NoHosts, NoHosts: rtc.Containers.NoHosts,
} }
// sigh docker-compose sets the mac address on the container config instead on the per network endpoint config
containerMacAddress := cc.MacAddress
// network names // network names
switch { switch {
case len(cc.NetworkingConfig.EndpointsConfig) > 0: case len(cc.NetworkingConfig.EndpointsConfig) > 0:
@ -331,6 +334,16 @@ func cliOpts(cc handlers.CreateContainerConfig, rtc *config.Config) (*entities.C
return nil, nil, fmt.Errorf("failed to parse the mac address %q", endpoint.MacAddress) return nil, nil, fmt.Errorf("failed to parse the mac address %q", endpoint.MacAddress)
} }
netOpts.StaticMAC = types.HardwareAddr(staticMac) netOpts.StaticMAC = types.HardwareAddr(staticMac)
} else if len(containerMacAddress) > 0 {
// docker-compose only sets one mac address for the container on the container config
// If there are more than one network attached it will end up on the first one,
// which is not deterministic since we iterate a map. Not nice but this matches docker.
staticMac, err := net.ParseMAC(containerMacAddress)
if err != nil {
return nil, nil, fmt.Errorf("failed to parse the mac address %q", containerMacAddress)
}
netOpts.StaticMAC = types.HardwareAddr(staticMac)
containerMacAddress = ""
} }
} }

View File

@ -2,6 +2,7 @@ version: "3.2"
services: services:
test: test:
image: alpine image: alpine
mac_address: 32:b5:b2:55:48:72
networks: networks:
net1: net1:
ipv4_address: 10.123.0.253 ipv4_address: 10.123.0.253

View File

@ -5,4 +5,6 @@ if [ "$TEST_FLAVOR" = "compose_v2" ]; then
ctr_name="ipam_set_ip-test-1" ctr_name="ipam_set_ip-test-1"
fi fi
podman container inspect "$ctr_name" --format '{{ .NetworkSettings.Networks.ipam_set_ip_net1.IPAddress }}' podman container inspect "$ctr_name" --format '{{ .NetworkSettings.Networks.ipam_set_ip_net1.IPAddress }}'
like "$output" "10.123.0.253" "$testname : ip address is set" is "$output" "10.123.0.253" "$testname : ip address is set"
podman container inspect "$ctr_name" --format '{{ .NetworkSettings.Networks.ipam_set_ip_net1.MacAddress }}'
is "$output" "32:b5:b2:55:48:72" "$testname : mac address is set"