Skip to content

Commit c899ad7

Browse files
fix: fastapi with docker compose (#15)
Signed-off-by: Guruprasanna02 <guruprasannavasudevan02@gmail.com>
1 parent 4c2a22f commit c899ad7

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

fastapi-postgres/Dockerfile

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
# Use the official Python image as the base image
22
FROM python:3.11.5-bullseye
33

4+
# Install PostgreSQL client
5+
RUN apt-get update \
6+
&& apt-get install -y --no-install-recommends \
7+
postgresql-client \
8+
curl \
9+
netcat-traditional
10+
411
# Set the working directory to /app
512
WORKDIR /app
613

@@ -20,11 +27,8 @@ COPY . .
2027
# POSTGRES_HOST=0.0.0.0 \
2128
# POSTGRES_PORT=5432
2229

23-
# Install PostgreSQL client
24-
RUN apt-get update && apt-get install -y postgresql-client
25-
2630
# Expose port 80 for the FastAPI application
2731
EXPOSE 8000
2832

2933
# Start the FastAPI application
30-
CMD ["uvicorn", "application.main:app", "--host", "0.0.0.0", "--port", "8000"]
34+
CMD [ "/app/entrypoint.sh" ]

fastapi-postgres/docker-compose.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ services:
1313
- ./sql/init.sql:/docker-entrypoint-initdb.d/init.sql
1414
networks:
1515
- keploy-network
16-
16+
1717
app:
1818
container_name: fastapi-app
1919
image: fastapi
@@ -26,4 +26,8 @@ services:
2626
depends_on:
2727
- postgres
2828
networks:
29-
- keploy-network
29+
- keploy-network
30+
31+
networks:
32+
keploy-network:
33+
external: true

fastapi-postgres/entrypoint.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/sh
2+
3+
until nc -z -v -w30 postgres 5432
4+
do
5+
echo "Waiting for database connection..."
6+
# wait for 5 seconds before check again
7+
sleep 5
8+
done
9+
10+
uvicorn application.main:app --host 0.0.0.0 --port 8000

0 commit comments

Comments
 (0)