|
| 1 | +#!/usr/bin/env bash |
| 2 | +#------------------------------------------------------------------------------------------------------------- |
| 3 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 4 | +# Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information. |
| 5 | +#------------------------------------------------------------------------------------------------------------- |
| 6 | +# |
| 7 | +# Docs: https://github.com/microsoft/vscode-dev-containers/blob/master/script-library/docs/docker-in-docker.md |
| 8 | +# Maintainer: The VS Code and Codespaces Teams |
| 9 | +# |
| 10 | +# Syntax: ./docker-in-docker-debian.sh [enable non-root docker access flag] [non-root user] [use moby] |
| 11 | + |
| 12 | +ENABLE_NONROOT_DOCKER=${1:-"true"} |
| 13 | +USERNAME=${2:-"automatic"} |
| 14 | +USE_MOBY=${3:-"true"} |
| 15 | + |
| 16 | +set -e |
| 17 | + |
| 18 | +if [ "$(id -u)" -ne 0 ]; then |
| 19 | + echo -e 'Script must be run as root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.' |
| 20 | + exit 1 |
| 21 | +fi |
| 22 | + |
| 23 | +# Determine the appropriate non-root user |
| 24 | +if [ "${USERNAME}" = "auto" ] || [ "${USERNAME}" = "automatic" ]; then |
| 25 | + USERNAME="" |
| 26 | + POSSIBLE_USERS=("vscode" "node" "codespace" "$(awk -v val=1000 -F ":" '$3==val{print $1}' /etc/passwd)") |
| 27 | + for CURRENT_USER in ${POSSIBLE_USERS[@]}; do |
| 28 | + if id -u ${CURRENT_USER} > /dev/null 2>&1; then |
| 29 | + USERNAME=${CURRENT_USER} |
| 30 | + break |
| 31 | + fi |
| 32 | + done |
| 33 | + if [ "${USERNAME}" = "" ]; then |
| 34 | + USERNAME=root |
| 35 | + fi |
| 36 | +elif [ "${USERNAME}" = "none" ] || ! id -u ${USERNAME} > /dev/null 2>&1; then |
| 37 | + USERNAME=root |
| 38 | +fi |
| 39 | + |
| 40 | +# Function to run apt-get if needed |
| 41 | +apt-get-update-if-needed() |
| 42 | +{ |
| 43 | + if [ ! -d "/var/lib/apt/lists" ] || [ "$(ls /var/lib/apt/lists/ | wc -l)" = "0" ]; then |
| 44 | + echo "Running apt-get update..." |
| 45 | + apt-get update |
| 46 | + else |
| 47 | + echo "Skipping apt-get update." |
| 48 | + fi |
| 49 | +} |
| 50 | + |
| 51 | +# Ensure apt is in non-interactive to avoid prompts |
| 52 | +export DEBIAN_FRONTEND=noninteractive |
| 53 | + |
| 54 | +# Install docker/dockerd dependencies if missing |
| 55 | +if ! dpkg -s apt-transport-https curl ca-certificates lsb-release lxc pigz iptables > /dev/null 2>&1 || ! type gpg > /dev/null 2>&1; then |
| 56 | + apt-get-update-if-needed |
| 57 | + apt-get -y install --no-install-recommends apt-transport-https curl ca-certificates lsb-release lxc pigz iptables gnupg2 |
| 58 | +fi |
| 59 | + |
| 60 | +# Swap to legacy iptables for compatibility |
| 61 | +update-alternatives --set iptables /usr/sbin/iptables-legacy |
| 62 | +update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy |
| 63 | + |
| 64 | +# Install Docker / Moby CLI if not already installed |
| 65 | +if type docker > /dev/null 2>&1 && type dockerd > /dev/null 2>&1; then |
| 66 | + echo "Docker / Moby CLI and Engine already installed." |
| 67 | +else |
| 68 | + if [ "${USE_MOBY}" = "true" ]; then |
| 69 | + DISTRO=$(lsb_release -is | tr '[:upper:]' '[:lower:]') |
| 70 | + CODENAME=$(lsb_release -cs) |
| 71 | + curl -s https://packages.microsoft.com/keys/microsoft.asc | (OUT=$(apt-key add - 2>&1) || echo $OUT) |
| 72 | + echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-${DISTRO}-${CODENAME}-prod ${CODENAME} main" > /etc/apt/sources.list.d/microsoft.list |
| 73 | + apt-get update |
| 74 | + apt-get -y install --no-install-recommends moby-cli moby-engine |
| 75 | + else |
| 76 | + curl -fsSL https://download.docker.com/linux/$(lsb_release -is | tr '[:upper:]' '[:lower:]')/gpg | (OUT=$(apt-key add - 2>&1) || echo $OUT) |
| 77 | + echo "deb [arch=amd64] https://download.docker.com/linux/$(lsb_release -is | tr '[:upper:]' '[:lower:]') $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list |
| 78 | + apt-get update |
| 79 | + apt-get -y install --no-install-recommends docker-ce-cli docker-ce |
| 80 | + fi |
| 81 | +fi |
| 82 | + |
| 83 | +echo "Finished installing docker / moby" |
| 84 | + |
| 85 | +# Install Docker Compose if not already installed |
| 86 | +if type docker-compose > /dev/null 2>&1; then |
| 87 | + echo "Docker Compose already installed." |
| 88 | +else |
| 89 | + LATEST_COMPOSE_VERSION=$(curl -sSL "https://api.github.com/repos/docker/compose/releases/latest" | grep -o -P '(?<="tag_name": ").+(?=")') |
| 90 | + curl -sSL "https://github.com/docker/compose/releases/download/${LATEST_COMPOSE_VERSION}/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose |
| 91 | + chmod +x /usr/local/bin/docker-compose |
| 92 | +fi |
| 93 | + |
| 94 | +# If init file already exists, exit |
| 95 | +if [ -f "/usr/local/share/docker-init.sh" ]; then |
| 96 | + echo "/usr/local/share/docker-init.sh already exists, so exiting." |
| 97 | + exit 0 |
| 98 | +fi |
| 99 | +echo "docker-init doesnt exist..." |
| 100 | + |
| 101 | +# Add user to the docker group |
| 102 | +if [ "${ENABLE_NONROOT_DOCKER}" = "true" ]; then |
| 103 | + if ! getent group docker > /dev/null 2>&1; then |
| 104 | + groupadd docker |
| 105 | + fi |
| 106 | + usermod -aG docker ${USERNAME} |
| 107 | +fi |
| 108 | + |
| 109 | +tee /usr/local/share/docker-init.sh > /dev/null \ |
| 110 | +<< 'EOF' |
| 111 | +#!/usr/bin/env bash |
| 112 | +#------------------------------------------------------------------------------------------------------------- |
| 113 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 114 | +# Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information. |
| 115 | +#------------------------------------------------------------------------------------------------------------- |
| 116 | +
|
| 117 | +sudoIf() |
| 118 | +{ |
| 119 | + if [ "$(id -u)" -ne 0 ]; then |
| 120 | + sudo "$@" |
| 121 | + else |
| 122 | + "$@" |
| 123 | + fi |
| 124 | +} |
| 125 | +
|
| 126 | +# explicitly remove dockerd and containerd PID file to ensure that it can start properly if it was stopped uncleanly |
| 127 | +# ie: docker kill <ID> |
| 128 | +sudoIf find /run /var/run -iname 'docker*.pid' -delete || : |
| 129 | +sudoIf find /run /var/run -iname 'container*.pid' -delete || : |
| 130 | +
|
| 131 | +set -e |
| 132 | +
|
| 133 | +## Dind wrapper script from docker team |
| 134 | +# Maintained: https://github.com/moby/moby/blob/master/hack/dind |
| 135 | +
|
| 136 | +export container=docker |
| 137 | +
|
| 138 | +if [ -d /sys/kernel/security ] && ! sudoIf mountpoint -q /sys/kernel/security; then |
| 139 | + sudoIf mount -t securityfs none /sys/kernel/security || { |
| 140 | + echo >&2 'Could not mount /sys/kernel/security.' |
| 141 | + echo >&2 'AppArmor detection and --privileged mode might break.' |
| 142 | + } |
| 143 | +fi |
| 144 | +
|
| 145 | +# Mount /tmp (conditionally) |
| 146 | +if ! sudoIf mountpoint -q /tmp; then |
| 147 | + sudoIf mount -t tmpfs none /tmp |
| 148 | +fi |
| 149 | +
|
| 150 | +# cgroup v2: enable nesting |
| 151 | +if [ -f /sys/fs/cgroup/cgroup.controllers ]; then |
| 152 | + # move the init process (PID 1) from the root group to the /init group, |
| 153 | + # otherwise writing subtree_control fails with EBUSY. |
| 154 | + sudoIf mkdir -p /sys/fs/cgroup/init |
| 155 | + sudoIf echo 1 > /sys/fs/cgroup/init/cgroup.procs |
| 156 | + # enable controllers |
| 157 | + sudoIf sed -e 's/ / +/g' -e 's/^/+/' < /sys/fs/cgroup/cgroup.controllers \ |
| 158 | + > /sys/fs/cgroup/cgroup.subtree_control |
| 159 | +fi |
| 160 | +## Dind wrapper over. |
| 161 | +
|
| 162 | +# Handle DNS |
| 163 | +set +e |
| 164 | +cat /etc/resolv.conf | grep -i 'internal.cloudapp.net' |
| 165 | +if [ $? -eq 0 ] |
| 166 | +then |
| 167 | + echo "Setting dockerd Azure DNS." |
| 168 | + CUSTOMDNS="--dns 168.63.129.16" |
| 169 | +else |
| 170 | + echo "Not setting dockerd DNS manually." |
| 171 | + CUSTOMDNS="" |
| 172 | +fi |
| 173 | +set -e |
| 174 | +
|
| 175 | +# Start docker/moby engine |
| 176 | +( sudoIf dockerd $CUSTOMDNS > /tmp/dockerd.log 2>&1 ) & |
| 177 | +
|
| 178 | +set +e |
| 179 | +
|
| 180 | +# Execute whatever commands were passed in (if any). This allows us |
| 181 | +# to set this script to ENTRYPOINT while still executing the default CMD. |
| 182 | +exec "$@" |
| 183 | +EOF |
| 184 | + |
| 185 | +chmod +x /usr/local/share/docker-init.sh |
| 186 | +chown ${USERNAME}:root /usr/local/share/docker-init.sh |
0 commit comments