Add internal DNS server if no custom DNS is set (#45)

Fixes #44

Co-authored-by: Lauris BH <lauris@nix.lv>
Reviewed-on: https://codeberg.org/woodpecker-plugins/plugin-docker-buildx/pulls/45
Reviewed-by: 6543 <6543@obermui.de>
Co-authored-by: Lauris BH <lafriks@noreply.codeberg.org>
Co-committed-by: Lauris BH <lafriks@noreply.codeberg.org>
This commit is contained in:
Lauris BH 2022-11-23 14:01:45 +00:00 committed by 6543
parent 8a70e48182
commit a2d26878e7
4 changed files with 65 additions and 0 deletions

3
Corefile Normal file
View File

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

View File

@ -15,6 +15,9 @@ RUN --mount=type=cache,target=/root/.cache/go-build \
FROM docker/buildx-bin:${BUILDX_VERSION} as buildx-bin FROM docker/buildx-bin:${BUILDX_VERSION} as buildx-bin
FROM docker:${DOCKER_VERSION} 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=buildx-bin /buildx /usr/libexec/docker/cli-plugins/docker-buildx
COPY --from=build /src/plugin-docker-buildx /bin/plugin-docker-buildx COPY --from=build /src/plugin-docker-buildx /bin/plugin-docker-buildx

48
plugin/coredns.go Normal file
View File

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

View File

@ -209,6 +209,17 @@ func (p *Plugin) writeBuildkitConfig() error {
func (p *Plugin) Execute() error { func (p *Plugin) Execute() error {
// start the Docker daemon server // start the Docker daemon server
if !p.settings.Daemon.Disabled { 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() p.startDaemon()
} }