@@ -45,10 +45,10 @@ provision:
45
45
set -eux -o pipefail
46
46
command -v docker >/dev/null 2>&1 && exit 0
47
47
readonly override_conf=/etc/systemd/system/docker.socket.d/override.conf
48
- if [ ! -e " $override_conf" ]; then
49
- mkdir -p $(dirname " $override_conf" )
48
+ if [ ! -e $override_conf ]; then
49
+ mkdir -p $(dirname $override_conf)
50
50
# Alternatively we could just add the user to the "docker" group, but that requires restarting the user session
51
- cat <<EOF >" $override_conf"
51
+ cat <<EOF >$override_conf
52
52
[Socket]
53
53
SocketUser={{.User}}
54
54
EOF
@@ -58,50 +58,81 @@ provision:
58
58
- mode : user # configure docker under non-root user
59
59
script : |
60
60
#!/bin/bash
61
- set -eux -o pipefail
62
- command -v jq &>/dev/null || sudo apt-get install -y jq
63
- readonly rootless_installed=$(systemctl --user list-unit-files docker.service &>/dev/null && echo true || echo false)
61
+ set -o errexit -o nounset -o pipefail -o xtrace
64
62
65
- if [ "{{.Param.Rootful}}" = "true" ]; then
66
- readonly config_dir="/etc/docker"
67
- readonly systemctl="sudo systemctl"
68
- readonly tee="sudo tee"
63
+ if ! command -v jq &>/dev/null; then
64
+ sudo apt-get install --assume-yes jq
65
+ fi
66
+ if systemctl --user list-unit-files docker.service &>/dev/null; then
67
+ readonly rootless_installed=true
68
+ else
69
+ readonly rootless_installed=false
70
+ fi
69
71
70
- [ "$rootless_installed" != "true" ] || systemctl --user disable --now docker
71
- docker context use default
72
+ # Setting shell variable makes it easier to read cloud-init-output.log
73
+ readonly ROOTFUL="{{.Param.ROOTFUL}}"
74
+ if [ "$ROOTFUL" = true ]; then
75
+ if [ $rootless_installed = true ]; then
76
+ systemctl --user disable --now docker
77
+ fi
72
78
79
+ readonly config_dir=/etc/docker
80
+ readonly context=default
81
+ function systemctl_wrapper() { sudo systemctl "$@"; }
82
+ function tee_wrapper() { sudo tee "$@"; }
73
83
else
74
- readonly config_dir="$HOME/.config/docker"
75
- readonly systemctl="systemctl --user"
76
- readonly tee="tee"
77
-
78
84
sudo systemctl disable --now docker
79
- if [ "$rootless_installed" != "true" ]; then
80
- sudo apt-get install -y dbus-user-session fuse3 uidmap
81
- $systemctl start dbus
82
- [ ! -S /var/run/docker.sock ] || sudo rm /var/run/docker.sock
85
+ if [ $rootless_installed != true ]; then
86
+ sudo apt-get install --assume-yes dbus-user-session fuse3 uidmap
87
+ if [ -S /var/run/docker.sock ]; then
88
+ sudo rm /var/run/docker.sock
89
+ fi
83
90
dockerd-rootless-setuptool.sh install
84
91
fi
85
- docker context use rootless
92
+
93
+ readonly config_dir="$HOME/.config/docker"
94
+ readonly context=rootless
95
+ function systemctl_wrapper() { systemctl --user "$@"; }
96
+ function tee_wrapper() { tee "$@"; }
86
97
fi
87
- $systemctl enable --now docker
98
+
99
+ systemctl_wrapper enable --now docker
100
+ docker context use $context
88
101
89
102
readonly config="$config_dir/daemon.json"
90
- needs_restart=
103
+ function print_config() {
104
+ if [ -s "$config" ]; then
105
+ cat "$config"
106
+ else
107
+ # print empty JSON object instead of empty string for jq to work
108
+ echo "{}"
109
+ fi
110
+ }
111
+ needs_restart=false
91
112
function set_docker_daemon_json() {
92
- function cat_config() { test -s "$config" && cat "$config" || echo "{}" ; }
93
- local -r current=$(cat_config | jq -r "$1 // empty")
113
+ local -r current=$(print_config | jq --raw-output "$1 // empty")
94
114
[ "$current" = "$2" ] && return 0
95
- mkdir -p "$config_dir" && cat_config | jq "$1 = ${2:-empty}" | (sleep 0 && $tee "$config") && needs_restart=1
115
+ mkdir -p "$config_dir"
116
+ # sleep 0 is a trick to avoid tee_wrapper overwriting the file before reading it
117
+ if print_config | jq "$1 = ${2:-empty}" | (sleep 0 && tee_wrapper "$config"); then
118
+ needs_restart=true
119
+ fi
96
120
}
97
121
122
+ # Setting shell variable makes it easier to read cloud-init-output.log
123
+ readonly CONTAINERD_IMAGE_STORE="{{.Param.CONTAINERD_IMAGE_STORE}}"
98
124
# enable containerd image store
99
- set_docker_daemon_json '.features."containerd-snapshotter"' "$(
100
- [ "{{.Param.ContainerdImageStore}}" = "true" ] && echo 'true'
101
- )"
125
+ if [ "$CONTAINERD_IMAGE_STORE" = true ]; then
126
+ set_docker_daemon_json '.features."containerd-snapshotter"' 'true'
127
+ else
128
+ # passing empty string to remove the key and use the default value
129
+ set_docker_daemon_json '.features."containerd-snapshotter"' ''
130
+ fi
102
131
103
132
# restart docker to apply the new configuration
104
- [ -z "$needs_restart" ] || $systemctl restart docker
133
+ if [ $needs_restart = true ]; then
134
+ systemctl_wrapper restart docker
135
+ fi
105
136
probes :
106
137
- script : |
107
138
#!/bin/bash
@@ -110,9 +141,10 @@ probes:
110
141
echo >&2 "docker is not installed yet"
111
142
exit 1
112
143
fi
113
- if [ "{{.Param.Rootful}}" = "true" ]; then
144
+ readonly ROOTFUL="{{.Param.ROOTFUL}}"
145
+ if [ "$ROOTFUL" = true ]; then
114
146
target=dockerd
115
- target_description=" dockerd"
147
+ target_description=dockerd
116
148
else
117
149
target=rootlesskit
118
150
target_description="rootlesskit (used by rootless docker)"
@@ -128,7 +160,7 @@ hostResolver:
128
160
hosts :
129
161
host.docker.internal : host.lima.internal
130
162
portForwards :
131
- - guestSocket : " {{if eq .Param.Rootful \" true\" }}/var/run{{else}}/run/user/{{.UID}}{{end}}/docker.sock"
163
+ - guestSocket : " {{if eq .Param.ROOTFUL \" true\" }}/var/run{{else}}/run/user/{{.UID}}{{end}}/docker.sock"
132
164
hostSocket : " {{.Dir}}/sock/docker.sock"
133
165
message : |
134
166
To run `docker` on the host (assumes docker-cli is installed), run the following commands:
@@ -138,5 +170,5 @@ message: |
138
170
docker run hello-world
139
171
------
140
172
param :
141
- ContainerdImageStore : false
142
- Rootful : true
173
+ CONTAINERD_IMAGE_STORE : false
174
+ ROOTFUL : true
0 commit comments