-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bd8fea1
commit dadff8f
Showing
7 changed files
with
252 additions
and
99 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Use the official Python image with slim variant | ||
FROM python:3.9-slim-buster | ||
|
||
# Set environment variables directly in the Dockerfile | ||
ENV db_username=postgres | ||
ENV db_password=your_postgres_password | ||
ENV SECRET_KEY=your_secret_key | ||
ENV DB_HOST=localhost | ||
ENV DB_PORT=5432 | ||
ENV OAUTHDB=OAUTHDB | ||
|
||
# Set the working directory | ||
WORKDIR /app | ||
|
||
# Install system dependencies | ||
RUN apt-get update && apt-get install -y --no-install-recommends \ | ||
build-essential \ | ||
libpq-dev \ | ||
postgresql \ | ||
postgresql-contrib \ | ||
supervisor \ | ||
&& rm -rf /var/lib/apt/lists/* | ||
|
||
# Install Python dependencies | ||
COPY requirements.txt . | ||
RUN pip install --no-cache-dir -r requirements.txt | ||
|
||
# Copy the application code | ||
COPY . . | ||
|
||
# Copy the supervisor configuration file | ||
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf | ||
|
||
# Copy the initialization script | ||
COPY init.sh /init.sh | ||
RUN chmod +x /init.sh | ||
|
||
# Ensure the logs directory exists | ||
RUN mkdir -p /var/log/postgresql && mkdir -p /var/log/fastapi | ||
|
||
# Expose port 3100 | ||
EXPOSE 3100 | ||
|
||
# Set the entrypoint to run the init script before starting supervisord | ||
ENTRYPOINT ["/bin/bash", "-c", "/init.sh && /usr/bin/supervisord -n"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#!/bin/bash | ||
set -e | ||
|
||
# Define PostgreSQL version | ||
PG_VERSION=11 | ||
|
||
# Paths | ||
POSTGRES_BIN_DIR="/usr/lib/postgresql/${PG_VERSION}/bin" | ||
|
||
# Initialize PostgreSQL data directory if it doesn't exist | ||
if [ ! -d "/var/lib/postgresql/data" ]; then | ||
echo "Initializing PostgreSQL data directory..." | ||
mkdir -p /var/lib/postgresql/data | ||
chown -R postgres:postgres /var/lib/postgresql | ||
su postgres -c "${POSTGRES_BIN_DIR}/initdb -D /var/lib/postgresql/data" | ||
fi | ||
|
||
# Update pg_hba.conf to allow password authentication | ||
PG_HBA=/var/lib/postgresql/data/pg_hba.conf | ||
if [ -f "$PG_HBA" ]; then | ||
echo "Configuring PostgreSQL to use md5 authentication..." | ||
sed -i "s/^\(local\s\+all\s\+all\s\+\)peer/\1md5/" $PG_HBA | ||
fi | ||
|
||
# Start PostgreSQL to perform setup | ||
echo "Starting PostgreSQL..." | ||
su postgres -c "${POSTGRES_BIN_DIR}/pg_ctl -D /var/lib/postgresql/data -w start" | ||
|
||
# Create PostgreSQL user with SUPERUSER privilege if it doesn't exist | ||
echo "Creating PostgreSQL user with SUPERUSER privilege if it doesn't exist..." | ||
su postgres -c "psql -tc \"SELECT 1 FROM pg_roles WHERE rolname = '$db_username'\" | grep -q 1 || psql -c \"CREATE USER $db_username WITH PASSWORD '$db_password' SUPERUSER;\"" | ||
|
||
# Create database if it doesn't exist | ||
echo "Creating PostgreSQL database if it doesn't exist..." | ||
su postgres -c "psql -tc \"SELECT 1 FROM pg_database WHERE datname = '$OAUTHDB'\" | grep -q 1 || psql -c \"CREATE DATABASE $OAUTHDB OWNER $db_username;\"" | ||
|
||
# Grant all privileges on the database to the user (redundant but ensures full access) | ||
su postgres -c "psql -c \"GRANT ALL PRIVILEGES ON DATABASE $OAUTHDB TO $db_username;\"" | ||
|
||
# Stop PostgreSQL (Supervisor will manage it) | ||
echo "Stopping PostgreSQL..." | ||
su postgres -c "${POSTGRES_BIN_DIR}/pg_ctl -D /var/lib/postgresql/data -m fast -w stop" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
fastapi | ||
uvicorn[standard] | ||
asyncpg | ||
passlib[bcrypt] | ||
PyJWT | ||
starlette | ||
pydantic | ||
python-dotenv | ||
itsdangerous | ||
python-multipart |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
[supervisord] | ||
nodaemon=true | ||
|
||
[program:postgresql] | ||
command=/usr/lib/postgresql/11/bin/postgres -D /var/lib/postgresql/data | ||
user=postgres | ||
stdout_logfile=/var/log/postgresql/postgres_stdout.log | ||
stderr_logfile=/var/log/postgresql/postgres_stderr.log | ||
autostart=true | ||
autorestart=true | ||
priority=10 | ||
|
||
[program:fastapi] | ||
command=uvicorn main:app --host 0.0.0.0 --port 3100 | ||
directory=/app | ||
autostart=true | ||
autorestart=true | ||
stdout_logfile=/var/log/fastapi_stdout.log | ||
stderr_logfile=/var/log/fastapi_stderr.log | ||
priority=20 |