Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docker: move host-gateway-ip config to systemd #904

Merged
merged 2 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 36 additions & 14 deletions environment/container/docker/daemon.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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