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 need.


set -e

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

# 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, since CMD is always unset when an ENTRYPOINT is defined:

ENTRYPOINT ["/entry", ""]
CMD ["postgres"]

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

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

Or if using docker-compose do this:

      context: .
      dockerfile: ./docker/postgres/Dockerfile
      - ./environment/secrets.env:/environment/secrets.env