The entrypoint file

As described in Creating a custom base image, one of the components of an ADE-compatible base image is the entrypoint file:

#!/usr/bin/env bash
#
# Copyright 2017 - 2018 Ternaris
# SPDX-License-Identifier: Apache 2.0

set -e

if [[ -n "$GITLAB_CI" ]]; then
    exec "$@"
fi


if [[ -n "$DEBUG" ]]; then
    set -x
fi


if [[ -n "$TIMEZONE" ]]; then
    echo "$TIMEZONE" > /etc/timezone
    ln -sf /usr/share/zoneinfo/"$TIMEZONE" /etc/localtime
    dpkg-reconfigure -f noninteractive tzdata
fi


groupdel "$GROUP" &>/dev/null || true
groupadd -og "$GROUP_ID" "$GROUP"

useradd -M -u "$USER_ID" -g "$GROUP_ID" -d "/home/$USER" -s /bin/bash "$USER"

groupdel video &> /dev/null || true
groupadd -og "${VIDEO_GROUP_ID}" video
gpasswd -a "${USER}" video


for x in /etc/skel/.*; do
    target="/home/$USER/$(basename "$x")"
    if [[ ! -e "$target" ]]; then
	cp -a "$x" "$target"
	chown -R "$USER":"$GROUP" "$target"
    fi
done


if [[ -z "$SKIP_ADEINIT" ]]; then
    for x in /opt/*; do
	if [[ -x "$x/.adeinit" ]]; then
	    echo "Initializing $x"
	    sudo -Hu "$USER" -- bash -lc "$x/.adeinit"
	    echo "Initializing $x done"
	fi
    done
fi


echo 'ADE startup completed.'
exec "$@"

Let’s break down the main parts:

  1. There are a few lines to help debug the entrypoint:

set -e
if [[ -n "$DEBUG" ]]; then
    set -x
fi
  1. The Docker image should behave like a regular Docker image, if it’s running in CI:

if [[ -n "$GITLAB_CI" ]]; then
    exec "$@"
fi

This ensures that ADE and CI can work together to provide developers a single, reproducible environment.

  1. The user’s environment is configured inside ADE:

    Set the timezone

    if [[ -n "$TIMEZONE" ]]; then
        echo "$TIMEZONE" > /etc/timezone
        ln -sf /usr/share/zoneinfo/"$TIMEZONE" /etc/localtime
        dpkg-reconfigure -f noninteractive tzdata
    fi
    

    This ensures the programs, like git commit will have the correct time.

    Create a user inside the container and make sure it’s in the correct groups

    groupdel "$GROUP" &>/dev/null || true
    groupadd -og "$GROUP_ID" "$GROUP"
    
    useradd -M -u "$USER_ID" -g "$GROUP_ID" -d "/home/$USER" -s /bin/bash "$USER"
    
    groupdel video &> /dev/null || true
    groupadd -og "${VIDEO_GROUP_ID}" video
    gpasswd -a "${USER}" video
    
    
    for x in /etc/skel/.*; do
        target="/home/$USER/$(basename "$x")"
        if [[ ! -e "$target" ]]; then
    	cp -a "$x" "$target"
    	chown -R "$USER":"$GROUP" "$target"
        fi
    done
    

    This ensures that the user on the host and inside ADE behave the same way. This step includes creating a home directory based on /etc/skel/, not unlike a Linux distribution creates a home directory

  2. It calls the .adeinit scripts of any volumes:

    if [[ -z "$SKIP_ADEINIT" ]]; then
        for x in /opt/*; do
    	if [[ -x "$x/.adeinit" ]]; then
    	    echo "Initializing $x"
    	    sudo -Hu "$USER" -- bash -lc "$x/.adeinit"
    	    echo "Initializing $x done"
    	fi
        done
    fi
    

    This gives a way for volumes to provide scripts that must be run on startup to configure the ADE instance appropriately for the volume. See Creating a custom volume also.

  3. Finally, it starts ADE:

    echo 'ADE startup completed.'
    exec "$@"