Skip to content

Commit a9dfabe

Browse files
authored
Merge pull request #37 from minrk/postgres
use postgres for the database
2 parents a671e52 + 6917060 commit a9dfabe

File tree

5 files changed

+52
-8
lines changed

5 files changed

+52
-8
lines changed

.env

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,12 @@ DATA_VOLUME_HOST=jupyterhub-data
3030

3131
# Data volume container mount point
3232
DATA_VOLUME_CONTAINER=/data
33+
34+
# Name of JupyterHub postgres database data volume
35+
DB_VOLUME_HOST=jupyterhub-db-data
36+
37+
# Postgres volume container mount point
38+
DB_VOLUME_CONTAINER=/var/lib/postgresql/data/jupyterhub
39+
40+
# The name of the postgres database containing JupyterHub state
41+
POSTGRES_DB=jupyterhub

Dockerfile.jupyterhub

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
# Distributed under the terms of the Modified BSD License.
33
FROM jupyterhub/jupyterhub-onbuild:0.7.2
44

5-
# Install dockerspawner and its dependencies
6-
RUN /opt/conda/bin/pip install \
5+
# Install dockerspawner, oauth, postgres
6+
RUN /opt/conda/bin/conda install psycopg2=2.7 && \
7+
/opt/conda/bin/conda clean -tipsy && \
8+
/opt/conda/bin/pip install \
79
oauthenticator==0.5.* \
810
dockerspawner==0.7.*
911

Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,15 @@ network:
1010

1111
volumes:
1212
@docker volume inspect $(DATA_VOLUME_HOST) >/dev/null 2>&1 || docker volume create --name $(DATA_VOLUME_HOST)
13+
@docker volume inspect $(DB_VOLUME_HOST) >/dev/null 2>&1 || docker volume create --name $(DB_VOLUME_HOST)
1314

1415
self-signed-cert:
1516
# make a self-signed cert
1617

18+
secrets/postgres.env:
19+
@echo "Generating postgres password in $@"
20+
@echo "POSTGRES_PASSWORD=$(shell openssl rand -hex 32)" > $@
21+
1722
secrets/jupyterhub.crt:
1823
@echo "Need an SSL certificate in secrets/jupyterhub.crt"
1924
@exit 1
@@ -36,7 +41,7 @@ else
3641
cert_files=
3742
endif
3843

39-
check-files: userlist $(cert_files)
44+
check-files: userlist $(cert_files) secrets/oauth.env secrets/postgres.env
4045

4146
pull:
4247
docker pull $(DOCKER_NOTEBOOK_IMAGE)

docker-compose.yml

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,25 @@
55
version: "2"
66

77
services:
8+
hub-db:
9+
image: postgres:9.5
10+
container_name: jupyterhub-db
11+
restart: always
12+
environment:
13+
POSTGRES_DB: ${POSTGRES_DB}
14+
PGDATA: ${DB_VOLUME_CONTAINER}
15+
env_file:
16+
- secrets/postgres.env
17+
volumes:
18+
- "db:${DB_VOLUME_CONTAINER}"
19+
820
hub:
21+
depends_on:
22+
- hub-db
923
build:
1024
context: .
1125
dockerfile: Dockerfile.jupyterhub
26+
restart: always
1227
image: jupyterhub
1328
container_name: jupyterhub
1429
volumes:
@@ -19,6 +34,8 @@ services:
1934
- "data:${DATA_VOLUME_CONTAINER}"
2035
ports:
2136
- "443:443"
37+
links:
38+
- hub-db
2239
environment:
2340
# All containers will join this network
2441
DOCKER_NETWORK_NAME: ${DOCKER_NETWORK_NAME}
@@ -28,17 +45,22 @@ services:
2845
DOCKER_NOTEBOOK_DIR: ${DOCKER_NOTEBOOK_DIR}
2946
# Using this run command (optional)
3047
DOCKER_SPAWN_CMD: ${DOCKER_SPAWN_CMD}
31-
# Required to authenticate users using GitHub OAuth
32-
GITHUB_CLIENT_ID: ${GITHUB_CLIENT_ID}
33-
GITHUB_CLIENT_SECRET: ${GITHUB_CLIENT_SECRET}
34-
OAUTH_CALLBACK_URL: ${OAUTH_CALLBACK_URL}
48+
# Postgres db info
49+
POSTGRES_DB: ${POSTGRES_DB}
50+
POSTGRES_HOST: hub-db
51+
env_file:
52+
- secrets/postgres.env
53+
- secrets/oauth.env
3554
command: >
3655
jupyterhub -f /srv/jupyterhub/jupyterhub_config.py
3756
3857
volumes:
3958
data:
4059
external:
4160
name: ${DATA_VOLUME_HOST}
61+
db:
62+
external:
63+
name: ${DB_VOLUME_HOST}
4264

4365
networks:
4466
default:

jupyterhub_config.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,16 @@
5757

5858
# Persist hub data on volume mounted inside container
5959
data_dir = os.environ.get('DATA_VOLUME_CONTAINER', '/data')
60-
c.JupyterHub.db_url = os.path.join('sqlite:///', data_dir, 'jupyterhub.sqlite')
60+
6161
c.JupyterHub.cookie_secret_file = os.path.join(data_dir,
6262
'jupyterhub_cookie_secret')
6363

64+
c.JupyterHub.db_url = 'postgresql://postgres:{password}@{host}/{db}'.format(
65+
host=os.environ['POSTGRES_HOST'],
66+
password=os.environ['POSTGRES_PASSWORD'],
67+
db=os.environ['POSTGRES_DB'],
68+
)
69+
6470
# Whitlelist users and admins
6571
c.Authenticator.whitelist = whitelist = set()
6672
c.Authenticator.admin_users = admin = set()

0 commit comments

Comments
 (0)