|
| 1 | +#!/bin/sh |
| 2 | +set -eu |
| 3 | + |
| 4 | +_tls_ensure_private() { |
| 5 | + local f="$1"; shift |
| 6 | + [ -s "$f" ] || openssl genrsa -out "$f" 4096 |
| 7 | +} |
| 8 | +_tls_san() { |
| 9 | + { |
| 10 | + ip -oneline address | awk '{ gsub(/\/.+$/, "", $4); print "IP:" $4 }' |
| 11 | + { |
| 12 | + cat /etc/hostname |
| 13 | + echo 'docker' |
| 14 | + echo 'localhost' |
| 15 | + hostname -f |
| 16 | + hostname -s |
| 17 | + } | sed 's/^/DNS:/' |
| 18 | + [ -z "${DOCKER_TLS_SAN:-}" ] || echo "$DOCKER_TLS_SAN" |
| 19 | + } | sort -u | xargs printf '%s,' | sed "s/,\$//" |
| 20 | +} |
| 21 | +_tls_generate_certs() { |
| 22 | + local dir="$1"; shift |
| 23 | + |
| 24 | + # if ca/key.pem || !ca/cert.pem, generate CA public if necessary |
| 25 | + # if ca/key.pem, generate server public |
| 26 | + # if ca/key.pem, generate client public |
| 27 | + # (regenerating public certs every startup to account for SAN/IP changes and/or expiration) |
| 28 | + |
| 29 | + # https://github.com/FiloSottile/mkcert/issues/174 |
| 30 | + local certValidDays='825' |
| 31 | + |
| 32 | + if [ -s "$dir/ca/key.pem" ] || [ ! -s "$dir/ca/cert.pem" ]; then |
| 33 | + # if we either have a CA private key or do *not* have a CA public key, then we should create/manage the CA |
| 34 | + mkdir -p "$dir/ca" |
| 35 | + _tls_ensure_private "$dir/ca/key.pem" |
| 36 | + openssl req -new -key "$dir/ca/key.pem" \ |
| 37 | + -out "$dir/ca/cert.pem" \ |
| 38 | + -subj '/CN=docker:dind CA' -x509 -days "$certValidDays" |
| 39 | + fi |
| 40 | + |
| 41 | + if [ -s "$dir/ca/key.pem" ]; then |
| 42 | + # if we have a CA private key, we should create/manage a server key |
| 43 | + mkdir -p "$dir/server" |
| 44 | + _tls_ensure_private "$dir/server/key.pem" |
| 45 | + openssl req -new -key "$dir/server/key.pem" \ |
| 46 | + -out "$dir/server/csr.pem" \ |
| 47 | + -subj '/CN=docker:dind server' |
| 48 | + cat > "$dir/server/openssl.cnf" <<-EOF |
| 49 | + [ x509_exts ] |
| 50 | + subjectAltName = $(_tls_san) |
| 51 | + EOF |
| 52 | + openssl x509 -req \ |
| 53 | + -in "$dir/server/csr.pem" \ |
| 54 | + -CA "$dir/ca/cert.pem" \ |
| 55 | + -CAkey "$dir/ca/key.pem" \ |
| 56 | + -CAcreateserial \ |
| 57 | + -out "$dir/server/cert.pem" \ |
| 58 | + -days "$certValidDays" \ |
| 59 | + -extfile "$dir/server/openssl.cnf" \ |
| 60 | + -extensions x509_exts |
| 61 | + cp "$dir/ca/cert.pem" "$dir/server/ca.pem" |
| 62 | + openssl verify -CAfile "$dir/server/ca.pem" "$dir/server/cert.pem" |
| 63 | + fi |
| 64 | + |
| 65 | + if [ -s "$dir/ca/key.pem" ]; then |
| 66 | + # if we have a CA private key, we should create/manage a client key |
| 67 | + mkdir -p "$dir/client" |
| 68 | + _tls_ensure_private "$dir/client/key.pem" |
| 69 | + chmod 0644 "$dir/client/key.pem" # openssl defaults to 0600 for the private key, but this one needs to be shared with arbitrary client contexts |
| 70 | + openssl req -new \ |
| 71 | + -key "$dir/client/key.pem" \ |
| 72 | + -out "$dir/client/csr.pem" \ |
| 73 | + -subj '/CN=docker:dind client' |
| 74 | + cat > "$dir/client/openssl.cnf" <<-'EOF' |
| 75 | + [ x509_exts ] |
| 76 | + extendedKeyUsage = clientAuth |
| 77 | + EOF |
| 78 | + openssl x509 -req \ |
| 79 | + -in "$dir/client/csr.pem" \ |
| 80 | + -CA "$dir/ca/cert.pem" \ |
| 81 | + -CAkey "$dir/ca/key.pem" \ |
| 82 | + -CAcreateserial \ |
| 83 | + -out "$dir/client/cert.pem" \ |
| 84 | + -days "$certValidDays" \ |
| 85 | + -extfile "$dir/client/openssl.cnf" \ |
| 86 | + -extensions x509_exts |
| 87 | + cp "$dir/ca/cert.pem" "$dir/client/ca.pem" |
| 88 | + openssl verify -CAfile "$dir/client/ca.pem" "$dir/client/cert.pem" |
| 89 | + fi |
| 90 | +} |
| 91 | + |
| 92 | +# no arguments passed |
| 93 | +# or first arg is `-f` or `--some-option` |
| 94 | +if [ "$#" -eq 0 ] || [ "${1#-}" != "$1" ]; then |
| 95 | + # set "dockerSocket" to the default "--host" *unix socket* value (for both standard or rootless) |
| 96 | + uid="$(id -u)" |
| 97 | + if [ "$uid" = '0' ]; then |
| 98 | + dockerSocket='unix:///var/run/docker.sock' |
| 99 | + else |
| 100 | + # if we're not root, we must be trying to run rootless |
| 101 | + : "${XDG_RUNTIME_DIR:=/run/user/$uid}" |
| 102 | + dockerSocket="unix://$XDG_RUNTIME_DIR/docker.sock" |
| 103 | + fi |
| 104 | + case "${DOCKER_HOST:-}" in |
| 105 | + unix://*) |
| 106 | + dockerSocket="$DOCKER_HOST" |
| 107 | + ;; |
| 108 | + esac |
| 109 | + |
| 110 | + # add our default arguments |
| 111 | + if [ -n "${DOCKER_TLS_CERTDIR:-}" ] \ |
| 112 | + && _tls_generate_certs "$DOCKER_TLS_CERTDIR" \ |
| 113 | + && [ -s "$DOCKER_TLS_CERTDIR/server/ca.pem" ] \ |
| 114 | + && [ -s "$DOCKER_TLS_CERTDIR/server/cert.pem" ] \ |
| 115 | + && [ -s "$DOCKER_TLS_CERTDIR/server/key.pem" ] \ |
| 116 | + ; then |
| 117 | + # generate certs and use TLS if requested/possible (default in 19.03+) |
| 118 | + set -- dockerd \ |
| 119 | + --host="$dockerSocket" \ |
| 120 | + --host=tcp://0.0.0.0:2376 \ |
| 121 | + --tlsverify \ |
| 122 | + --tlscacert "$DOCKER_TLS_CERTDIR/server/ca.pem" \ |
| 123 | + --tlscert "$DOCKER_TLS_CERTDIR/server/cert.pem" \ |
| 124 | + --tlskey "$DOCKER_TLS_CERTDIR/server/key.pem" \ |
| 125 | + "$@" |
| 126 | + DOCKERD_ROOTLESS_ROOTLESSKIT_FLAGS="${DOCKERD_ROOTLESS_ROOTLESSKIT_FLAGS:-} -p 0.0.0.0:2376:2376/tcp" |
| 127 | + else |
| 128 | + # TLS disabled (-e DOCKER_TLS_CERTDIR='') or missing certs |
| 129 | + set -- dockerd \ |
| 130 | + --host="$dockerSocket" \ |
| 131 | + --host=tcp://0.0.0.0:2375 \ |
| 132 | + "$@" |
| 133 | + DOCKERD_ROOTLESS_ROOTLESSKIT_FLAGS="${DOCKERD_ROOTLESS_ROOTLESSKIT_FLAGS:-} -p 0.0.0.0:2375:2375/tcp" |
| 134 | + fi |
| 135 | +fi |
| 136 | + |
| 137 | +if [ "$1" = 'dockerd' ]; then |
| 138 | + # explicitly remove Docker's default PID file to ensure that it can start properly if it was stopped uncleanly (and thus didn't clean up the PID file) |
| 139 | + find /run /var/run -iname 'docker*.pid' -delete || : |
| 140 | + |
| 141 | + uid="$(id -u)" |
| 142 | + if [ "$uid" != '0' ]; then |
| 143 | + # if we're not root, we must be trying to run rootless |
| 144 | + if ! command -v rootlesskit > /dev/null; then |
| 145 | + echo >&2 "error: attempting to run rootless dockerd but missing 'rootlesskit' (perhaps the 'docker:dind-rootless' image variant is intended?)" |
| 146 | + exit 1 |
| 147 | + fi |
| 148 | + user="$(id -un 2>/dev/null || :)" |
| 149 | + if ! grep -qE "^($uid${user:+|$user}):" /etc/subuid || ! grep -qE "^($uid${user:+|$user}):" /etc/subgid; then |
| 150 | + echo >&2 "error: attempting to run rootless dockerd but missing necessary entries in /etc/subuid and/or /etc/subgid for $uid" |
| 151 | + exit 1 |
| 152 | + fi |
| 153 | + : "${XDG_RUNTIME_DIR:=/run/user/$uid}" |
| 154 | + export XDG_RUNTIME_DIR |
| 155 | + if ! mkdir -p "$XDG_RUNTIME_DIR" || [ ! -w "$XDG_RUNTIME_DIR" ] || ! mkdir -p "$HOME/.local/share/docker" || [ ! -w "$HOME/.local/share/docker" ]; then |
| 156 | + echo >&2 "error: attempting to run rootless dockerd but need writable HOME ($HOME) and XDG_RUNTIME_DIR ($XDG_RUNTIME_DIR) for user $uid" |
| 157 | + exit 1 |
| 158 | + fi |
| 159 | + if [ -f /proc/sys/kernel/unprivileged_userns_clone ] && unprivClone="$(cat /proc/sys/kernel/unprivileged_userns_clone)" && [ "$unprivClone" != '1' ]; then |
| 160 | + echo >&2 "error: attempting to run rootless dockerd but need 'kernel.unprivileged_userns_clone' (/proc/sys/kernel/unprivileged_userns_clone) set to 1" |
| 161 | + exit 1 |
| 162 | + fi |
| 163 | + if [ -f /proc/sys/user/max_user_namespaces ] && maxUserns="$(cat /proc/sys/user/max_user_namespaces)" && [ "$maxUserns" = '0' ]; then |
| 164 | + echo >&2 "error: attempting to run rootless dockerd but need 'user.max_user_namespaces' (/proc/sys/user/max_user_namespaces) set to a sufficiently large value" |
| 165 | + exit 1 |
| 166 | + fi |
| 167 | + # TODO overlay support detection? |
| 168 | + exec rootlesskit \ |
| 169 | + --net="${DOCKERD_ROOTLESS_ROOTLESSKIT_NET:-vpnkit}" \ |
| 170 | + --mtu="${DOCKERD_ROOTLESS_ROOTLESSKIT_MTU:-1500}" \ |
| 171 | + --disable-host-loopback \ |
| 172 | + --port-driver=builtin \ |
| 173 | + --copy-up=/etc \ |
| 174 | + --copy-up=/run \ |
| 175 | + ${DOCKERD_ROOTLESS_ROOTLESSKIT_FLAGS:-} \ |
| 176 | + "$@" |
| 177 | + elif [ -x '/usr/local/bin/dind' ]; then |
| 178 | + # if we have the (mostly defunct now) Docker-in-Docker wrapper script, use it |
| 179 | + set -- '/usr/local/bin/dind' "$@" |
| 180 | + fi |
| 181 | +else |
| 182 | + # if it isn't `dockerd` we're trying to run, pass it through `docker-entrypoint.sh` so it gets `DOCKER_HOST` set appropriately too |
| 183 | + set -- docker-entrypoint.sh "$@" |
| 184 | +fi |
| 185 | + |
| 186 | +exec "$@" |
0 commit comments