Skip to content

Commit 23d1f92

Browse files
committed
Changed Dockerfile to include pgdb
1 parent d87749f commit 23d1f92

1 file changed

Lines changed: 32 additions & 3 deletions

File tree

backend/Dockerfile

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,47 @@
11
# Use official Python image
22
FROM python:3.11-slim
33

4+
# Install PostgreSQL client and server
5+
RUN apt-get update && apt-get install -y \
6+
postgresql \
7+
postgresql-contrib \
8+
postgresql-client \
9+
&& rm -rf /var/lib/apt/lists/*
10+
411
# Set work directory
512
WORKDIR /app
613

714
# Install dependencies
815
COPY requirements.txt ./
916
RUN pip install --no-cache-dir -r requirements.txt
1017

11-
# Copy the rest of the code
18+
# Copy the rest of the code (including .env file)
1219
COPY . .
1320

14-
# Expose the backend port
15-
EXPOSE 8000
21+
# Set up PostgreSQL with credentials from .env file
22+
USER postgres
23+
RUN /etc/init.d/postgresql start && \
24+
psql --command "CREATE USER postgres WITH SUPERUSER PASSWORD 'postgres';" && \
25+
createdb -O postgres appdb
26+
27+
# Switch back to root user
28+
USER root
29+
30+
# Create a startup script that loads environment variables
31+
RUN echo '#!/bin/bash\n\
32+
# Load environment variables from .env file\n\
33+
if [ -f .env ]; then\n\
34+
export $(cat .env | grep -v "^#" | xargs)\n\
35+
fi\n\
36+
# Update DB_HOST to localhost since PostgreSQL runs in same container\n\
37+
export DB_HOST=localhost\n\
38+
service postgresql start\n\
39+
exec "$@"' > /entrypoint.sh && \
40+
chmod +x /entrypoint.sh
41+
42+
# Expose the backend port and PostgreSQL port
43+
EXPOSE 8000 5432
1644

1745
# Start the backend server (adjust if using something other than uvicorn)
46+
ENTRYPOINT ["/entrypoint.sh"]
1847
CMD ["python", "main.py"]

0 commit comments

Comments
 (0)