gokrazy’s goal is to make it easy to build Go appliances. In an ideal world, all
building blocks you need would be available in Go. In reality, that is not
entirely the case. Perhaps you need to run a C program next to your Go
programs. Docker containers make incremental (or partial) adoption of gokrazy
easy.
We’re going to use podman, a drop-in replacement for Docker, because there is a
statically compiled version for amd64 and arm64 available that we could easily
re-package into https://github.com/gokrazy/podman.
Step 1: Install podman to your gokrazy device
Include the following packages in your gokrazy installation:
Now that you have the required tools, there are a couple of decisions you have
to make depending on what you want to run in your container(s):
Should container data be stored ephemerally in tmpfs (lost with the next
reboot), on the permanent partition of your SD card, or somewhere else
entirely (e.g. network storage)?
Do you want to pull new container versions automatically before each run, or
manually on demand only?
Should your container be started as a one-off job only (→ detached
mode), or
supervised continuously (restarted when it exits)?
Should your container use a deterministic name (so that you can exec
commands in it easily), or use a fresh name for each run (so that there never
are conflicts)?
Aside from these broad questions, you very likely need to set a bunch of detail
options for your container, such as additional environment variables, volume
mounts, networking flags, or command line arguments.
The following program is an example for how this could look like. I use this
program to run irssi.
packagemainimport (
"fmt""log""os""os/exec""strings""github.com/gokrazy/gokrazy")
funcpodman(args...string) error {
podman:=exec.Command("/usr/local/bin/podman", args...)
podman.Env = expandPath(os.Environ())
podman.Env = append(podman.Env, "TMPDIR=/tmp")
podman.Stdin = os.Stdinpodman.Stdout = os.Stdoutpodman.Stderr = os.Stderriferr:=podman.Run(); err!=nil {
returnfmt.Errorf("%v: %v", podman.Args, err)
}
returnnil}
funcirssi() error {
// Ensure we have an up-to-date clock, which in turn also means that// networking is up. This is relevant because podman takes what’s in// /etc/resolv.conf (nothing at boot) and holds on to it, meaning your// container will never have working networking if it starts too early.gokrazy.WaitForClock()
iferr:=podman("kill", "irssi"); err!=nil {
log.Print(err)
}
iferr:=podman("rm", "irssi"); err!=nil {
log.Print(err)
}
// You could podman pull here.iferr:=podman("run",
"-td",
"-v", "/perm/irssi:/home/michael/.irssi",
"-v", "/perm/irclogs:/home/michael/irclogs",
"-e", "TERM=rxvt-unicode",
"-e", "LANG=C.UTF-8",
"--network", "host",
"--name", "irssi",
"docker.io/stapelberg/irssi:latest",
"screen", "-S", "irssi", "irssi"); err!=nil {
returnerr }
returnnil}
funcmain() {
iferr:=irssi(); err!=nil {
log.Fatal(err)
}
}
// expandPath returns env, but with PATH= modified or added// such that both /user and /usr/local/bin are included, which podman needs.funcexpandPath(env []string) []string {
extra:="/user:/usr/local/bin"found:=falseforidx, val:=rangeenv {
parts:=strings.Split(val, "=")
if len(parts) < 2 {
continue// malformed entry }
key:=parts[0]
ifkey!="PATH" {
continue }
val:=strings.Join(parts[1:], "=")
env[idx] = fmt.Sprintf("%s=%s:%s", key, extra, val)
found = true }
if !found {
constbusyboxDefaultPATH = "/usr/local/sbin:/sbin:/usr/sbin:/usr/local/bin:/bin:/usr/bin"env = append(env, fmt.Sprintf("PATH=%s:%s", extra, busyboxDefaultPATH))
}
returnenv}