fix: use internal dns for docker if no default is set (#228)

This commit is contained in:
Robert Kaussow 2023-03-24 14:04:29 +01:00 committed by GitHub
parent 6a78c8217c
commit f6ce6cca84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 65 additions and 1 deletions

3
Corefile Normal file
View File

@ -0,0 +1,3 @@
.:53 {
forward . /etc/resolv.conf
}

View File

@ -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"]

40
plugin/coredns.go Normal file
View File

@ -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
}

View File

@ -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()
}