Skip to content

Commit

Permalink
Merge pull request #72 from lukasredev/master
Browse files Browse the repository at this point in the history
Add Docker Based Deployment
  • Loading branch information
czue authored Jun 24, 2024
2 parents 7778df7 + b602a8c commit 28f85df
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 2 deletions.
21 changes: 21 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Dockerfile
FROM python:3.12

RUN apt-get update && apt-get install nginx --yes

COPY requirements.txt requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

COPY deploy/prod-requirements.txt prod-requirements.txt
RUN pip install --no-cache-dir -r prod-requirements.txt

COPY deploy/nginx.conf /etc/nginx/sites-enabled/default

# Mounts the application code to the image
COPY . app
WORKDIR /app

EXPOSE 8080

# runs the production server
CMD ["/app/deploy/entrypoint.sh"]
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,32 @@ You can now visit your site at `http://localhost:8000/`.
The dashboard and admin interface are available at `http://localhost:8000/dashboard/` and `http://localhost:8000/admin/`.
Use the superuser created in step three of the commands above.


### Database
The default setup of this project uses a SQLite database, which is persisted on the file system. If you want to use another
database you can change the `DATABASES` config option in the `settings.py` file. This file already contains an example
configuration on how to use the [Postgres](https://www.postgresql.org/) database specified in the docker-compose file.

### Docker
You can also run the project using [Docker](https://www.docker.com/). To build the image and run the container you can run:
```bash
docker build -t django-wedding-website .
docker run -it -p 8080:8080 \
-e DJANGO_SUPERUSER_PASSWORD=changeme \
-e DJANGO_SUPERUSER_USERNAME=admin \
-e [email protected] \
django-wedding-website
```
You can now visit your site at `http://localhost:8080`

#### Docker Compose
To run the project with a Postgres database, you can
- Change the `DATABASES` configuration in `settings.py` to use the Postgres database
- Start the Postgres Database and the project container with `docker-compose up --build`
- You can now visit your site at `http://localhost:8080`

> Note that if you want to make a production deployment with Docker you need to backup the SQLite or Postgres database!
## Customization

I recommend forking this project and just manually modifying it by hand to replace everything with what you want.
Expand Down
18 changes: 16 additions & 2 deletions bigday/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@

import os

import environ

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Initialise environment variables
env = environ.Env()
environ.Env.read_env(os.path.join(BASE_DIR, '.env'))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/
Expand All @@ -23,7 +28,7 @@
# This is a default value and must be changed!
# Rename "localsettings.py.template" to 'localsettings.py' and edit your settings.
# To protect your credentials from leaking to your Git server we added 'localsettings.py' to the gitignore
SECRET_KEY = 'u7!-y4k1c6b44q507nr_l+c^12o7ur++cpzyn!$65w^!gum@h%'
SECRET_KEY = env('SECRET_KEY', default='u7!-y4k1c6b44q507nr_l+c^12o7ur++cpzyn!$65w^!gum@h%')

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
Expand Down Expand Up @@ -92,7 +97,16 @@
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
},
# if you want to use the postgres database just uncomment the following lines and comment out the sqlite3 lines
# 'default': {
# 'ENGINE': 'django.db.backends.postgresql',
# 'NAME': env('POSTGRES_DB'),
# 'USER': env('POSTGRES_USER'),
# 'PASSWORD': env('POSTGRES_PASSWORD'),
# 'HOST': env('POSTGRES_SERVER'),
# 'PORT': env('POSTGRES_PORT'),
# }
}


Expand Down
15 changes: 15 additions & 0 deletions deploy/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

python manage.py collectstatic --noinput

# i commit my migration files to git so i dont need to run it on server
# ./manage.py makemigrations app_name
python manage.py migrate

# Create the superuser

python manage.py createsuperuser --noinput

/usr/sbin/nginx -g 'daemon off;' &

gunicorn bigday.wsgi --bind 0.0.0.0:8000
31 changes: 31 additions & 0 deletions deploy/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
upstream app_server_djangoapp {
server localhost:8000 fail_timeout=0;
}

server {
listen 8080;
server_name localhost;

access_log /var/log/nginx/wedding-access.log;
error_log /var/log/nginx/wedding-error.log info;

keepalive_timeout 5;

root /app;

location /static {
autoindex on;
alias /app/static_root;
}

location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;

if (!-f $request_filename) {
proxy_pass http://app_server_djangoapp;
break;
}
}
}
30 changes: 30 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
version: "3"
services:
# WARNING: If you want to use this docker-compose file in production, you need to backup your database!
postgres:
container_name: postgres-wedding
image: postgres
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=password
- POSTGRES_DB=wedding
ports:
- 5432:5432
wedding:
container_name: wedding
build: .
environment:
- DEBUG=False
- SECRET_KEY=changeme
- POSTGRES_SERVER=postgres-wedding
- POSTGRES_USER=postgres
- POSTGRES_DB=wedding
- POSTGRES_PASSWORD=password
- POSTGRES_PORT=5432
- DJANGO_SUPERUSER_PASSWORD=changeme
- DJANGO_SUPERUSER_USERNAME=admin
- [email protected]
ports:
- 8080:8080
depends_on:
- postgres
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
django==4.2.*
Fabric==1.10.2
psycopg2-binary==2.9.3
django-environ==0.11.2

0 comments on commit 28f85df

Please sign in to comment.