Skip to content

Commit bad472d

Browse files
authored
Merge pull request #280 from DefangLabs/jupyter-notebook-postgres-sample
Jupyter Notebooks & Postgres Sample
2 parents 8f9e640 + da0506f commit bad472d

File tree

11 files changed

+1197
-0
lines changed

11 files changed

+1197
-0
lines changed

.github/workflows/deploy-changed-samples.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ jobs:
7676
TEST_DATABASE_USERNAME: ${{ secrets.TEST_DATABASE_USERNAME }}
7777
TEST_HASURA_GRAPHQL_ADMIN_SECRET: ${{ secrets.TEST_HASURA_GRAPHQL_ADMIN_SECRET }}
7878
TEST_HASURA_GRAPHQL_DATABASE_URL: ${{ secrets.TEST_HASURA_GRAPHQL_DATABASE_URL }}
79+
TEST_JUPYTER_TOKEN: ${{ secrets.TEST_JUPYTER_TOKEN }}
7980
TEST_HF_TOKEN: ${{ secrets.TEST_HF_TOKEN }}
8081
TEST_MB_DB_DBNAME: ${{ secrets.TEST_MB_DB_DBNAME }}
8182
TEST_MB_DB_HOST: ${{ secrets.TEST_MB_DB_HOST }}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
FROM mcr.microsoft.com/devcontainers/python:3.12-bookworm
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"build": {
3+
"dockerfile": "Dockerfile",
4+
"context": ".."
5+
},
6+
"features": {
7+
"ghcr.io/defanglabs/devcontainer-feature/defang-cli:1.0.4": {},
8+
"ghcr.io/devcontainers/features/docker-in-docker:2": {},
9+
"ghcr.io/devcontainers/features/aws-cli:1": {}
10+
}
11+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Deploy
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
deploy:
10+
environment: playground
11+
runs-on: ubuntu-latest
12+
permissions:
13+
contents: read
14+
id-token: write
15+
16+
steps:
17+
- name: Checkout Repo
18+
uses: actions/checkout@v4
19+
20+
- name: Deploy
21+
with:
22+
config-env-vars: POSTGRES_PASSWORD JUPYTER_TOKEN
23+
uses: DefangLabs/[email protected]
24+
env:
25+
POSTGRES_PASSWORD: ${{ secrets.POSTGRES_PASSWORD }}
26+
JUPYTER_TOKEN: ${{ secrets.JUPYTER_TOKEN }}

samples/jupyter-postgres/README.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Jupyter & Postgres
2+
3+
[![1-click-deploy](https://defang.io/deploy-with-defang.png)](https://portal.defang.dev/redirect?url=https%3A%2F%2Fgithub.com%2Fnew%3Ftemplate_name%3Dsample-jupyter-postgres-template%26template_owner%3DDefangSamples)
4+
5+
This sample shows you how to spin up a postgres database and a Jupyter notebook server. This is useful if you need to use Jupyter notebooks to read data from or persist data to a database.
6+
7+
## Prerequisites
8+
9+
1. Download [Defang CLI](https://github.com/DefangLabs/defang)
10+
2. (Optional) If you are using [Defang BYOC](https://docs.defang.io/docs/concepts/defang-byoc) authenticate with your cloud provider account
11+
3. (Optional for local development) [Docker CLI](https://docs.docker.com/engine/install/)
12+
13+
## Development
14+
15+
To run the application locally, you can use the following command:
16+
17+
```bash
18+
docker compose -f compose.dev.yaml up --build
19+
```
20+
21+
## Configuration
22+
23+
For this sample, you will need to provide the following [configuration](https://docs.defang.io/docs/concepts/configuration):
24+
25+
> Note that if you are using the 1-click deploy option, you can set these values as secrets in your GitHub repository and the action will automatically deploy them for you.
26+
27+
### `POSTGRES_PASSWORD`
28+
The password to use for the postgres database.
29+
```bash
30+
defang config set POSTGRES_PASSWORD
31+
```
32+
33+
### `JUPYTER_TOKEN`
34+
The token to access your Jupyter notebook server.
35+
```bash
36+
defang config set JUPYTER_TOKEN
37+
```
38+
39+
## Deployment
40+
41+
> [!NOTE]
42+
> Download [Defang CLI](https://github.com/DefangLabs/defang)
43+
44+
### Defang Playground
45+
46+
Deploy your application to the Defang Playground by opening up your terminal and typing:
47+
```bash
48+
defang compose up
49+
```
50+
51+
### BYOC (AWS)
52+
53+
If you want to deploy to your own cloud account, you can use Defang BYOC:
54+
55+
1. [Authenticate your AWS account](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html), and check that you have properly set your environment variables like `AWS_PROFILE`, `AWS_REGION`, `AWS_ACCESS_KEY_ID`, and `AWS_SECRET_ACCESS_KEY`.
56+
2. Run in a terminal that has access to your AWS environment variables:
57+
```bash
58+
defang --provider=aws compose up
59+
```
60+
61+
---
62+
63+
Title: Jupyter & Postgres
64+
65+
Short Description: This sample shows you how to spin up a postgres database and a Jupyter notebook server.
66+
67+
Tags: Jupyter, Postgres, Database
68+
69+
Languages: Python, SQL
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
services:
2+
jupyter:
3+
extends:
4+
service: jupyter
5+
file: compose.yaml
6+
environment:
7+
JUPYTER_TOKEN: jupyter
8+
POSTGRES_PASSWORD: password
9+
volumes:
10+
- ./jupyter/notebooks:/home/jovyan/work
11+
12+
db:
13+
extends:
14+
service: db
15+
file: compose.yaml
16+
environment:
17+
POSTGRES_PASSWORD: password
18+
volumes:
19+
- postgres_data:/var/lib/postgresql/data
20+
21+
volumes:
22+
postgres_data:
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
services:
2+
jupyter:
3+
# Uncomment the following line and run `defang cert generate` to generate an new ssl certificate for your domain
4+
# domainname: notebooks.mycompany.com
5+
build:
6+
context: ./jupyter
7+
ports:
8+
- mode: ingress
9+
target: 8888
10+
published: 8888
11+
deploy:
12+
resources:
13+
limits:
14+
cpus: '1.0'
15+
memory: 1G
16+
environment:
17+
JUPYTER_TOKEN:
18+
POSTGRES_PASSWORD:
19+
DATABASE_HOST: db
20+
healthcheck:
21+
test: ["CMD", "curl", "-f", "http://localhost:8888/login" ]
22+
depends_on:
23+
- db
24+
25+
db:
26+
image: postgres:14
27+
x-defang-postgres: true
28+
ports:
29+
- mode: host
30+
target: 5432
31+
published: 5432
32+
healthcheck:
33+
test: ["CMD-SHELL", "pg_isready -U postgres"]
34+
environment:
35+
POSTGRES_PASSWORD:
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FROM jupyter/datascience-notebook
2+
3+
# 4.002 Error: pg_config executable not found.
4+
# make sure the development packages are installed
5+
6+
USER root
7+
8+
RUN apt-get update && apt-get install -y libpq-dev
9+
10+
USER 1000
11+
12+
COPY requirements.txt /tmp/
13+
RUN pip install --no-cache-dir -r /tmp/requirements.txt
14+
15+
COPY ./notebooks /home/jovyan/work

0 commit comments

Comments
 (0)