Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Table of Contents

Introduction

  • Users can build their own Docker images for Agents.
  • This article explains options how to create the Agent image.

Build Environment

For the build environment the following directory hierarchy is assumed:

  • agent
    • build.sh
    • build
      • Dockerfile
      • start-agent.sh
      • config

The root directory agent could have any name. Consider that below build script by default will use the directory name and release number to determine the resulting image name.

The build script build.sh and Agent start script start-agent.sh are explained below.

Dockerfile

Docker images for JS7 Agents provided by SOS make use of the following Dockerfile:

Code Block
languagebash
titleDockerfile for Agent Image
linenumberstrue
collapsetrue
FROM openjdk:8-jre-alpine

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

# BUILD SETTINGS

# provide build arguments for release information
ARG JS_RELEASE
ARG JS_RELEASE_MAJOR

# default user id has to match later run-time user
ARG JS_USER_ID=${UID:-1001}
ARG JS_HTTP_INTERFACE=${JS_HTTP_INTERFACE:-0.0.0.0}
ARG JS_HTTP_PORT=${JS_HTTP_PORT:-4445}
ARG JS_HTTPS_INTERFACE=${JS_HTTPS_INTERFACE:-0.0.0.0}
ARG JS_HTTPS_PORT=${JS_HTTPS_PORT:-4443}
ARG JS_JAVA_OPTIONS=${JS_JAVA_OPTIONS}

# RUN-TIME SETTINGS

# JS7 JobScheduler ports and Java options
ENV RUN_JS_HTTP_INTERFACE=${RUN_JS_HTTP_INTERFACE:-$JS_HTTP_INTERFACE}
ENV RUN_JS_HTTP_PORT=${RUN_JS_HTTP_PORT:-$JS_HTTP_PORT}
ENV RUN_JS_HTTPS_INTERFACE=${RUN_JS_HTTPS_INTERFACE:-$JS_HTTPS_INTERFADE}
ENV RUN_JS_HTTPS_PORT=${RUN_JS_HTTPS_PORT}
ENV RUN_JS_JAVA_OPTIONS=${RUN_JS_JAVA_OPTIONS:-$JS_JAVA_OPTIONS}

# PREPARATION

# install process tools, net tools, bash
RUN apk update && apk add --no-cache \
    procps \
    net-tools \
    bash

# setup working directory
RUN mkdir -p /var/sos-berlin.com/js7
WORKDIR /var/sos-berlin.com/js7

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

# add start script
COPY start-agent.sh /usr/local/bin/

# INSTALLATION

# extract tarball
# for JDK < 12, /dev/random does not provide sufficient entropy, see https://kb.sos-berlin.com/x/lIM3
# add jobscheduler user account and make it the owner of directories
RUN adduser -u ${JS_USER_ID:-1001} --disabled-password --home /home/jobscheduler --no-create-home --shell /bin/bash jobscheduler jobscheduler && \
    test -e /usr/local/src/js7_agent_unix.${JS_RELEASE}.tar.gz && \
    tar xfvz /usr/local/src/js7_agent_unix.${JS_RELEASE}.tar.gz -C /var/sos-berlin.com/js7  && \
    rm /usr/local/src/js7_agent_unix.${JS_RELEASE}.tar.gz && \
    chown -R jobscheduler:jobscheduler /var/sos-berlin.com && \
    chmod +x /usr/local/bin/start-agent.sh && \
    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

# CONFIGURATION

# copy default configuration
# COPY config/ /var/sos-berlin.com/js7/agent/var_$JS_HTTP_PORT/config/

# CODA

# run-time user, can be overwritten when running the container
USER jobscheduler

CMD ["sh","-c","/usr/local/bin/start-agent.sh --http-port=$RUN_JS_HTTP_INTERFACE:$RUN_JS_HTTP_PORT --https-port=$RUN_JS_HTTPS_INTERFACE:$RUN_JS_HTTPS_PORT --java-options=\"$RUN_JS_JAVA_OPTIONS\""]

