Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6f4c0a4

Browse files
author
Rafael
committed
somewhat runnung docker
1 parent 02b4dd1 commit 6f4c0a4

File tree

6 files changed

+75
-19
lines changed

6 files changed

+75
-19
lines changed

‎Dockerfile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Use an official Python runtime as a parent image
2+
FROM python:3.12-slim
3+
4+
# Set the working directory in the container
5+
WORKDIR /app
6+
7+
# Copy the requirements file into the container
8+
COPY requirements.txt .
9+
10+
# Install the dependencies
11+
RUN pip install --no-cache-dir -r requirements.txt
12+
13+
# Copy the application code into the container
14+
COPY . .
15+
16+
# Expose the port the app runs on
17+
EXPOSE 8000
18+
19+
# Command to run the application
20+
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]

‎app/config.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,15 @@
55

66

77
class Settings(BaseSettings):
8-
DATABASE_PORT: int
9-
POSTGRES_PASSWORD: str
8+
POSTGRES_HOSTNAME: str
109
POSTGRES_USER: str
10+
POSTGRES_PASSWORD: str
1111
POSTGRES_DB: str
12-
POSTGRES_HOST: str
13-
POSTGRES_HOSTNAME: str
14-
api_key: str
12+
DATABASE_PORT: int
13+
API_KEY: str
1514

1615
class Config:
17-
env_file = '../.env'
16+
env_file = ".env"
1817

1918

2019
settings = Settings()

‎app/database.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
from .config import settings
55
from fastapi_utils.guid_type import setup_guids_postgresql
66

7-
POSTGRES_URL = f"postgresql://{settings.POSTGRES_USER}:{settings.POSTGRES_PASSWORD}@{settings.POSTGRES_HOSTNAME}:{settings.DATABASE_PORT}/{settings.POSTGRES_DB}"
7+
POSTGRES_URL = (
8+
f"postgresql://{settings.POSTGRES_USER}@{settings.POSTGRES_HOSTNAME}:{settings.DATABASE_PORT}/{settings.POSTGRES_DB}"
9+
)
10+
811

912
engine = create_engine(
1013
POSTGRES_URL, echo=True

‎app/main.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
from app import models, note
2-
from fastapi import FastAPI, HTTPException
2+
from fastapi import FastAPI, HTTPException, Depends
33
from fastapi.middleware.cors import CORSMiddleware
4-
from .database import engine
4+
from .database import engine, get_db
55
from dotenv import load_dotenv
66
import os
77
import requests
8+
from sqlalchemy.exc import OperationalError
9+
from sqlalchemy.orm import Session
810

911
models.Base.metadata.create_all(bind=engine)
1012

@@ -32,6 +34,15 @@
3234
def root():
3335
return {"message": "Welcome to FastAPI with SQLAlchemy"}
3436

37+
@app.get("/api/db-healthchecker")
38+
def db_healthchecker(db: Session = Depends(get_db)):
39+
try:
40+
# Attempt to execute a simple query to check database connectivity
41+
db.execute("SELECT 1")
42+
return {"message": "Database is healthy"}
43+
except OperationalError:
44+
raise HTTPException(status_code=500, detail="Database is not reachable")
45+
3546
@app.get("/posts/{post_id}")
3647
async def get_post(post_id: int):
3748
try:
@@ -66,4 +77,4 @@ async def get_crypto_price():
6677
raise HTTPException(status_code=response.status_code, detail="API call failed")
6778

6879
except Exception as e:
69-
raise HTTPException(status_code=500, detail=str(e))
80+
raise HTTPException(status_code=500, detail=str(e))

‎docker-compose.yml

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,37 @@
1-
version: '3'
1+
version: "3.9"
2+
23
services:
34
postgres:
4-
image: postgres
5+
image: postgres:14
56
container_name: postgres
7+
environment:
8+
POSTGRES_USER: postgres
9+
POSTGRES_PASSWORD: password
10+
POSTGRES_DB: fastapi
11+
POSTGRES_HOST_AUTH_METHOD: trust
612
ports:
7-
- '6500:5432'
8-
restart: always
9-
env_file:
10-
- ./.env
13+
- "5432:5432"
14+
volumes:
15+
- postgres_data:/var/lib/postgresql/data
16+
healthcheck:
17+
test: ["CMD-SHELL", "pg_isready -U user"]
18+
interval: 10s
19+
timeout: 5s
20+
retries: 5
21+
22+
app:
23+
build:
24+
context: .
25+
dockerfile: Dockerfile
26+
ports:
27+
- "8000:8000"
1128
volumes:
12-
- postgres-db:/var/lib/postgresql/data
29+
- .:/app
30+
depends_on:
31+
postgres:
32+
condition: service_healthy
33+
env_file:
34+
- .env
35+
1336
volumes:
14-
postgres-db:
37+
postgres_data:

‎requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ itsdangerous #==2.1.2
1616
Jinja2 #==3.1.2
1717
MarkupSafe #==2.1.1
1818
orjson #==3.8.1
19-
psycopg2 #==2.9.5
19+
psycopg2-binary #==2.9.5
2020
pycodestyle #==2.9.1
2121
pydantic #==1.10.2
2222
pydantic-settings ###########

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /