Introduction

  • This article describes a simplified build process for Controller images that is extracted from the SOS build environment.
  • Users can build their own container images for Controllers.

Build Environment

The following directory hierarchy is assumed for the build environment:

  • controller
    • build.sh
    • build
      • Dockerfile
      • entrypoint.sh
      • config

The root directory controller can have any name. Note that build script listed below will, by default, use the directory name and release number to determine the resulting image name.

The build.sh build script and entrypoint.sh entrypoint script are described below.

Dockerfile

Download: Dockerfile

Container images for JS7 Controllers provided by SOS make use of the following Dockerfile:

Dockerfile for Controller Image
# BUILD PRE-IMAGE # BUILD PRE-IMAGE

FROM alpine:3.15 AS js7-pre-image

# provide build arguments for release information
ARG JS_RELEASE
ARG JS_RELEASE_MAJOR

# add/copy installation tarball
# ADD https://download.sos-berlin.com/JobScheduler.${JS_RELEASE_MAJOR}/js7_controller_unix.${JS_RELEASE}.tar.gz /usr/local/src/
COPY js7_controller_unix.${JS_RELEASE}.tar.gz /usr/local/src/

RUN mkdir -p /var/sos-berlin.com/js7 && \
    test -e /usr/local/src/js7_controller_unix.${JS_RELEASE}.tar.gz && \
    tar xfvz /usr/local/src/js7_controller_unix.${JS_RELEASE}.tar.gz -C /var/sos-berlin.com/js7/

# add entrypoint script
COPY entrypoint.sh /var/sos-berlin.com/js7/

# copy configuration
COPY config/ /var/sos-berlin.com/js7/controller/var/config/

# BUILD IMAGE

FROM alpine:3.15 AS js7-image

LABEL maintainer="Software- und Organisations-Service GmbH"

# provide build arguments for release information
ARG JS_RELEASE
ARG JS_RELEASE_MAJOR

# image user id has to match later run-time user id
ARG JS_USER_ID=${JS_USER_ID:-1001}
ARG JS_ID=${JS_ID:-jobscheduler}
ARG JS_HTTP_PORT=${JS_HTTP_PORT:-4444}
ARG JS_HTTPS_PORT=${JS_HTTPS_PORT:-""}
ARG JS_JAVA_OPTIONS=${JS_JAVA_OPTIONS}

# JS7 user id, Controller ID, ports and Java optionsa
ENV RUN_JS_USER_ID=${RUN_JS_USER_ID:-1001}
ENV RUN_JS_ID=${RUN_JS_ID:-$JS_ID}
ENV RUN_JS_HTTP_PORT=${RUN_JS_HTTP_PORT:-$JS_HTTP_PORT}
ENV RUN_JS_HTTPS_PORT=${RUN_JS_HTTPS_PORT}
ENV RUN_JS_JAVA_OPTIONS=${RUN_JS_JAVA_OPTIONS:-$JS_JAVA_OPTIONS}

COPY --from=js7-pre-image ["/var/sos-berlin.com/js7", "/var/sos-berlin.com/js7"]

# INSTALLATION

# install process tools, net tools, bash, openjdk
# add jobscheduler user account and make it the owner of directories
# for JDK < 12, /dev/random does not provide sufficient entropy, see https://kb.sos-berlin.com/x/lIM3
RUN apk update && apk add --no-cache \
    --repository=http://dl-cdn.alpinelinux.org/alpine/edge/community \
    procps \
    net-tools \
    su-exec \
    bash \
    shadow \
    openjdk11 && \
    sed -i 's/securerandom.source=file:\/dev\/random/securerandom.source=file:\/dev\/urandom/g' /usr/lib/jvm/java-11-openjdk/conf/security/java.security && \
    sed -i 's/jdk.tls.disabledAlgorithms=SSLv3, RC4, DES, MD5withRSA, DH keySize < 1024, \\/jdk.tls.disabledAlgorithms=SSLv3, RC4, DES, MD5withRSA, DH keySize < 1024, TLSv1, TLSv1.1, \\/g' /usr/lib/jvm/java-11-openjdk/conf/security/java.security && \
    adduser -u ${JS_USER_ID:-1001} --disabled-password --home /home/jobscheduler --shell /bin/bash jobscheduler jobscheduler && \
    chown -R jobscheduler:jobscheduler /var/sos-berlin.com && \
    mv /var/sos-berlin.com/js7/entrypoint.sh /usr/local/bin/ && \
    chmod +x /usr/local/bin/entrypoint.sh

