#!/bin/bash set -e is_enabled () { echo "$1" | grep -q -i -E "^(yes|on|true|1)$" } #CODE_PASSWORD #CODE_PASSWORD_FILE CODE_UUID="${CODE_UUID:-1001}" CODE_GUID="${CODE_GUID:-1001}" CODE_USERNAME=$(getent passwd "$CODE_UUID" | cut -d: -f1) CODE_GROUPNAME=$(getent group "$CODE_GUID" | cut -d: -f1) USER_SUDO="${USER_SUDO:-false}" RUN_DOCKER="${RUN_DOCKER:-true}" #DOCKER_HOST #GIT_NAME #GIT_EMAIL if [ -f /firstrun ]; then echo 'First run of the container. Code will be configured.' echo 'If exist, configuration and data will be reused and upgraded as needed.' # Configure timezone if needed if [ -n "$TZ" ]; then cp /usr/share/zoneinfo/$TZ /etc/localtime fi # Configure GIT if [ "$GIT_NAME" ]; then HOME=/home/code git config --global user.name "$GIT_NAME" fi if [ "$GIT_EMAIL" ]; then HOME=/home/code git config --global user.email "$GIT_EMAIL" fi # Create code user/group if needed if [ -z "$CODE_GROUPNAME" ]; then groupadd -r -g "$CODE_GUID" code CODE_GROUPNAME="code" fi if [ -z "$CODE_USERNAME" ]; then if [ "$CODE_PASSWORD" ] && [ "$CODE_PASSWORD_FILE" ]; then echo 'error: both CODE_PASSWORD and CODE_PASSWORD_FILE are set (but are exclusive)\n' exit 1 fi password=""`` if [ "$CODE_PASSWORD" ]; then password="$CODE_PASSWORD" elif [ "$CODE_PASSWORD_FILE" ]; then password="$(< ${CODE_PASSWORD_FILE})" fi useradd -r -d /home/code -s /bin/bash -g "$CODE_GUID" -u "$CODE_UUID" -M -N code usermod -aG docker code echo "code:$password" | chpasswd CODE_USERNAME="code" else usermod -d /home/code "$CODE_USERNAME" fi chown -R "$CODE_USERNAME":"$CODE_GROUPNAME" /home/code # Clean rm -rf /firstrun fi # Add or remove user from sudo group if is_enabled "${USER_SUDO}"; then groups "$CODE_USERNAME" | tr " " "\n" | grep "^sudo$" || usermod -aG sudo "$CODE_USERNAME" else ! groups "$CODE_USERNAME" | tr " " "\n" | grep "^sudo$" || gpasswd -d "$CODE_USERNAME" sudo fi # Link external docker even for dumb tools (ie. Visual Studio Docker extension) if [ "$RUN_DOCKER" != "true" -a "$DOCKER_HOST" != "unix:///var/run/docker.sock" -a "$DOCKER_HOST" != "" ]; then ln -s "${DOCKER_HOST##unix://}" /run/docker.sock fi export CODE_USERNAME export RUN_DOCKER cd /home/code exec "$@"