Skip to content

Fix mac_address ignored on API v1.44+ when network is specified#3396

Open
lasssim wants to merge 1 commit into
docker:mainfrom
lasssim:fix/mac-address-endpoint-config
Open

Fix mac_address ignored on API v1.44+ when network is specified#3396
lasssim wants to merge 1 commit into
docker:mainfrom
lasssim:fix/mac-address-endpoint-config

Conversation

@lasssim

@lasssim lasssim commented Apr 21, 2026

Copy link
Copy Markdown

Summary

Docker Engine API v1.44+ (Docker Engine 25.0+) deprecated the mac_address field on the container config. The MAC address must now be set via EndpointConfig in the networking_config for the target network.

Currently, when both network and mac_address are passed to containers.create() or containers.run(), the mac_address ends up on the container config where modern Docker engines silently ignore it. This PR fixes that by moving mac_address into the EndpointConfig for the target network when API version >= 1.44.

On older API versions (< 1.44) the behaviour is unchanged.

Reproducer

import docker
c = docker.from_env()

net = c.networks.create('test-mac')
container = c.containers.run(
    'alpine', 'cat /sys/class/net/eth0/address',
    network='test-mac',
    mac_address='02:42:ac:11:00:99',
    detach=True,
)
container.wait()
print(container.logs().decode().strip())  # Random MAC, not 02:42:ac:11:00:99
container.remove()
net.remove()

Fix

In _create_container_args(), when network is specified and the API version is >= 1.44, pop mac_address from the container config and inject it into the EndpointConfig for the target network.

The version check uses the existing version_gte() helper and the version value already passed through host_config_kwargs.

Test plan

  • Updated existing test_create_container_args to expect MacAddress in EndpointsConfig instead of on the container config (test uses DEFAULT_DOCKER_API_VERSION = '1.45')
  • All 50 existing container model unit tests pass
  • Manually verified with Docker Engine 29.3.0 (API 1.54) on bridge, custom networks, and with user-provided networking_config

Fixes #3283

@lasssim lasssim force-pushed the fix/mac-address-endpoint-config branch from 6c05d8e to 46576cc Compare April 21, 2026 12:17
Docker Engine API v1.44+ (Docker Engine 25.0+) deprecated the
mac_address field on the container config.  The MAC address must now
be set via EndpointConfig in the networking_config for the target
network.

When both `network` and `mac_address` are passed to
`containers.create()` or `containers.run()`, and the negotiated API
version is >= 1.44, move `mac_address` into the EndpointConfig for
the target network so it takes effect on modern Docker engines.

On older API versions (< 1.44) the behaviour is unchanged —
`mac_address` stays on the container config where it has always
worked.

Fixes docker#3283

Signed-off-by: Simon Lasselsberger <simon@lasssim.com>
@lasssim lasssim force-pushed the fix/mac-address-endpoint-config branch from 46576cc to 2cc63c9 Compare April 21, 2026 12:21
@thaJeztah thaJeztah closed this Jul 9, 2026
@thaJeztah thaJeztah reopened this Jul 9, 2026
@thaJeztah thaJeztah requested a review from Copilot July 9, 2026 13:32

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates container argument construction to ensure mac_address is honored on Docker Engine API v1.44+ when a target network is specified, by placing the MAC address into the network’s EndpointConfig (networking_config) instead of the deprecated container-config field.

Changes:

  • Move mac_address into networking_config.EndpointsConfig[network].MacAddress when API version >= 1.44 and network is specified.
  • Keep legacy behavior (leave mac_address on container config) for API versions < 1.44.
  • Update and add unit tests to cover old/new API behavior for MAC address placement.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
docker/models/containers.py Adds API-version-gated logic to relocate mac_address into the target network’s EndpointConfig.
tests/unit/models_containers_test.py Updates expected args and adds tests to validate behavior across API version boundaries.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +1174 to +1176
endpoint = networking_config.get(network)
if endpoint is not None and 'MacAddress' not in endpoint:
endpoint['MacAddress'] = mac_address
Comment on lines +249 to +262
def test_create_container_args_mac_address_new_api(self):
"""On API >= 1.44, mac_address moves into EndpointConfig."""
create_kwargs = _create_container_args({
'image': 'alpine',
'command': '/bin/sh',
'mac_address': 'abc123',
'network': 'foo',
'version': '1.44',
})

assert 'mac_address' not in create_kwargs
net_config = create_kwargs['networking_config']
assert net_config['EndpointsConfig']['foo']['MacAddress'] == 'abc123'

@vvoland vvoland requested a review from docker-agent July 10, 2026 10:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

create_host_config and mac_address

3 participants