Skip to content

Commit

Permalink
docker: move host-gateway-ip config to systemd (#904)
Browse files Browse the repository at this point in the history
* docker: move host-gateway-ip config to systemd

* chore: remove redundant code
  • Loading branch information
abiosoft committed Nov 24, 2023
1 parent f75d4ff commit 95653db
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
50 changes: 36 additions & 14 deletions environment/container/docker/daemon.go
Expand Up @@ -7,6 +7,7 @@ import (
)

const daemonFile = "/etc/docker/daemon.json"
const hostGatewayIPKey = "host-gateway-ip"

func (d dockerRuntime) createDaemonFile(conf map[string]any, env map[string]string) error {
if conf == nil {
Expand All @@ -24,20 +25,9 @@ func (d dockerRuntime) createDaemonFile(conf map[string]any, env map[string]stri
} else if opts, ok := conf["exec-opts"].([]string); ok {
conf["exec-opts"] = append(opts, "native.cgroupdriver=cgroupfs")
}

// get host-gateway ip from the guest
ip, err := d.guest.RunOutput("sh", "-c", "grep 'host.lima.internal' /etc/hosts | awk -F' ' '{print $1}'")
if err != nil {
return fmt.Errorf("error retrieving host gateway IP address: %w", err)
}
if net.ParseIP(ip) == nil {
return fmt.Errorf("invalid host gateway IP address: '%s'", ip)
}

// set host-gateway ip to loopback interface (if not set by user)
if _, ok := conf["host-gateway"]; !ok {
conf["host-gateway-ip"] = ip
}
// remove host-gateway-ip if set by the user
// to avoid clash with systemd configuration
delete(conf, hostGatewayIPKey)

// add proxy vars if set
// according to https://docs.docker.com/config/daemon/systemd/#httphttps-proxy
Expand All @@ -59,3 +49,35 @@ func (d dockerRuntime) createDaemonFile(conf map[string]any, env map[string]stri
}
return d.guest.Write(daemonFile, b)
}

func (d dockerRuntime) addHostGateway(conf map[string]any) error {
// get host-gateway ip from the guest
ip, err := d.guest.RunOutput("sh", "-c", "grep 'host.lima.internal' /etc/hosts | awk -F' ' '{print $1}'")
if err != nil {
return fmt.Errorf("error retrieving host gateway IP address: %w", err)
}
// if set by the user, use the user specified value
if _, ok := conf[hostGatewayIPKey]; ok {
if gip, ok := conf[hostGatewayIPKey].(string); ok {
ip = gip
}
}
if net.ParseIP(ip) == nil {
return fmt.Errorf("invalid host gateway IP address: '%s'", ip)
}

// set host-gateway ip as systemd service file
content := fmt.Sprintf(systemdUnitFileContent, ip)
if err := d.guest.Write(systemdUnitFilename, []byte(content)); err != nil {
return fmt.Errorf("error creating systemd unit file: %w", err)
}

return nil
}

const systemdUnitFilename = "/etc/systemd/system/docker.service.d/docker.conf"
const systemdUnitFileContent string = `
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --host-gateway-ip=%s
`
5 changes: 4 additions & 1 deletion environment/container/docker/docker.go
Expand Up @@ -45,10 +45,13 @@ func (d dockerRuntime) Provision(ctx context.Context) error {

// daemon.json
a.Add(func() error {
// not a fatal error
// these are not fatal errors
if err := d.createDaemonFile(conf.Docker, conf.Env); err != nil {
log.Warnln(err)
}
if err := d.addHostGateway(conf.Docker); err != nil {
log.Warnln(err)
}
return nil
})

Expand Down

0 comments on commit 95653db

Please sign in to comment.