# JDK 8
#   openjdk8 && \
#   sed -i 's/securerandom.source=file:\/dev\/random/securerandom.source=file:\/dev\/urandom/g' /usr/lib/jvm/java-1.8-openjdk/jre/lib/security/java.security && \
# JDK 11
#   openjdk11 && \
#   sed -i 's/securerandom.source=file:\/dev\/random/securerandom.source=file:\/dev\/urandom/g' /usr/lib/jvm/java-11-openjdk/conf/security/java.security && \

# license
# COPY --chown=jobscheduler:jobscheduler js7-license.jar /var/sos-berlin.com/js7/controller/lib/user_lib/

# START

ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]

CMD ["/var/sos-berlin.com/js7/controller/bin/controller.sh","start_docker"]

Explanation:

  • The build script implements two stages to exclude installer files from the resulting image.
  • Line 3: The base image is the current Alpine image at build-time.
  • Line 6 - 7: The release identification is injected by build arguments. This information is used to determine the tarball to be downloaded or copied.
  • Line 10 - 11: You can either download the Controller tarball directly from the SOS web site or you can store the tarball with the build directory and copy from this location.
  • Line 13 - 15: The tarball is extracted. For Unix Controllers no installer is used.

  • Line 18: The entrypoint.sh script is copied from the build directory to the image. Users can apply their own version of the entrypoint script. The entrypoint script used by SOS looks like this:
    Download: entrypoint.sh

    Controller Entrypoint Script
    #!/bin/bash
    
    js_args_count=0
    
    if [ ! "$RUN_JS_ID" = "" ]
    then
      js_args[$js_args_count]="--id=$RUN_JS_ID"
      js_args_count=$(( $js_args_count + 1 ))
    fi
    
    if [ ! "$RUN_JS_HTTP_PORT" = "" ] && [ ! "$RUN_JS_HTTP_PORT" = ":" ]
    then
      js_args[$js_args_count]="--http-port=$RUN_JS_HTTP_PORT"
      js_args_count=$(( $js_args_count + 1 ))
    fi
    
    if [ ! "$RUN_JS_HTTPS_PORT" = "" ] && [ ! "$RUN_JS_HTTPS_PORT" = ":" ]
    then
      js_args[$js_args_count]="--https-port=$RUN_JS_HTTPS_PORT"
      js_args_count=$(( $js_args_count + 1 ))
    fi
    
    if [ ! "$RUN_JS_JAVA_OPTIONS" = "" ]
    then
      js_args[$js_args_count]="--java-options=\"$RUN_JS_JAVA_OPTIONS\""
      js_args_count=$(( $js_args_count + 1 ))
    fi
    
    # work directory is created by container
    if [ -d "/var/sos-berlin.com/js7/controller/var/work"  ]
    then
      rmdir /var/sos-berlin.com/js7/controller/var/work
    fi
    
    JS_USER_ID=`echo $RUN_JS_USER_ID | cut -d ':' -f 1`
    JS_GROUP_ID=`echo $RUN_JS_USER_ID | cut -d ':' -f 2`
    
    BUILD_GROUP_ID=`cat /etc/group | grep jobscheduler | cut -d ':' -f 3`
    BUILD_USER_ID=`cat /etc/passwd | grep jobscheduler | cut -d ':' -f 4`
    
    if [ "$(id -u)" = "0" ]
    then
      if [ ! "$BUILD_USER_ID" = "$JS_USER_ID" ]
      then
        echo "JS7 entrypoint script switchng ownership of image user id '$BUILD_USER_ID' -> '$JS_USER_ID', group id '$BUILD_GROUP_ID' -> '$JS_GROUP_ID'"
        usermod -u $JS_USER_ID jobscheduler
        groupmod -g $JS_GROUP_ID jobscheduler
        find /var/sos-berlin.com/ -group $BUILD_GROUP_ID -exec chgrp -h jobscheduler {} \;
        find /var/sos-berlin.com/ -user $BUILD_USER_ID -exec chown -h jobscheduler {} \;
      fi
    
      echo "JS7 entrypoint script switching to user account 'jobscheduler' to run start script"
      echo "JS7 entrypoint script starting Controller: exec su-exec jobscheduler:$JS_GROUP_ID /var/sos-berlin.com/js7/controller/bin/controller.sh start_docker" "${js_args[@]}"
      exec su-exec jobscheduler:$JS_GROUP_ID /var/sos-berlin.com/js7/controller/bin/controller.sh start_docker "${js_args[@]}"
    else
      if [ "$BUILD_USER_ID" = "$JS_USER_ID" ]
      then
        if [ "$(id -u)" = "$JS_USER_ID" ]
        then
          echo "JS7 entrypoint script running for user id '$(id -u)'"
        else
          echo "JS7 entrypoint script running for user id '$(id -u)' cannot switch to user id '$JS_USER_ID', group id '$JS_GROUP_ID'"
          echo "JS7 entrypoint script missing permission to switch user id and group id, consider to omit the 'docker run --user' option"
        fi
      else
        echo "JS7 entrypoint script running for user id '$(id -u)' cannot switch image user id '$BUILD_USER_ID' -> '$JS_USER_ID', group id '$BUILD_GROUP_ID' -> '$JS_GROUP_ID'"
        echo "JS7 entrypoint script missing permission to switch user id and group id, consider to omit the 'docker run --user' option"
      fi
    
      echo "JS7 entrypoint script starting Controller: exec sh -c /var/sos-berlin.com/js7/controller/bin/controller.sh start_docker" "${js_args[@]}"
      exec sh -c "/var/sos-berlin.com/js7/controller/bin/controller.sh start_docker \"${js_args[*]}\""
    fi
  • Line 21: The config folder available in the build directory is copied to the config sub-folder in the image. This can be useful for creating an image with individual default settings in configuration files, see the JS7 - Controller Configuration Items article.
  • Line 34 - 38: Defaults for the Controller ID and user id running the Controller inside the container as well as HTTP and HTTPS ports are provided. These values can be overwritten by providing the relevant build arguments.
  • Line 41 - 45: Environment variables are provided at run-time, not at build-time. They can be used to specify ports and Java options when running the container.
  • Line 54 - 60: The image OS is updated and additional packages are installed (ps, netstat, bash,).
  • Line 61: The most recent Java 11 package available with Alpine is applied. Controllers can be operated with newer Java releases. However, stick to Oracle, OpenJDK or AdoptOpenJDK as the source for your Java LTS release. Alternatively you can use your own base image and install Java 1.8 or later on top of this. For details see Which Java versions is JobScheduler available for?
  • Line 62 - 63: Java releases might make use of /dev/random for random number generation. This is a bottleneck as random number generation with this file is blocking. Instead /dev/urandom should be used that implements non-blocking behavior. The change of the random file is applied to the Java security file.
  • Line 64 - 65: The jobscheduler user account is created and assigned the user id and group id handed over by the respective build arguments. This translates to the fact that the account running the Controller inside the container and the account that starts the container are assigned the same user id and group id. This allows the account running the container to access any files created by the Controller in mounted volumes with identical permissions.
  • Line 81 - 83: The entrypoint script and Controller start script are executed and are dynamically parameterized from environment variables when starting the container.

