From 98a7a13aad1b8a9652954e2f3d57e5f43f3dc02e Mon Sep 17 00:00:00 2001 From: ArtuoS Date: Tue, 14 Jul 2026 09:54:23 -0300 Subject: [PATCH] Support extra_hosts on pre_start Signed-off-by: ArtuoS --- pkg/compose/pre_start.go | 1 + pkg/compose/pre_start_test.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/pkg/compose/pre_start.go b/pkg/compose/pre_start.go index 439969e16e..2733540c5d 100644 --- a/pkg/compose/pre_start.go +++ b/pkg/compose/pre_start.go @@ -150,6 +150,7 @@ func (s *composeService) createPreStartContainer( AutoRemove: true, Privileged: hook.Privileged, VolumesFrom: []string{ctr.ID}, + ExtraHosts: service.ExtraHosts.AsList(":"), } apiVersion, err := s.RuntimeAPIVersion(ctx) diff --git a/pkg/compose/pre_start_test.go b/pkg/compose/pre_start_test.go index 005bc5a012..6bd97eb0a2 100644 --- a/pkg/compose/pre_start_test.go +++ b/pkg/compose/pre_start_test.go @@ -247,6 +247,40 @@ func TestPreStart_VolumesFromServiceContainer(t *testing.T) { assert.Assert(t, gotAutoRemove) } +func TestPreStart_ExtraHostsPassedToContainer(t *testing.T) { + tested, apiClient := newPreStartTestService(t) + + project := &types.Project{Name: "demo"} + service := types.ServiceConfig{ + Name: "web", + Image: "alpine", + ExtraHosts: types.HostsList{ + "somehost": {"162.242.195.82"}, + }, + PreStart: []types.ServiceHook{ + {Image: "alpine", Command: types.ShellCommand{"true"}}, + }, + } + ctr := container.Summary{ID: "service-ctr-id"} + + var gotExtraHosts []string + apiClient.EXPECT().ContainerCreate(gomock.Any(), gomock.Any()). + DoAndReturn(func(_ any, opts client.ContainerCreateOptions) (client.ContainerCreateResult, error) { + gotExtraHosts = opts.HostConfig.ExtraHosts + return client.ContainerCreateResult{ID: "hook-1"}, nil + }) + apiClient.EXPECT().ContainerStart(gomock.Any(), "hook-1", gomock.Any()). + Return(client.ContainerStartResult{}, nil) + apiClient.EXPECT().ContainerLogs(gomock.Any(), "hook-1", gomock.Any()). + Return(emptyLogs(), nil) + apiClient.EXPECT().ContainerWait(gomock.Any(), "hook-1", gomock.Any()). + Return(waitResultExit(0)) + + err := tested.runPreStart(t.Context(), project, service, ctr, func(api.ContainerEvent) {}) + assert.NilError(t, err) + assert.DeepEqual(t, gotExtraHosts, []string{"somehost:162.242.195.82"}) +} + func TestPreStart_ContainerCreateFailurePropagates(t *testing.T) { tested, apiClient := newPreStartTestService(t)