Install gosu for Docker

gosu is an essential help for dockerized applications. Following recipe is from daily work. I always have some connectivity issues due to security precautions. This recipe works especially behind corporate firewalls with a http proxy.

The ENVinstruction in the Dockerfile gives us some flexibility.

    http_proxy=${http_proxy:-} \

The first line defines the release version of gosu, which can easily updated by bumping the version.

Working behind a corporate firewall, requires to define the proxy environment variables http_proxy and https_proxy. You can pass the variables in the docker build command. If not omitted the defined default proxy and port 3128 is taken. I use an overall proxy - cntlm - to ease the setup. Using Jenkins to build docker containers, often comes with different settings. Therefore you can always override the default setting by passing the arguments into the docker build command.

docker build --build-arg http_proxy=http://the-mapper-proxy:8080 --build-arg https_proxy=https://secure-proxy:8181 

For Alpine distributions, this RUN command fetch the respective target from github, checks for validity and install it in the docker container for usage.

# add gosu
RUN apk update && \
    apk add vim && apk add wget && \
    set -x \
    && apk add --no-cache --virtual .gosu-deps \
        dpkg \
        gnupg \
        openssl \
    && dpkgArch="$(dpkg --print-architecture | awk -F- '{ print $NF }')" \
    && wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$dpkgArch" \
    && wget -O /usr/local/bin/gosu.asc "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$dpkgArch.asc" \
    && export GNUPGHOME="$(mktemp -d)" \
    && gpg --keyserver ha.pool.sks-keyservers.net --keyserver-options http-proxy=$http_proxy --recv-keys B42F6819007F00F88E364FD4036A9C25BF357DD4 \
    && gpg --batch --verify /usr/local/bin/gosu.asc /usr/local/bin/gosu \
    && rm -r "$GNUPGHOME" /usr/local/bin/gosu.asc \
    && chmod +x /usr/local/bin/gosu \
    && gosu nobody true \
    && apk del .gosu-deps