Skip to content

Commit

Permalink
Docker setup init (#2967)
Browse files Browse the repository at this point in the history
* Update contributors.json

* new contributor

* Improve README.md

* Containerize the blt project along with database

* Apply suggestions from code review

* Update .env.example

* retain sentry

* set ssl_redirect to True

* Revert to apply pre-commit

* pass pre-commit and make the required changes by Settings SECURE_SSL_REDIRECT=TRUE

* retain secure_proxy_ssl_header and pass run test

* pass run test

---------

Co-authored-by: DonnieBLT <[email protected]>
  • Loading branch information
drvcodenta and DonnieBLT authored Dec 2, 2024
1 parent 2ccd1f7 commit ae45ef2
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 13 deletions.
13 changes: 8 additions & 5 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ [email protected]
SUPERUSER_PASSWORD=admi345n@12343453

DOMAIN_NAME=localhost
PORT=80
PORT=8000
CALLBACK_URL_FOR_GITHUB=http://127.0.0.1:8000/
CALLBACK_URL_FOR_GOOGLE=http://127.0.0.1:8000/
CALLBACK_URL_FOR_FACEBOOK=http://127.0.0.1:8000/
Expand All @@ -17,12 +17,15 @@ LANGCHAIN_TRACING_V2=true
LANGCHAIN_PROJECT=default
LANGCHAIN_ENDPOINT="https://api.smith.langchain.com"

#Database URL
DATABASE_URL=postgres://user:password@localhost:5432/dbname
#Database Credentials
POSTGRES_PASSWORD=postgres
POSTGRES_USER=postgres
POSTGRES_DB=example_db
POSTGRES_PORT=5432 #default is 5432, but sometimes it may be occupied by other services. So you can change it to any other port.
DATABASE_URL=postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@localhost:${POSTGRES_PORT}/${POSTGRES_DB}

#Sentry DSN
SENTRY_DSN=https://[email protected]/0


SLACK_CLIENT_ID=
SLACK_CLIENT_SECRET=
SLACK_CLIENT_SECRET=
16 changes: 12 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,17 @@ RUN apt-get update && apt-get install -y \
RUN pip install poetry
RUN poetry config virtualenvs.create false
RUN poetry install
RUN pip install opentelemetry-api opentelemetry-instrumentation

RUN python manage.py migrate
RUN python manage.py loaddata website/fixtures/initial_data.json
# RUN python manage.py collectstatic
RUN python manage.py initsuperuser
# Install dos2unix
RUN apt-get update && apt-get install -y dos2unix

# Add entrypoint

COPY entrypoint.sh /entrypoint.sh
RUN dos2unix .env Dockerfile docker-compose.yml entrypoint.sh ./blt/settings.py
RUN chmod +x /entrypoint.sh


ENTRYPOINT [ "./entrypoint.sh" ]
CMD [ "poetry", "run", "python", "manage.py", "runserver", "0.0.0.0:8000" ]
53 changes: 49 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,55 @@
version: "3"

services:
db:
image: postgres
ports:
- "${POSTGRES_PORT}:5432"
volumes:
- postgres_db:/var/lib/postgresql/data
environment:
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_DB=${POSTGRES_DB}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
healthcheck:
test: ["CMD", "pg_isready", "-U", "${POSTGRES_USER}", "-d", "${POSTGRES_DB}"]
interval: 10s
timeout: 5s
retries: 5
container_name: postgres-db
restart: always

app:
command: "poetry run python manage.py runserver 0.0.0.0:8000"
build: .
depends_on:
db:
condition: service_healthy
container_name: app
restart: on-failure
env_file:
- .env
volumes:
- .:/blt
ports:
- "8000:8000"
- "${PORT}:8000"
environment:
- DATABASE_URL=postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db:5432/${POSTGRES_DB}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
command: ./entrypoint.sh

init:
profiles:
- init
build: .
depends_on:
db:
condition: service_healthy
env_file:
- .env
volumes:
- .:/blt
environment:
- DATABASE_URL=postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db:5432/${POSTGRES_DB}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
command: ./entrypoint.sh

volumes:
postgres_db:
43 changes: 43 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/sh
set -x
echo "Entrypoint script is running"

# Wait for the database to be ready
until PGPASSWORD=$POSTGRES_PASSWORD psql -h "db" -U "$POSTGRES_USER" -d "$POSTGRES_DB" -c '\q'; do
>&2 echo "Postgres is unavailable - sleeping"
sleep 1
done

>&2 echo "Postgres is up - executing command"

# Function to check if migrations are applied
check_migrations() {
python manage.py showmigrations --plan | grep -q "\[ \]"
return $?
}

# Check if migrations need to be applied
if check_migrations; then
echo "Migrations need to be applied. Running initialization tasks."

# Run migrations
echo "Migration script is running"
python manage.py migrate

# Load initial data
python manage.py loaddata website/fixtures/initial_data.json

# Create superuser
echo "Creating the superuser, if it does not exist!"
python manage.py initsuperuser

# Collect static files
echo "Collecting the static files!"
python manage.py collectstatic --noinput
else
echo "All migrations have already been applied. Skipping initialization."
fi

# Start the main application
echo "Starting the main application http://localhost:8000/"
exec python manage.py runserver 0.0.0.0:8000

0 comments on commit ae45ef2

Please sign in to comment.