Skip to content

Commit

Permalink
Dockerize the project
Browse files Browse the repository at this point in the history
  • Loading branch information
aktech committed May 26, 2020
1 parent 53dc800 commit 6c0c94c
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 2 deletions.
37 changes: 37 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

16 changes: 16 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM python:3.8-slim-buster
RUN apt-get update -y && \
apt-get install -y python-pip python-dev && \
apt-get install -y libpq-dev && \
apt-get install -y postgresql-client

WORKDIR /app
COPY ./requirements.txt /app/requirements.txt
RUN pip install -r requirements.txt

COPY ./start.sh /app/start.sh
RUN chmod +x /app/start.sh


EXPOSE 8000
ENV PYTHONPATH='.'
26 changes: 26 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
version: '3'

volumes:
local_postgres_data: {}
local_postgres_data_backups: {}

services:
db:
image: postgres
volumes:
- local_postgres_data:/var/lib/postgresql/data
- local_postgres_data_backups:/backups
env_file:
- .env

web:
build: .
command: ./start.sh
volumes:
- .:/app
ports:
- "8000:8000"
depends_on:
- db
env_file:
- .env
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
ansible==2.9.9
Django==3.0.6
psycopg2==2.8.5
8 changes: 6 additions & 2 deletions sample/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,12 @@

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'sample',
'USER': 'postgres',
'PASSWORD': os.environ['POSTGRES_PASSWORD'],
'HOST': 'db',
'PORT': 5432,
}
}

Expand Down
26 changes: 26 additions & 0 deletions start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env bash

RETRIES=5
until PGPASSWORD="$POSTGRES_PASSWORD" psql -h db -U "$POSTGRES_USER" -d "$POSTGRES_DB" -c "select 1" || [ $RETRIES -eq 0 ]; do
echo "Waiting for postgres server, $((RETRIES)) remaining attempts..."
RETRIES=$((RETRIES-=1))
sleep 1
done

python manage.py migrate

superuser_name=admin
[email protected]

script=$(cat <<END
from django.contrib.auth import get_user_model
User = get_user_model() # get the currently active user model,
User.objects.filter(username='admin').exists() or User.objects.create_superuser('admin', '$superuser_email', '$SUPERUSER_PASSWORD')
END
)


echo "$script" | python manage.py shell
python manage.py runserver 0.0.0.0:8000

0 comments on commit 6c0c94c

Please sign in to comment.