Docker Images: basic entrypoint

This is a docker entrypoint script that loads environment variables from a mounted file and executes the given command; most of the time it’s all you ever need.

#!/bin/sh

set -e

# Load environment variables.
if [ -f /environment/secrets.env ];
then
  set -a
  echo 'Sourcing: /environment/secrets.env'
  . /environment/secrets.env
  set +a
fi

# Executes the given command.
exec "$@"

Give this file a short and simple name like entry and copy it in your Dockerfile:

COPY ./entry /

RUN set -exo pipefail                      && \
                                              \
    echo 'Ensure entrypoint is executable' && \
    chmod +x /entry

ENTRYPOINT ["/entry"]

In an image with an existing entrypoint, like postgres, you need to chain the existing entrypoint, and redefine the command:

ENTRYPOINT ["/entry", "docker-entrypoint.sh"]
CMD ["postgres"]

Make sure to run with that secrets.env file mounted:

docker run --rm -it -v "$PWD"/secrets.env:/environment/secrets.env alpine env