Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/deploy-changed-samples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ jobs:
TEST_DATABASE_USERNAME: ${{ secrets.TEST_DATABASE_USERNAME }}
TEST_HASURA_GRAPHQL_ADMIN_SECRET: ${{ secrets.TEST_HASURA_GRAPHQL_ADMIN_SECRET }}
TEST_HASURA_GRAPHQL_DATABASE_URL: ${{ secrets.TEST_HASURA_GRAPHQL_DATABASE_URL }}
TEST_JUPYTER_TOKEN: ${{ secrets.TEST_JUPYTER_TOKEN }}
TEST_HF_TOKEN: ${{ secrets.TEST_HF_TOKEN }}
TEST_MB_DB_DBNAME: ${{ secrets.TEST_MB_DB_DBNAME }}
TEST_MB_DB_HOST: ${{ secrets.TEST_MB_DB_HOST }}
Expand Down
1 change: 1 addition & 0 deletions samples/jupyter-postgres/.devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FROM mcr.microsoft.com/devcontainers/python:3.12-bookworm
11 changes: 11 additions & 0 deletions samples/jupyter-postgres/.devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"build": {
"dockerfile": "Dockerfile",
"context": ".."
},
"features": {
"ghcr.io/defanglabs/devcontainer-feature/defang-cli:1.0.4": {},
"ghcr.io/devcontainers/features/docker-in-docker:2": {},
"ghcr.io/devcontainers/features/aws-cli:1": {}
}
}
26 changes: 26 additions & 0 deletions samples/jupyter-postgres/.github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Deploy

on:
push:
branches:
- main

jobs:
deploy:
environment: playground
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write

steps:
- name: Checkout Repo
uses: actions/checkout@v4

- name: Deploy
with:
config-env-vars: POSTGRES_PASSWORD JUPYTER_TOKEN
uses: DefangLabs/[email protected]
env:
POSTGRES_PASSWORD: ${{ secrets.POSTGRES_PASSWORD }}
JUPYTER_TOKEN: ${{ secrets.JUPYTER_TOKEN }}
69 changes: 69 additions & 0 deletions samples/jupyter-postgres/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Jupyter & Postgres

[![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)

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.

## Prerequisites

1. Download [Defang CLI](https://github.com/DefangLabs/defang)
2. (Optional) If you are using [Defang BYOC](https://docs.defang.io/docs/concepts/defang-byoc) authenticate with your cloud provider account
3. (Optional for local development) [Docker CLI](https://docs.docker.com/engine/install/)

## Development

To run the application locally, you can use the following command:

```bash
docker compose -f compose.dev.yaml up --build
```

## Configuration

For this sample, you will need to provide the following [configuration](https://docs.defang.io/docs/concepts/configuration):

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

### `POSTGRES_PASSWORD`
The password to use for the postgres database.
```bash
defang config set POSTGRES_PASSWORD
```

### `JUPYTER_TOKEN`
The token to access your Jupyter notebook server.
```bash
defang config set JUPYTER_TOKEN
```

## Deployment

> [!NOTE]
> Download [Defang CLI](https://github.com/DefangLabs/defang)

### Defang Playground

Deploy your application to the Defang Playground by opening up your terminal and typing:
```bash
defang compose up
```

### BYOC (AWS)

If you want to deploy to your own cloud account, you can use Defang BYOC:

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`.
2. Run in a terminal that has access to your AWS environment variables:
```bash
defang --provider=aws compose up
```

---

Title: Jupyter & Postgres

Short Description: This sample shows you how to spin up a postgres database and a Jupyter notebook server.

Tags: Jupyter, Postgres, Database

Languages: Python, SQL
22 changes: 22 additions & 0 deletions samples/jupyter-postgres/compose.dev.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
services:
jupyter:
extends:
service: jupyter
file: compose.yaml
environment:
JUPYTER_TOKEN: jupyter
POSTGRES_PASSWORD: password
volumes:
- ./jupyter/notebooks:/home/jovyan/work

db:
extends:
service: db
file: compose.yaml
environment:
POSTGRES_PASSWORD: password
volumes:
- postgres_data:/var/lib/postgresql/data

volumes:
postgres_data:
35 changes: 35 additions & 0 deletions samples/jupyter-postgres/compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
services:
jupyter:
# Uncomment the following line and run `defang cert generate` to generate an ssl certificate for your domain
# domainname: notebooks.mycompany.com
build:
context: ./jupyter
ports:
- mode: ingress
target: 8888
published: 8888
deploy:
resources:
limits:
cpus: '1.0'
memory: 1G
environment:
JUPYTER_TOKEN:
POSTGRES_PASSWORD:
DATABASE_HOST: db
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8888/login" ]
depends_on:
- db

db:
image: postgres:14
x-defang-postgres: true
ports:
- mode: host
target: 5432
published: 5432
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
environment:
POSTGRES_PASSWORD:
15 changes: 15 additions & 0 deletions samples/jupyter-postgres/jupyter/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM jupyter/datascience-notebook

# 4.002 Error: pg_config executable not found.
# make sure the development packages are installed

USER root

RUN apt-get update && apt-get install -y libpq-dev

USER 1000

COPY requirements.txt /tmp/
RUN pip install --no-cache-dir -r /tmp/requirements.txt

COPY ./notebooks /home/jovyan/work
Loading
Loading