From 6747d41646b680130d614185249aeed44ef48c4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Giedrius=20Statkevi=C4=8Dius?= Date: Mon, 14 Jul 2025 14:33:00 +0300 Subject: [PATCH] env: add custom DNS capability Add ability to use custom DNS server where /etc/hosts doesn't cut it since it only supports 1:1 mapping. --- env.go | 12 ++++++++++-- env_docker.go | 9 ++++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/env.go b/env.go index 6d921bf..7dc3498 100644 --- a/env.go +++ b/env.go @@ -27,8 +27,16 @@ type environmentOptions struct { verbose bool name string - volumes []string - cpus string + volumes []string + cpus string + dnsServer string +} + +// WithDNS sets DNS server to be used by the environment. +func WithDNS(dnsServer string) EnvironmentOption { + return func(o *environmentOptions) { + o.dnsServer = dnsServer + } } func WithCPUs(cpus string) EnvironmentOption { diff --git a/env_docker.go b/env_docker.go index bb9f76e..c659716 100644 --- a/env_docker.go +++ b/env_docker.go @@ -44,7 +44,9 @@ type DockerEnvironment struct { hostAddr string dockerVolumes []string - cpus string + + dnsServer string + cpus string registered map[string]struct{} listeners []EnvironmentListener @@ -115,6 +117,7 @@ func New(opts ...EnvironmentOption) (_ *DockerEnvironment, err error) { registered: map[string]struct{}{}, dockerVolumes: e.volumes, cpus: e.cpus, + dnsServer: e.dnsServer, } // Force a shutdown in order to cleanup from a spurious situation in case @@ -318,6 +321,10 @@ func (e *DockerEnvironment) buildDockerRunArgs(name string, ports map[string]int args = append(args, "--privileged") } + if e.dnsServer != "" { + args = append(args, "--dns", e.dnsServer) + } + for _, c := range opts.Capabilities { args = append(args, "--cap-add", string(c)) }