diff --git a/examples/postgres/README.md b/examples/postgres/README.md index 6dbb9add..13cb9f5f 100644 --- a/examples/postgres/README.md +++ b/examples/postgres/README.md @@ -4,9 +4,9 @@ This example shows how you can connect Jupyterhub to a Postgres database instead of the default SQLite backend. ### Running Postgres with Jupyterhub on the host. -0. Replace `ENV JPY_PSQL_PASSWORD arglebargle` with your own password in the - Dockerfile for `examples/postgres/db`. (Alternatively you can pass -e - `JPY_PSQL_PASSWORD=` when you start the db container.) +0. Uncomment and replace `ENV JPY_PSQL_PASSWORD arglebargle` with your own + password in the Dockerfile for `examples/postgres/db`. (Alternatively, pass + -e `JPY_PSQL_PASSWORD=` when you start the db container.) 1. `cd` to the root of your jupyterhub repo. @@ -21,26 +21,27 @@ instead of the default SQLite backend. 4. Run jupyterhub with `jupyterhub --db=postgresql://jupyterhub:@localhost:5433/jupyterhub`. -### Running Postgres with Containerized Jupyterhub. -0. Replace `ENV JPY_PSQL_PASSWORD arglebargle` with your own password in the - Dockerfile for `examples/postgres/hub`. (Alternatively you can pass -e - `JPY_PSQL_PASSWORD=` when you start the hub container.) +5. Log in as the user running jupyterhub on your host machine. -1. Do steps 0-2 in from the above section, ensuring that the values set/passed +### Running Postgres with Containerized Jupyterhub. +0. Do steps 0-2 in from the above section, ensuring that the values set/passed for `JPY_PSQL_PASSWORD` match for the hub and db containers. -2. Build the hub image with `docker build -t jupyterhub-postgres-db - examples/postgres/db`. This may take a minute or two the first time it's run. +1. Build the hub image with `docker build -t jupyterhub-postgres-hub + examples/postgres/hub`. This may take a minute or two the first time it's + run. -3. Run the db image with `docker run -d --name=jpy-db +2. Run the db image with `docker run -d --name=jpy-db jupyterhub-postgres`. Note that, unlike when connecting to a host machine jupyterhub, we don't specify a port-forwarding scheme here, but we do need to specify a name for the container. -4. Run the containerized hub with `docker run -it --link jpy-db:postgres - jupyterhub-postgres-hub`. +3. Run the containerized hub with `docker run -it --link jpy-db:postgres + jupyterhub-postgres-hub`. This instructs docker to run the hub container + with a link to the already-running db container, which will forward + environment and connection information from the DB to the hub. -5. Log in as one of the users defined in the `examples/postgres/hub/` +4. Log in as one of the users defined in the `examples/postgres/hub/` Dockerfile. By default `rhea` is the server's admin user, `io` and `ganymede` are non-admin users, and all users' passwords are their usernames. diff --git a/examples/postgres/db/Dockerfile b/examples/postgres/db/Dockerfile index 346f37df..844614ff 100644 --- a/examples/postgres/db/Dockerfile +++ b/examples/postgres/db/Dockerfile @@ -6,5 +6,5 @@ RUN mkdir /docker-entrypoint-initdb.d # startup, prior to the the database being started. COPY initdb.sh /docker-entrypoint-initdb.d/init.sh -# Replace this with your own password. -ENV JPY_PSQL_PASSWORD arglebargle +# Uncomment and replace this with your own password. +# ENV JPY_PSQL_PASSWORD arglebargle diff --git a/examples/postgres/db/initdb.sh b/examples/postgres/db/initdb.sh index 3d326c01..808354b4 100644 --- a/examples/postgres/db/initdb.sh +++ b/examples/postgres/db/initdb.sh @@ -1,5 +1,16 @@ #!/bin/bash +echo ""; +echo "Running initdb.sh."; +if [ -z "$JPY_PSQL_PASSWORD" ]; then + echo "Need to set JPY_PSQL_PASSWORD in Dockerfile via command line"; + exit 1; +elif [ "$JPY_PSQL_PASSWORD" == "arglebargle" ]; then + echo "WARNING: Running with password!" + echo "You are STRONGLY ADVISED to use your own password."; +fi +echo ""; + # Start a postgres daemon, ignoring log output. gosu postgres pg_ctl start -w -l /dev/null diff --git a/examples/postgres/hub/Dockerfile b/examples/postgres/hub/Dockerfile index 4264480e..28ef1a8c 100644 --- a/examples/postgres/hub/Dockerfile +++ b/examples/postgres/hub/Dockerfile @@ -7,9 +7,6 @@ RUN apt-get install -y libpq-dev \ && apt-get clean -y \ && pip3 install psycopg2 -# Replace this with your own password. -ENV JPY_PSQL_PASSWORD arglebargle - RUN useradd -m -G shadow -p $(openssl passwd -1 rhea) rhea RUN chown rhea . diff --git a/examples/postgres/hub/jupyter_hub_config.py b/examples/postgres/hub/jupyter_hub_config.py index 81832703..f883cea7 100644 --- a/examples/postgres/hub/jupyter_hub_config.py +++ b/examples/postgres/hub/jupyter_hub_config.py @@ -6,9 +6,10 @@ c = get_config() c.JupyterHubApp.admin_users = {'rhea'} c.Authenticator.whitelist = {'ganymede', 'io', 'rhea'} -# Set up the database url. +# These environment variables are automatically supplied by the linked postgres +# container. import os; -pg_pass = os.getenv('JPY_PSQL_PASSWORD') +pg_pass = os.getenv('POSTGRES_ENV_JPY_PSQL_PASSWORD') pg_host = os.getenv('POSTGRES_PORT_5432_TCP_ADDR') c.JupyterHubApp.db_url = 'postgresql://jupyterhub:{}@{}:5432/jupyterhub'.format( pg_pass,