File tree Expand file tree Collapse file tree 6 files changed +114
-5
lines changed Expand file tree Collapse file tree 6 files changed +114
-5
lines changed Original file line number Diff line number Diff line change 1+ {
2+ "name" : " FastAPI Jinja2 Postgres WebApp" ,
3+ "image" : " mcr.microsoft.com/devcontainers/python:3.13-bookworm" ,
4+ "workspaceFolder" : " /workspaces/${localWorkspaceFolderBasename}" ,
5+
6+ "features" : {
7+ "ghcr.io/devcontainers/features/docker-outside-of-docker:1" : {}
8+ },
9+
10+ "runArgs" : [
11+ " --add-host=host.docker.internal:host-gateway"
12+ ],
13+
14+ "forwardPorts" : [8000 , 5432 ],
15+ "portsAttributes" : {
16+ "8000" : { "label" : " FastAPI" , "onAutoForward" : " openBrowser" },
17+ "5432" : { "label" : " PostgreSQL" }
18+ },
19+
20+ "initializeCommand" : " bash \" ${localWorkspaceFolder}/.devcontainer/ensure-env.sh\" " ,
21+ "postStartCommand" : " docker compose -f .devcontainer/docker-compose.yml up -d" ,
22+ "postCreateCommand" : " bash .devcontainer/init-env.sh && curl -LsSf https://astral.sh/uv/install.sh | sh && ~/.local/bin/uv sync" ,
23+
24+ "customizations" : {
25+ "vscode" : {
26+ "extensions" : [
27+ " ms-python.python" ,
28+ " ms-python.vscode-pylance"
29+ ]
30+ }
31+ }
32+ }
Original file line number Diff line number Diff line change 1+ services :
2+ db :
3+ image : postgres:16
4+ restart : unless-stopped
5+ ports :
6+ - " 5432:5432"
7+ environment :
8+ POSTGRES_USER : ${DB_USER:-postgres}
9+ POSTGRES_PASSWORD : ${DB_PASSWORD:-postgres}
10+ POSTGRES_DB : ${DB_NAME:-fastapi-jinja2-postgres-webapp}
11+ volumes :
12+ - pgdata:/var/lib/postgresql/data
13+
14+ volumes :
15+ pgdata :
Original file line number Diff line number Diff line change 1+ #! /usr/bin/env bash
2+ set -euo pipefail
3+
4+ # Run from repo root
5+ cd " $( dirname " ${BASH_SOURCE[0]} " ) /.."
6+
7+ # Ensure a .env exists BEFORE docker compose evaluates env_file
8+ if [ ! -f " .env" ]; then
9+ if [ -f " .env.example" ]; then
10+ cp .env.example .env || true
11+ else
12+ touch .env
13+ fi
14+ fi
15+
16+ echo " .env ensured for compose evaluation."
17+
Original file line number Diff line number Diff line change 1+ #! /usr/bin/env bash
2+ set -euo pipefail
3+
4+ # Run from repo root
5+ cd " $( dirname " ${BASH_SOURCE[0]} " ) /.."
6+
7+ # Ensure a working .env exists
8+ if [ ! -f " .env" ]; then
9+ if [ -f " .env.example" ]; then
10+ cp .env.example .env || true
11+ else
12+ touch .env
13+ fi
14+ fi
15+
16+ # Ensure DB_HOST points to host.docker.internal for DooD sibling container access
17+ if grep -q ' ^DB_HOST=' .env; then
18+ sed -i ' s/^DB_HOST=.*/DB_HOST=host.docker.internal/' .env
19+ else
20+ echo ' DB_HOST=host.docker.internal' >> .env
21+ fi
22+
23+ generate_secret () {
24+ if command -v openssl > /dev/null 2>&1 ; then
25+ openssl rand -base64 32
26+ else
27+ python - << 'PY '
28+ import base64, os
29+ print(base64.b64encode(os.urandom(32)).decode('ascii'))
30+ PY
31+ fi
32+ }
33+
34+ # Ensure SECRET_KEY exists and is non-empty/non-placeholder
35+ if grep -q ' ^SECRET_KEY=' .env; then
36+ current_secret=" $( grep ' ^SECRET_KEY=' .env | cut -d= -f2-) "
37+ if [ -z " ${current_secret} " ] || [ " ${current_secret} " = " changeme" ] || [ " ${current_secret} " = " REPLACE_ME" ]; then
38+ new_secret=" $( generate_secret) "
39+ sed -i " s/^SECRET_KEY=.*/SECRET_KEY=${new_secret} /" .env
40+ fi
41+ else
42+ echo " SECRET_KEY=$( generate_secret) " >> .env
43+ fi
44+
45+ echo " Environment prepared. DB_HOST set to 'host.docker.internal' and SECRET_KEY ensured."
46+
Original file line number Diff line number Diff line change 1- .devcontainer
21__pycache__
32* .pyc
43.env
Original file line number Diff line number Diff line change @@ -2,11 +2,11 @@ services:
22 db :
33 image : postgres:latest
44 environment :
5- POSTGRES_USER : ${DB_USER}
6- POSTGRES_PASSWORD : ${DB_PASSWORD}
7- POSTGRES_DB : ${DB_NAME}
5+ POSTGRES_USER : ${DB_USER:-postgres }
6+ POSTGRES_PASSWORD : ${DB_PASSWORD:-postgres }
7+ POSTGRES_DB : ${DB_NAME:-fastapi-jinja2-postgres-webapp }
88 ports :
9- - " ${DB_PORT}:5432"
9+ - " ${DB_PORT:-5432 }:5432"
1010 volumes :
1111 - postgres_data:/var/lib/postgresql/data
1212
You can’t perform that action at this time.
0 commit comments