diff options
author | Sam Pearson <sam@sgp.me.uk> | 2018-09-20 15:42:54 +0100 |
---|---|---|
committer | Sam Pearson <sam@sgp.me.uk> | 2018-09-28 14:35:46 +0100 |
commit | d84af0f71aeef0581224c84d74c0372ff0a59877 (patch) | |
tree | c2a2ad5887965163d29e05becd5a887b12fefb8d /bin | |
parent | f8aed6f02cf5084a43375d8680a2e5fafb761529 (diff) |
[Docker] Initial Dockerfile & docker-compose setup
This includes four containers: app, memcached, nginx, and postgres.
The preinit script is used at container startup to ensure the database
is initialised. Includes a volume for the Postgres database to permit
persistence. Also sets the `PGDATA` variable to a subdirectory to
support optional use of a filesystem mount.
The repo/branch can be specified at build time.
If `SUPERUSER_EMAIL` and `SUPERUSER_PASSWORD` are set when the FMS
container starts the preinit script will pass these to
`bin/createsuperuser` when it starts up. These have been set to test
values in the supplied Docker Compose configuration.
Reverse proxy issue
===================
If nginx and fms were on the same machine, ReverseProxy would
automatically be in use, but via docker containers they are not. Do we
need to force it to be switched on? Let's see. There are four possible
options, with their outcome:
* port not in Host, ReverseProxy not in use
Anything using the automatically-generated base instead of BASE_URL uses
port 9000, meaning those links don't work.
* port not in Host, ReverseProxy in use
Anything using the automatically-generated base instead of BASE_URL uses
port 80, meaning those links don't work (they would if you had
docker-compose listen on port 80, being then a similar situation to e.g.
the AMI image).
* port in Host, ReverseProxy not in use
This works *unless* the port is 80, just to be contrary to the above; in
that case it is stripped and :9000 is put back on, meaning those links
again don't work. I realise we use 8000, but would be confusing if
someone tried it out.
* port in Host, ReverseProxy in use
This works in all scenarios, and thus is what we go with.
Diffstat (limited to 'bin')
-rw-r--r-- | bin/docker.preinit | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/bin/docker.preinit b/bin/docker.preinit new file mode 100644 index 000000000..12881ef0b --- /dev/null +++ b/bin/docker.preinit @@ -0,0 +1,42 @@ +#!/bin/sh + +# Things to do before starting FixMyStreet in Docker + +# Make sure that the Postgres environment is up and running. +echo "Testing connection to ${FMS_DB_HOST}." +while ! pg_isready -h $FMS_DB_HOST >/dev/null 2>&1 ; do + echo "Still waiting for ${FMS_DB_HOST}..." + sleep 1 +done +echo "Done." + +# If there's a password for the postgres user, set it up for root and see if we need +# to create an FMS user. This is intended for use when using a dedicated local postgres +# container. If this variable doesn't exist, we're going to assume that the FMS user +# has been created already so the stuff below will work. +if [ -n "$POSTGRES_PASSWORD" ]; then + echo "${FMS_DB_HOST}:*:*:postgres:${POSTGRES_PASSWORD}" > /root/.pgpass + chmod 0600 /root/.pgpass + psql -h $FMS_DB_HOST -U postgres postgres -c "create user \"${FMS_DB_USER}\" with CREATEDB password '${FMS_DB_PASS}'" || true +fi + +# Set up a .pgpass for the FMS user. Note that we're assuming the same name for +# both the local shell account and the DB user. +su ${FMS_DB_USER} -c "echo \"${FMS_DB_HOST}:*:*:${FMS_DB_USER}:${FMS_DB_PASS}\" > /home/${FMS_DB_USER}/.pgpass" +chmod 0600 /home/${FMS_DB_USER}/.pgpass + +# If the FMS database doesn't exist, try to create it. +if ! su $FMS_DB_USER -c "psql -h $FMS_DB_HOST -U $FMS_DB_USER -l | egrep \"^ *${FMS_DB_NAME} *\|\" > /dev/null" ; then + su $FMS_DB_USER -c "createdb -h $FMS_DB_HOST -U $FMS_DB_USER --owner \"$FMS_DB_USER\" \"$FMS_DB_NAME\"" +fi + +# Ensure the schema is up-to-date. +su $FMS_DB_USER -c "${FMS_ROOT}/bin/update-schema --commit" + +# Update reports +su $FMS_DB_USER -c "${FMS_ROOT}/bin/update-all-reports" + +# If the right environment variables are present, set up a FMS superuser account. +if [ -n "$SUPERUSER_PASSWORD" ] && [ -n "$SUPERUSER_EMAIL" ]; then + su $FMS_DB_USER -c "${FMS_ROOT}/bin/createsuperuser $SUPERUSER_EMAIL $SUPERUSER_PASSWORD" +fi |