Skip to content

Commit

Permalink
established deployable environment
Browse files Browse the repository at this point in the history
  • Loading branch information
nishu-saini committed Nov 7, 2023
1 parent 72d16d3 commit f2eb24b
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
DB_NAME=dbname
DB_USER=rootuser
DB_PASS=changeme
DJANGO_SECRET_KEY=changeme
DJANGO_ALLOWED_HOSTS=127.0.0.1
12 changes: 8 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ ENV PYTHONUNBUFFERED 1

COPY ./requirements.txt /tmp/requirements.txt
COPY ./requirements.dev.txt /tmp/requirements.dev.txt
COPY ./scripts /scripts
COPY ./app /app
WORKDIR /app
EXPOSE 8000
Expand All @@ -14,7 +15,7 @@ RUN python -m venv /py && \
/py/bin/pip install --upgrade pip && \
apk add --update --no-cache postgresql-client jpeg-dev && \
apk add --update --no-cache --virtual .tmp-build-deps \
build-base postgresql-dev musl-dev zlib zlib-dev && \
build-base postgresql-dev musl-dev zlib zlib-dev linux-headers && \
/py/bin/pip install -r /tmp/requirements.txt && \
if [ $DEV = true ]; \
then /py/bin/pip install -r /tmp/requirements.dev.txt ; \
Expand All @@ -28,8 +29,11 @@ RUN python -m venv /py && \
mkdir -p /vol/web/media && \
mkdir -p /vol/web/static && \
chown -R django-user:django-user /vol && \
chmod -R 755 /vol
chmod -R 755 /vol && \
chmod -R +x /scripts

ENV PATH="/py/bin:$PATH"
ENV PATH="/scripts:/py/bin:$PATH"

USER django-user
USER django-user

CMD [ "run.sh" ]
11 changes: 8 additions & 3 deletions app/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,18 @@
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-1@cd&!j=pybeqe5y8^g5yp^^rq*&(vzy!ua2*&5j(yi*fb3d9='
SECRET_KEY = os.environ.get('SECRET_KEY', 'changeme')

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
DEBUG = bool(int(os.environ.get('DEBUG', 0)))

ALLOWED_HOSTS = []

ALLOWED_HOSTS.extend(
filter(
None,
os.environ.get('ALLOWED_HOSTS', '').split(',')
)
)

# Application definition

Expand Down
43 changes: 43 additions & 0 deletions docker-compose-deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
version: "3.9"

services:
app:
build:
context: .
restart: always
volumes:
- static-data:/vol/web
environment:
- DB_HOST=db
- DB_NAME=${DB_NAME}
- DB_USER=${DB_USER}
- DB_PASS=${DB_PASS}
- SECRET_KEY=${DJANGO_SECRET_KEY}
- ALLOWED_HOSTS=${DJANGO_ALLOWED_HOSTS}
depends_on:
- db

db:
image: postgres:13-alpine
restart: always
volumes:
- postgres-data:/var/lib/postgresql/data
environment:
- POSTGRES_DB=${DB_NAME}
- POSTGRES_USER=${DB_USER}
- POSTGRES_PASSWORD=${DB_PASS}

proxy:
build:
context: ./proxy
restart: always
depends_on:
- app
ports:
- 8000: 8000
volumes:
- static-data/vol/static

volumes:
postgres-data:
static-data:
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ services:
- DB_NAME=devdb
- DB_USER=devuser
- DB_PASS=changeme
- DEBUG=1
depends_on:
- db

Expand Down
24 changes: 24 additions & 0 deletions proxy/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
FROM nginxinc/nginx-unprivileged:1-alpine
LABEL maintainer="quarnstric"

COPY ./default.conf.tpl /etc/nginx/defualt.conf.tpl
COPY ./uwsgi_params /etc/nginx/uwsgi_params
COPY ./run.sh /run.sh

ENV LISTEN_PORT=8000
ENV APP_HOST=app
ENV APP_PORT=9000

USER root

RUN mkdir -p /vol/static && \
chmod 755 /vol/static && \
touch /etc/nginx/conf.d/defualt.conf && \
chown nginx:nginx /etc/nginx/conf.d/defualt.conf && \
chmod +x /run.sh

VOLUME /vol/static

USER nginx

CMD [ "/run.sh" ]
13 changes: 13 additions & 0 deletions proxy/default.conf.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
server {
listen ${LISTEN_PORT};

location /static {
alias /vol/static;
}

location / {
uwsgi_pass ${APP_HOST}:${APP_PORT};
include /etc/nginx/uwsgi_params;
client_max_body_size 10M;
}
}
6 changes: 6 additions & 0 deletions proxy/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh

set -e

envsubst < /etc/nginx/default.conf.tpl > /etc/nginx/conf.d/default.conf
nginx -g 'daemon off;'
13 changes: 13 additions & 0 deletions proxy/uwsgi_params
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;
uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_ADDR $server_addr;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ Django>=3.2.4,<3.3
djangorestframework>=3.12.4,<3.13
psycopg2>=2.8.6,<2.9
drf-spectacular>=0.15.1,<0.16
Pillow>=8.2.0,<8.3.0
Pillow>=8.2.0,<8.3.0
uwsgi>=2.0.19,<2.1
9 changes: 9 additions & 0 deletions scripts/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/sh

set -e

python manage.py wait_for_db
python manage.py collectstatic --noinput


uwsgi --socket :9000 --workers 4 --master --enable-threads --module app.wsgi

0 comments on commit f2eb24b

Please sign in to comment.