|
244 | 244 |
|
245 | 245 | tee /usr/local/share/docker-init.sh > /dev/null \
|
246 | 246 | << 'EOF'
|
247 |
| -#!/usr/bin/env bash |
| 247 | +#!/bin/sh |
248 | 248 | #-------------------------------------------------------------------------------------------------------------
|
249 | 249 | # Copyright (c) Microsoft Corporation. All rights reserved.
|
250 | 250 | # Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information.
|
251 | 251 | #-------------------------------------------------------------------------------------------------------------
|
252 | 252 |
|
253 |
| -sudoIf() |
254 |
| -{ |
255 |
| - if [ "$(id -u)" -ne 0 ]; then |
256 |
| - sudo "$@" |
257 |
| - else |
258 |
| - "$@" |
259 |
| - fi |
260 |
| -} |
| 253 | +set -e |
261 | 254 |
|
262 |
| -# explicitly remove dockerd and containerd PID file to ensure that it can start properly if it was stopped uncleanly |
263 |
| -# ie: docker kill <ID> |
264 |
| -sudoIf find /run /var/run -iname 'docker*.pid' -delete || : |
265 |
| -sudoIf find /run /var/run -iname 'container*.pid' -delete || : |
| 255 | +dockerd_start() { |
| 256 | + # explicitly remove dockerd and containerd PID file to ensure that it can start properly if it was stopped uncleanly |
| 257 | + # ie: docker kill <ID> |
| 258 | + find /run /var/run -iname 'docker*.pid' -delete || : |
| 259 | + find /run /var/run -iname 'container*.pid' -delete || : |
266 | 260 |
|
267 |
| -set -e |
| 261 | + ## Dind wrapper script from docker team, adapted to a function |
| 262 | + # Maintained: https://github.com/moby/moby/blob/master/hack/dind |
268 | 263 |
|
269 |
| -## Dind wrapper script from docker team |
270 |
| -# Maintained: https://github.com/moby/moby/blob/master/hack/dind |
| 264 | + export container=docker |
271 | 265 |
|
272 |
| -export container=docker |
| 266 | + if [ -d /sys/kernel/security ] && ! mountpoint -q /sys/kernel/security; then |
| 267 | + mount -t securityfs none /sys/kernel/security || { |
| 268 | + echo >&2 'Could not mount /sys/kernel/security.' |
| 269 | + echo >&2 'AppArmor detection and --privileged mode might break.' |
| 270 | + } |
| 271 | + fi |
273 | 272 |
|
274 |
| -if [ -d /sys/kernel/security ] && ! sudoIf mountpoint -q /sys/kernel/security; then |
275 |
| - sudoIf mount -t securityfs none /sys/kernel/security || { |
276 |
| - echo >&2 'Could not mount /sys/kernel/security.' |
277 |
| - echo >&2 'AppArmor detection and --privileged mode might break.' |
278 |
| - } |
279 |
| -fi |
| 273 | + # Mount /tmp (conditionally) |
| 274 | + if ! mountpoint -q /tmp; then |
| 275 | + mount -t tmpfs none /tmp |
| 276 | + fi |
280 | 277 |
|
281 |
| -# Mount /tmp (conditionally) |
282 |
| -if ! sudoIf mountpoint -q /tmp; then |
283 |
| - sudoIf mount -t tmpfs none /tmp |
284 |
| -fi |
| 278 | + # cgroup v2: enable nesting |
| 279 | + if [ -f /sys/fs/cgroup/cgroup.controllers ]; then |
| 280 | + # move the processes from the root group to the /init group, |
| 281 | + # otherwise writing subtree_control fails with EBUSY. |
| 282 | + # An error during moving non-existent process (i.e., "cat") is ignored. |
| 283 | + mkdir -p /sys/fs/cgroup/init |
| 284 | + xargs -rn1 < /sys/fs/cgroup/cgroup.procs > /sys/fs/cgroup/init/cgroup.procs || : |
| 285 | + # enable controllers |
| 286 | + sed -e 's/ / +/g' -e 's/^/+/' < /sys/fs/cgroup/cgroup.controllers \ |
| 287 | + > /sys/fs/cgroup/cgroup.subtree_control |
| 288 | + fi |
| 289 | + ## Dind wrapper over. |
| 290 | +
|
| 291 | + # Handle DNS |
| 292 | + set +e |
| 293 | + cat /etc/resolv.conf | grep -i 'internal.cloudapp.net' |
| 294 | + if [ $? -eq 0 ] |
| 295 | + then |
| 296 | + echo "Setting dockerd Azure DNS." |
| 297 | + CUSTOMDNS="--dns 168.63.129.16" |
| 298 | + else |
| 299 | + echo "Not setting dockerd DNS manually." |
| 300 | + CUSTOMDNS="" |
| 301 | + fi |
| 302 | + set -e |
285 | 303 |
|
286 |
| -# cgroup v2: enable nesting |
287 |
| -if [ -f /sys/fs/cgroup/cgroup.controllers ]; then |
288 |
| - # move the init process (PID 1) from the root group to the /init group, |
289 |
| - # otherwise writing subtree_control fails with EBUSY. |
290 |
| - sudoIf mkdir -p /sys/fs/cgroup/init |
291 |
| - sudoIf echo 1 > /sys/fs/cgroup/init/cgroup.procs |
292 |
| - # enable controllers |
293 |
| - sudoIf sed -e 's/ / +/g' -e 's/^/+/' < /sys/fs/cgroup/cgroup.controllers \ |
294 |
| - > /sys/fs/cgroup/cgroup.subtree_control |
295 |
| -fi |
296 |
| -## Dind wrapper over. |
| 304 | + # Start docker/moby engine |
| 305 | + ( dockerd $CUSTOMDNS > /tmp/dockerd.log 2>&1 ) & |
| 306 | +} |
297 | 307 |
|
298 |
| -# Handle DNS |
299 |
| -set +e |
300 |
| -cat /etc/resolv.conf | grep -i 'internal.cloudapp.net' |
301 |
| -if [ $? -eq 0 ] |
302 |
| -then |
303 |
| - echo "Setting dockerd Azure DNS." |
304 |
| - CUSTOMDNS="--dns 168.63.129.16" |
| 308 | +# Start using sudo if not invoked as root |
| 309 | +if [ "$(id -u)" -ne 0 ]; then |
| 310 | + sudo /bin/sh -c "$(declare -f dockerd_start); dockerd_start" |
305 | 311 | else
|
306 |
| - echo "Not setting dockerd DNS manually." |
307 |
| - CUSTOMDNS="" |
| 312 | + dockerd_start |
308 | 313 | fi
|
309 |
| -set -e |
310 |
| -
|
311 |
| -# Start docker/moby engine |
312 |
| -( sudoIf dockerd $CUSTOMDNS > /tmp/dockerd.log 2>&1 ) & |
313 | 314 |
|
314 | 315 | set +e
|
315 | 316 |
|
|
0 commit comments