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{,-rootful}.yaml: Use param in docker templates #2515

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
76 changes: 67 additions & 9 deletions examples/docker-rootful.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# $ export DOCKER_HOST=$(limactl list docker-rootful --format 'unix://{{.Dir}}/sock/docker.sock')
# $ docker ...

# This template requires Lima v0.20.0 or later
# This template requires Lima v0.23.0 or later
images:
# Try to use release-yyyyMMdd image if available. Note that release-yyyyMMdd will be removed after several months.
- location: "https://cloud-images.ubuntu.com/releases/24.04/release-20240423/ubuntu-24.04-server-cloudimg-amd64.img"
Expand Down Expand Up @@ -44,16 +44,64 @@ provision:
#!/bin/bash
set -eux -o pipefail
command -v docker >/dev/null 2>&1 && exit 0
if [ ! -e /etc/systemd/system/docker.socket.d/override.conf ]; then
mkdir -p /etc/systemd/system/docker.socket.d
readonly override_conf=/etc/systemd/system/docker.socket.d/override.conf
if [ ! -e "$override_conf" ]; then
mkdir -p $(dirname "$override_conf")
norio-nomura marked this conversation as resolved.
Show resolved Hide resolved
# Alternatively we could just add the user to the "docker" group, but that requires restarting the user session
cat <<-EOF >/etc/systemd/system/docker.socket.d/override.conf
[Socket]
SocketUser={{.User}}
cat <<EOF >"$override_conf"
[Socket]
SocketUser={{.User}}
EOF
fi
export DEBIAN_FRONTEND=noninteractive
curl -fsSL https://get.docker.com | sh
- mode: user # configure docker under non-root user
script: |
#!/bin/bash
set -eux -o pipefail
command -v jq &>/dev/null || sudo apt-get install -y jq
readonly rootless_installed=$(systemctl --user list-unit-files docker.service &>/dev/null && echo true || echo false)
norio-nomura marked this conversation as resolved.
Show resolved Hide resolved

if [ "{{.Param.Rootful}}" = "true" ]; then
norio-nomura marked this conversation as resolved.
Show resolved Hide resolved
readonly config_dir="/etc/docker"
readonly systemctl="sudo systemctl"
readonly tee="sudo tee"

[ "$rootless_installed" != "true" ] || systemctl --user disable --now docker
norio-nomura marked this conversation as resolved.
Show resolved Hide resolved
docker context use default

else
readonly config_dir="$HOME/.config/docker"
readonly systemctl="systemctl --user"
readonly tee="tee"
norio-nomura marked this conversation as resolved.
Show resolved Hide resolved

sudo systemctl disable --now docker
if [ "$rootless_installed" != "true" ]; then
sudo apt-get install -y dbus-user-session fuse3 uidmap
$systemctl start dbus
[ ! -S /var/run/docker.sock ] || sudo rm /var/run/docker.sock
dockerd-rootless-setuptool.sh install
fi
docker context use rootless
fi
$systemctl enable --now docker

readonly config="$config_dir/daemon.json"
needs_restart=
norio-nomura marked this conversation as resolved.
Show resolved Hide resolved
function set_docker_daemon_json() {
function cat_config() { test -s "$config" && cat "$config" || echo "{}" ; }
norio-nomura marked this conversation as resolved.
Show resolved Hide resolved
local -r current=$(cat_config | jq -r "$1 // empty")
[ "$current" = "$2" ] && return 0
mkdir -p "$config_dir" && cat_config | jq "$1 = ${2:-empty}" | (sleep 0 && $tee "$config") && needs_restart=1
}

# enable containerd image store
set_docker_daemon_json '.features."containerd-snapshotter"' "$(
[ "{{.Param.ContainerdImageStore}}" = "true" ] && echo 'true'
)"

# restart docker to apply the new configuration
[ -z "$needs_restart" ] || $systemctl restart docker
probes:
- script: |
#!/bin/bash
Expand All @@ -62,8 +110,15 @@ probes:
echo >&2 "docker is not installed yet"
exit 1
fi
if ! timeout 30s bash -c "until pgrep dockerd; do sleep 3; done"; then
echo >&2 "dockerd is not running"
if [ "{{.Param.Rootful}}" = "true" ]; then
target=dockerd
target_description="dockerd"
norio-nomura marked this conversation as resolved.
Show resolved Hide resolved
else
target=rootlesskit
target_description="rootlesskit (used by rootless docker)"
fi
if ! timeout 30s bash -c "until pgrep $target; do sleep 3; done"; then
echo >&2 "$target_description is not running"
exit 1
fi
hint: See "/var/log/cloud-init-output.log" in the guest
Expand All @@ -73,7 +128,7 @@ hostResolver:
hosts:
host.docker.internal: host.lima.internal
portForwards:
- guestSocket: "/var/run/docker.sock"
- guestSocket: "{{if eq .Param.Rootful \"true\"}}/var/run{{else}}/run/user/{{.UID}}{{end}}/docker.sock"
hostSocket: "{{.Dir}}/sock/docker.sock"
message: |
To run `docker` on the host (assumes docker-cli is installed), run the following commands:
Expand All @@ -82,3 +137,6 @@ message: |
docker context use lima-{{.Name}}
docker run hello-world
------
param:
ContainerdImageStore: false
Rootful: true
78 changes: 67 additions & 11 deletions examples/docker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# $ export DOCKER_HOST=$(limactl list docker --format 'unix://{{.Dir}}/sock/docker.sock')
# $ docker ...