Build Script

The build script offers a number of options to parameterize the Dockerfile:

Build Script for Controller Image
#!/bin/sh

set -e

SCRIPT_HOME=$(dirname "$0")
SCRIPT_HOME="`cd "${SCRIPT_HOME}" >/dev/null && pwd`"
SCRIPT_FOLDER="`basename $(dirname "$SCRIPT_HOME")`"


# ----- modify default settings -----

JS_RELEASE="2.5.0"
JS_REPOSITORY="sosberlin/js7"
JS_IMAGE="$(basename "${SCRIPT_HOME}")-${JS_RELEASE//\./-}"

JS_USER_ID="$UID"
JS_ID="jobscheduler"

JS_HTTP_PORT="4444"
JS_HTTPS_PORT="4443"

JS_JAVA_OPTIONS="-Xmx500m"
JS_BUILD_ARGS=""

# ----- modify default settings -----


for option in "$@"
do
  case "$option" in
         --release=*)      JS_RELEASE=`echo "$option" | sed 's/--release=//'`
                           ;;
         --repository=*)   JS_REPOSITORY=`echo "$option" | sed 's/--repository=//'`
                           ;;
         --image=*)        JS_IMAGE=`echo "$option" | sed 's/--image=//'`
                           ;;
         --user-id=*)      JS_USER_ID=`echo "$option" | sed 's/--user-id=//'`
                           ;;
         --id=*)           JS_ID=`echo "$option" | sed 's/--id=//'`
                           ;;
         --http-port=*)    JS_HTTP_PORT=`echo "$option" | sed 's/--http-port=//'`
                           ;;
         --https-port=*)   JS_HTTPS_PORT=`echo "$option" | sed 's/--https-port=//'`
                           ;;
         --java-options=*) JS_JAVA_OPTIONS=`echo "$option" | sed 's/--java-options=//'`
                           ;;
         --build-args=*)   JS_BUILD_ARGS=`echo "$option" | sed 's/--build-args=//'`
                           ;;
         *)                echo "unknown argument: $option"
                           exit 1
                           ;;
  esac
