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 5423829..92e1a22 100644 --- a/Dockerfile.multiarch +++ b/Dockerfile.multiarch @@ -27,7 +27,7 @@ ENV BUILDX_VERSION="${BUILDX_VERSION:-v0.10.4}" ENV DOCKER_HOST=unix:///var/run/docker.sock RUN apk --update add --virtual .build-deps curl && \ - apk --update add --no-cache git && \ + apk --update add --no-cache git coredns && \ mkdir -p /usr/lib/docker/cli-plugins/ && \ curl -SsL -o /usr/lib/docker/cli-plugins/docker-buildx \ "https://github.com/docker/buildx/releases/download/v${BUILDX_VERSION##v}/buildx-v${BUILDX_VERSION##v}.${TARGETOS:-linux}-${TARGETARCH:-amd64}" && \ @@ -36,5 +36,6 @@ RUN apk --update add --virtual .build-deps curl && \ rm -rf /var/cache/apk/* && \ rm -rf /tmp/* +COPY --from=build /src/Corefile /etc/coredns/Corefile COPY --from=build /src/dist/drone-docker-buildx /bin/drone-docker-buildx ENTRYPOINT ["/usr/local/bin/dockerd-entrypoint.sh", "drone-docker-buildx"] diff --git a/plugin/coredns.go b/plugin/coredns.go new file mode 100644 index 0000000..2178706 --- /dev/null +++ b/plugin/coredns.go @@ -0,0 +1,40 @@ +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) { + netInterfaceAddrList, err := net.InterfaceAddrs() + if err != nil { + return "", err + } + + for _, netInterfaceAddr := range netInterfaceAddrList { + netIP, ok := netInterfaceAddr.(*net.IPNet) + if ok && !netIP.IP.IsLoopback() && netIP.IP.To4() != nil { + return netIP.IP.String(), nil + } + } + + return "", nil +} diff --git a/plugin/impl.go b/plugin/impl.go index f4ab22c..ce530b7 100644 --- a/plugin/impl.go +++ b/plugin/impl.go @@ -111,9 +111,29 @@ func (p *Plugin) Validate() error { } // Execute provides the implementation of the plugin. +// +//nolint:gocognit func (p *Plugin) Execute() error { // start the Docker daemon server + //nolint: nestif 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) + } + + if ip != "" { + logrus.Debugf("discovered IP address: %v", ip) + p.startCoredns() + + if err := p.settings.Daemon.DNS.Set(ip); err != nil { + return fmt.Errorf("error setting daemon dns: %w", err) + } + } + } + p.startDaemon() }