# This template requires Lima v0.8.0 or later
# This template requires Lima v0.23.0 or later
images:
# Try to use release-yyyyMMdd image if available. Note that release-yyyyMMdd will be removed after several months.
- location: "https://cloud-images.ubuntu.com/releases/24.04/release-20240423/ubuntu-24.04-server-cloudimg-amd64.img"
Expand Down Expand Up @@ -44,18 +44,64 @@ provision:
#!/bin/bash
set -eux -o pipefail
command -v docker >/dev/null 2>&1 && exit 0
readonly override_conf=/etc/systemd/system/docker.socket.d/override.conf
if [ ! -e "$override_conf" ]; then
mkdir -p $(dirname "$override_conf")
# Alternatively we could just add the user to the "docker" group, but that requires restarting the user session
cat <<EOF >"$override_conf"
[Socket]
SocketUser={{.User}}
EOF
fi
export DEBIAN_FRONTEND=noninteractive
curl -fsSL https://get.docker.com | sh
# NOTE: you may remove the lines below, if you prefer to use rootful docker, not rootless
systemctl disable --now docker
apt-get install -y uidmap dbus-user-session
- mode: user
- mode: user # configure docker under non-root user
script: |
#!/bin/bash
set -eux -o pipefail
systemctl --user start dbus
dockerd-rootless-setuptool.sh install
docker context use rootless
command -v jq &>/dev/null || sudo apt-get install -y jq
readonly rootless_installed=$(systemctl --user list-unit-files docker.service &>/dev/null && echo true || echo false)

if [ "{{.Param.Rootful}}" = "true" ]; then
readonly config_dir="/etc/docker"
readonly systemctl="sudo systemctl"
readonly tee="sudo tee"

[ "$rootless_installed" != "true" ] || systemctl --user disable --now docker
docker context use default

else
readonly config_dir="$HOME/.config/docker"
readonly systemctl="systemctl --user"
readonly tee="tee"

sudo systemctl disable --now docker
if [ "$rootless_installed" != "true" ]; then
sudo apt-get install -y dbus-user-session fuse3 uidmap
$systemctl start dbus
[ ! -S /var/run/docker.sock ] || sudo rm /var/run/docker.sock
dockerd-rootless-setuptool.sh install
fi
docker context use rootless
fi
$systemctl enable --now docker

readonly config="$config_dir/daemon.json"
needs_restart=
function set_docker_daemon_json() {
function cat_config() { test -s "$config" && cat "$config" || echo "{}" ; }
local -r current=$(cat_config | jq -r "$1 // empty")
[ "$current" = "$2" ] && return 0
mkdir -p "$config_dir" && cat_config | jq "$1 = ${2:-empty}" | (sleep 0 && $tee "$config") && needs_restart=1
}

# enable containerd image store
set_docker_daemon_json '.features."containerd-snapshotter"' "$(
[ "{{.Param.ContainerdImageStore}}" = "true" ] && echo 'true'
)"

# restart docker to apply the new configuration
[ -z "$needs_restart" ] || $systemctl restart docker
probes:
- script: |
#!/bin/bash
Expand All @@ -64,8 +110,15 @@ probes:
echo >&2 "docker is not installed yet"
exit 1
fi
if ! timeout 30s bash -c "until pgrep rootlesskit; do sleep 3; done"; then
echo >&2 "rootlesskit (used by rootless docker) is not running"
if [ "{{.Param.Rootful}}" = "true" ]; then
target=dockerd
target_description="dockerd"
else
target=rootlesskit
target_description="rootlesskit (used by rootless docker)"
fi
if ! timeout 30s bash -c "until pgrep $target; do sleep 3; done"; then
echo >&2 "$target_description is not running"
exit 1
fi
hint: See "/var/log/cloud-init-output.log" in the guest
Expand All @@ -75,7 +128,7 @@ hostResolver:
hosts:
host.docker.internal: host.lima.internal
portForwards:
- guestSocket: "/run/user/{{.UID}}/docker.sock"
- guestSocket: "{{if eq .Param.Rootful \"true\"}}/var/run{{else}}/run/user/{{.UID}}{{end}}/docker.sock"
hostSocket: "{{.Dir}}/sock/docker.sock"
message: |
To run `docker` on the host (assumes docker-cli is installed), run the following commands:
Expand All @@ -84,3 +137,6 @@ message: |
docker context use lima-{{.Name}}
docker run hello-world
------
param:
ContainerdImageStore: false
Rootful: false
Loading