done

set -x

docker build --no-cache --rm \
      --tag=$JS_REPOSITORY:$JS_IMAGE \
      --file=$SCRIPT_HOME/build/Dockerfile \
      --build-arg="JS_RELEASE=$JS_RELEASE" \
      --build-arg="JS_RELEASE_MAJOR=$(echo $JS_RELEASE | cut -d . -f 1,2)" \
      --build-arg="JS_USER_ID=$JS_USER_ID" \
      --build-arg="JS_ID=$JS_ID" \
      --build-arg="JS_HTTP_PORT=$JS_HTTP_PORT" \
      --build-arg="JS_HTTPS_PORT=$JS_HTTPS_PORT" \
      --build-arg="JS_JAVA_OPTIONS=$JS_JAVA_OPTIONS" \
      $JS_BUILD_ARGS $SCRIPT_HOME/build

set +x

Explanation:

  • Line 12 - 23: Default values are specified which are used if no command line arguments are provided. This includes values for:
    • the release number: adjust this value to the release of JS7 that you want to build a Controller for.
    • the repository which by default is sosberlin:js7.
    • the image name is determined from the current folder name and the release number.
    • the user id by default is the user id of the user running the build script.
    • the Controller ID can be specified that is a unique identifier for a Controller installation.
    • the HTTP port and HTTPS port: if the respective port is not specified then the Controller will not listen to a port for the respective protocol. You can for example disable the HTTP protocol by specifying an empty value. The default ports should be fine as they are mapped by the run script to outside ports on the container's host. However, you can modify ports as you like.
    • Java options: typically you would specify default values e.g. for Java memory consumption. The Java options can be overwritten by the run script when starting the container. However, you might want to create your own image with adjusted default values.
  • Line 28 - 53: The above options can be overwritten by command line arguments like this:


    Running the Build Script with Arguments
    ./build.sh --id=js7-prod --http-port=14445 --https-port=14443 --java-options="-Xmx1G"
  • Line 60 - 71: The effective docker build command is executed with arguments. The Dockerfile is assumed to be located with the build sub-directory of the current directory.