Explanations:

  • Line 1: The base image is OpenJDK Java 1.8 (Alpine based). You can run Agents with newer Java releases, however, stick to Oracle, OpenJDK or AdoptOpenJDK as the source for your Java base image. Alternatively you can use your own base image and install Java 1.8 or later on top of this.
  • Line 8 - 9: The release identification is injected by build arguments. This information is used to determine the tarball to be downloaded or copied.
  • Line 12 - 17: Defaults for the user id running the Agent inside the container as well as HTTP and HTTPS ports are provided. These values can be overwritten by providing the respective build arguments.
  • Line 22 - 26: 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 31 - 34: The image OS is updated and additional packages are installed (ps, netstat, bash).
  • Line 41 - 42: You can either download the Agent tarball directly from the SOS web site or you store the tarball with the build directory and copy from this location.
  • Line 45: The start-agent.sh script is copied from the build directory to the image. Users can apply their own version of the start script. The start script used by SOS looks like this:

    Code Block
    languagebash
    titleAgent Start Script
    linenumberstrue
    collapsetrue
    #!/bin/sh
    
    js_http_port=""
    js_https_port=""
    js_java_options=""
    
    for option in "$@"
    do
      case "$option" in
             --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=//'`
                               ;;
             *)                echo "unknown argument: $option"
                               exit 1
                               ;;
      esac
    done
    
    
    js_args=""
    
    if [ ! "$js_http_port" = "" ] && [ ! "$js_http_port" = ":" ]
    then
      js_args="$js_args --http-port=$js_http_port"
    fi
    
    if [ ! "$js_https_port" = "" ] && [ ! "$js_https_port" = ":" ]
    then
      js_args="$js_args --https-port=$js_https_port"
    fi
    
    if [ ! "$js_java_options" = "" ]
    then
      js_args="$js_args --java-options=$js_java_options"
    fi
    
    echo "starting Agent: /var/sos-berlin.com/js7/agent/bin/agent.sh start $js_args"
    /var/sos-berlin.com/js7/agent/bin/agent.sh start $js_args && tail -f /dev/null
  • Line 52: The jobscheduler user account is created and is assigned the user id and group id handed over by the respective build arguments. This translates to the fact that the account running the Agent 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 Agent in mounted volumes with identical permissions.
  • Line 53 - 55: The tarball is extracted. For Unix Agents no installer is used.
  • Line 56 - 57: The jobscheduler user account is made the owner of installed files and the start script is made executable.
  • Line 58: Java releases < Java 12 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 63: Optionally a config folder is available in the build directory and is copied to the config sub-folder in the image. The parent folder var_<port> is determined from the HTTP port that the Agent is built for. This can be useful to create an image with individual default settings in configuration files, see JS7 - Agent Configuration Items.
  • Line 70: The start script is executed and is dynamically parameterized from environment variables that are forwarded when starting the container.

Build Script

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

Code Block
languagebash
titleBuild Script for Agent Image
linenumberstrue
collapsetrue
#!/bin/bash

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.0.0-SNAPSHOT"
JS_REPOSITORY="sosberlin/js7"
JS_IMAGE="$(basename "${SCRIPT_HOME}")-${JS_RELEASE//\./-}"

JS_USER_ID="$UID"

JS_HTTP_PORT="4445"
JS_HTTPS_PORT=

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=//'`
                           ;;
         --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_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

Explanations:

  • Line 12 - 22: Default values are specified that 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 an Agent 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 is by default the user id of the user running the build script.
    • the HTTP port and HTTPS port: if the respective port is not specified then the Agent 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 Docker 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 27 - 50: The above options can be overwritten by command line arguments like this:


    Code Block
    languagebash
    titleRunning the Build Script with Arguments
    linenumberstrue
    ./build.sh --http-port=14445 --https-port=14443 --java-options="-Xmx1G"
  • Line 54 - 63: 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.