Ability to add new extrahost (new line into /etc/hosts file) to already created container #1906
-
Could somebody help me please? Same question about adding some extra env variables |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Hey @igribkov thanks for sending this question. It's possible to set the extrahost of a container leveraging the func TestDockerContainerWithExtraHosts(t *testing.T) {
ctx := context.Background()
nginxC, err := GenericContainer(ctx, GenericContainerRequest{
ProviderType: providerType,
ContainerRequest: ContainerRequest{
Image: nginxAlpineImage,
ExposedPorts: []string{nginxDefaultPort},
Env: map[string]string{"FOO": "BAR"},
WaitingFor: wait.ForListeningPort(nginxDefaultPort),
HostConfigModifier: func(hc *container.HostConfig) {
hc.ExtraHosts = []string{"DEV01:172.16.0.10", "DEV01:172.16.0.11"}
},
},
Started: true,
})
require.NoError(t, err)
terminateContainerOnEnd(t, ctx, nginxC)
_, reader, err := nginxC.Exec(ctx, []string{"cat", "/etc/hosts"}, tcexec.Multiplexed())
require.NoError(t, err)
hostsBytes, err := io.ReadAll(reader)
require.NoError(t, err)
hosts := string(hostsBytes)
assert.Contains(t, string(hosts), "172.16.0.10\tDEV01")
assert.Contains(t, string(hosts), "172.16.0.11\tDEV01")
} Please pay attention to the Env HostConfigModifier fields in the request struct 👀 |
Beta Was this translation helpful? Give feedback.
-
I'm closing this discussion as resolved, thanks for asking! |
Beta Was this translation helpful? Give feedback.
@igribkov ah ok, I'm sorry, I thought your question was about container creation instead of "after it's been created and its running", right?
You can leverage the
PostStarts
container lifecycle hook, to inject a function that appends the entries you are interested. Please check the logs for the lifecycle hooks here, and an example here:testcontainers-go/options_test.go
Lines 131 to 162 in 5562dbc