From a2d26878e72a058dbeb73e76fa04d88c7fabb4f2 Mon Sep 17 00:00:00 2001 From: Lauris BH Date: Wed, 23 Nov 2022 14:01:45 +0000 Subject: [PATCH] Add internal DNS server if no custom DNS is set (#45) Fixes #44 Co-authored-by: Lauris BH Reviewed-on: https://codeberg.org/woodpecker-plugins/plugin-docker-buildx/pulls/45 Reviewed-by: 6543 <6543@obermui.de> Co-authored-by: Lauris BH Co-committed-by: Lauris BH --- Corefile | 3 +++ Dockerfile.multiarch | 3 +++ plugin/coredns.go | 48 ++++++++++++++++++++++++++++++++++++++++++++ plugin/impl.go | 11 ++++++++++ 4 files changed, 65 insertions(+) create mode 100644 Corefile create mode 100644 plugin/coredns.go diff --git a/Corefile b/Corefile new file mode 100644 index 0000000..231dccf --- /dev/null +++ b/Corefile @@ -0,0 +1,3 @@ +.:53 { + forward . /etc/resolv.conf +} diff --git a/Dockerfile.multiarch b/Dockerfile.multiarch index b9f3923..28c7eb5 100644 --- a/Dockerfile.multiarch +++ b/Dockerfile.multiarch @@ -15,6 +15,9 @@ RUN --mount=type=cache,target=/root/.cache/go-build \ FROM docker/buildx-bin:${BUILDX_VERSION} as buildx-bin FROM docker:${DOCKER_VERSION} +RUN apk --update --no-cache add coredns + +COPY --from=build /src/Corefile /etc/coredns/Corefile COPY --from=buildx-bin /buildx /usr/libexec/docker/cli-plugins/docker-buildx COPY --from=build /src/plugin-docker-buildx /bin/plugin-docker-buildx diff --git a/plugin/coredns.go b/plugin/coredns.go new file mode 100644 index 0000000..7bcc854 --- /dev/null +++ b/plugin/coredns.go @@ -0,0 +1,48 @@ +package plugin + +import ( + "io" + "net" + "os" + "os/exec" +) + +func (p Plugin) startCoredns() { + cmd := exec.Command("coredns", "-conf", "/etc/coredns/Corefile") + if p.settings.Daemon.Debug { + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + } else { + cmd.Stdout = io.Discard + cmd.Stderr = io.Discard + } + go func() { + trace(cmd) + cmd.Run() + }() +} + +func getContainerIP() (string, error) { + ifaces, err := net.Interfaces() + if err != nil { + return "", err + } + for _, i := range ifaces { + addrs, err := i.Addrs() + if err != nil { + return "", err + } + for _, addr := range addrs { + switch v := addr.(type) { + case *net.IPNet: + if v.IP.IsLoopback() { + continue + } + if v.IP.To4() != nil { + return v.IP.String(), nil + } + } + } + } + return "", nil +} diff --git a/plugin/impl.go b/plugin/impl.go index de2ec40..7ed723e 100644 --- a/plugin/impl.go +++ b/plugin/impl.go @@ -209,6 +209,17 @@ func (p *Plugin) writeBuildkitConfig() error { func (p *Plugin) Execute() error { // start the Docker daemon server if !p.settings.Daemon.Disabled { + // If no custom DNS value set start internal DNS server + if len(p.settings.Daemon.DNS.Value()) == 0 { + ip, err := getContainerIP() + if err != nil { + logrus.Warnf("error detecting IP address: %v", err) + } else if ip != "" { + p.startCoredns() + p.settings.Daemon.DNS.Set(ip) + } + } + p.startDaemon() }