Skip to content
This repository was archived by the owner on Aug 29, 2023. It is now read-only.

Commit f6ce6cc

Browse files
authored
fix: use internal dns for docker if no default is set (#228)
1 parent 6a78c82 commit f6ce6cc

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

Corefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.:53 {
2+
forward . /etc/resolv.conf
3+
}

Dockerfile.multiarch

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ ENV BUILDX_VERSION="${BUILDX_VERSION:-v0.10.4}"
2727
ENV DOCKER_HOST=unix:///var/run/docker.sock
2828

2929
RUN apk --update add --virtual .build-deps curl && \
30-
apk --update add --no-cache git && \
30+
apk --update add --no-cache git coredns && \
3131
mkdir -p /usr/lib/docker/cli-plugins/ && \
3232
curl -SsL -o /usr/lib/docker/cli-plugins/docker-buildx \
3333
"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 && \
3636
rm -rf /var/cache/apk/* && \
3737
rm -rf /tmp/*
3838

39+
COPY --from=build /src/Corefile /etc/coredns/Corefile
3940
COPY --from=build /src/dist/drone-docker-buildx /bin/drone-docker-buildx
4041
ENTRYPOINT ["/usr/local/bin/dockerd-entrypoint.sh", "drone-docker-buildx"]

plugin/coredns.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package plugin
2+
3+
import (
4+
"io"
5+
"net"
6+
"os"
7+
"os/exec"
8+
)
9+
10+
func (p Plugin) startCoredns() {
11+
cmd := exec.Command("coredns", "-conf", "/etc/coredns/Corefile")
12+
if p.settings.Daemon.Debug {
13+
cmd.Stdout = os.Stdout
14+
cmd.Stderr = os.Stderr
15+
} else {
16+
cmd.Stdout = io.Discard
17+
cmd.Stderr = io.Discard
18+
}
19+
20+
go func() {
21+
trace(cmd)
22+
_ = cmd.Run()
23+
}()
24+
}
25+
26+
func getContainerIP() (string, error) {
27+
netInterfaceAddrList, err := net.InterfaceAddrs()
28+
if err != nil {
29+
return "", err
30+
}
31+
32+
for _, netInterfaceAddr := range netInterfaceAddrList {
33+
netIP, ok := netInterfaceAddr.(*net.IPNet)
34+
if ok && !netIP.IP.IsLoopback() && netIP.IP.To4() != nil {
35+
return netIP.IP.String(), nil
36+
}
37+
}
38+
39+
return "", nil
40+
}

plugin/impl.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,29 @@ func (p *Plugin) Validate() error {
111111
}
112112

113113
// Execute provides the implementation of the plugin.
114+
//
115+
//nolint:gocognit
114116
func (p *Plugin) Execute() error {
115117
// start the Docker daemon server
118+
//nolint: nestif
116119
if !p.settings.Daemon.Disabled {
120+
// If no custom DNS value set start internal DNS server
121+
if len(p.settings.Daemon.DNS.Value()) == 0 {
122+
ip, err := getContainerIP()
123+
if err != nil {
124+
logrus.Warnf("error detecting IP address: %v", err)
125+
}
126+
127+
if ip != "" {
128+
logrus.Debugf("discovered IP address: %v", ip)
129+
p.startCoredns()
130+
131+
if err := p.settings.Daemon.DNS.Set(ip); err != nil {
132+
return fmt.Errorf("error setting daemon dns: %w", err)
133+
}
134+
}
135+
}
136+
117137
p.startDaemon()
118138
}
119139

0 commit comments

Comments
 (0)