Skip to content

Support extra_hosts on pre_start#13940

Open
ArtuoS wants to merge 1 commit into
docker:mainfrom
ArtuoS:13939-support-extra-hosts
Open

Support extra_hosts on pre_start#13940
ArtuoS wants to merge 1 commit into
docker:mainfrom
ArtuoS:13939-support-extra-hosts

Conversation

@ArtuoS

@ArtuoS ArtuoS commented Jul 14, 2026

Copy link
Copy Markdown

What I did

Fixes #13939.

pre_start hooks run as ephemeral containers with their own HostConfig, but the configuration was built with only four fields (AutoRemove, Privileged, VolumesFrom, NetworkMode), omitting ExtraHosts entirely. This meant custom host mappings defined on the service were invisible inside pre_start containers, even though they were correctly applied to the main service containers.

This PR propagates service.ExtraHosts into the pre_start container's HostConfig, matching the behavior of regular containers created via getCreateConfigs().

Location Change
pkg/compose/pre_start.gocreatePreStartContainer Set ExtraHosts: service.ExtraHosts.AsList(":") on the hook container's HostConfig
pkg/compose/pre_start_test.go New TestPreStart_ExtraHostsPassedToContainer verifying the host mapping is forwarded

Scope

Only ExtraHosts is in scope. Other HostConfig fields (e.g. DNS, CapAdd, SecurityOpt) are not propagated to pre_start containers and can be addressed separately if needed.

How to test manually

services:
  test:
    image: busybox
    extra_hosts:
      - "somehost:162.242.195.82"
    pre_start:
      - command: cat /etc/hosts
    command: cat /etc/hosts
$ docker compose up
test pre_start[0] -> | 127.0.0.1	localhost
test pre_start[0] -> | 162.242.195.82	somehost   ← now visible
test-1               | 162.242.195.82	somehost

@ArtuoS
ArtuoS requested review from a team as code owners July 14, 2026 12:58
@ArtuoS
ArtuoS requested review from glours and ndeloof July 14, 2026 12:58
Signed-off-by: ArtuoS <arthureduardomolinari@gmail.com>
@ArtuoS
ArtuoS force-pushed the 13939-support-extra-hosts branch from fa88625 to 98a7a13 Compare July 14, 2026 15:45
@ndeloof

ndeloof commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

This assumes we want the pre_start to inherit config from service, which must be debated (what should be inherited? What should not? How to override?)

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.

pre_start containers don't inherit/support extra_hosts

2 participants