Skip to content

Commit

Permalink
Merge pull request #106 from getty708/dev
Browse files Browse the repository at this point in the history
Dev: update production container
  • Loading branch information
getty708-review committed Aug 22, 2021
2 parents 90c68d8 + 6306d9e commit c4c9b60
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 78 deletions.
43 changes: 40 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,20 @@ MYSQL_DATABASE=docker
MYSQL_USER=docker
MYSQL_PASSWORD=ipTVFzI5Hx
DJANGO_SECRET_KEY="a10^j@6^2q$y*c&ks29$gnb7*3eodvqp!4!$7h31mlsq0ad3+s"
VIRTUAL_HOST="pubz.example.com"
```

**WARNING: Please change secret key by yorself for production mode.**
**WARNING: Please change secret key by yorself for production mode. To generate own key, check [here](https://qiita.com/frosty/items/bb5bc1553f452e5bb8ff).**


```python
# Generate Secret Key
from django.core.management.utils import get_random_secret_key

secret_key = get_random_secret_key()
text = 'SECRET_KEY = \'{0}\''.format(secret_key)
print(text)
```

### Step.1 Build Docker Containers

Expand All @@ -39,9 +50,11 @@ Move to the directory which `docker-compose.yml` exists and issue these commands
```bash
# Create Storage Directory
mkdir -p ./storage/db/data
# Create containers with docker-compose
docker-compose build
# Create docker image with make command
cd docker/django
make build
# Start containers
cd ../../
docker-compose up
```

Expand All @@ -65,6 +78,8 @@ python manage.py createsuperuser
# Migrate Bibtex models
python manage.py makemigrations core
python manage.py migrate
# Collect static files (e.g., js, css)
python manage.py collectstatic
```

For development, use `Username=root, email=test@test.com, pw=password (pwBman88)`
Expand Down Expand Up @@ -93,12 +108,34 @@ $ mkdocs serve

With this setup, we launched 3 containers. You can access to 3 of them with your browser.


| App | URL |
|------------|------------------|
| Django | http://localhost:7000 |
| phpmyadmin | http://localhost:7070 |
| docs | http://localhost:7777 |

---

## Setup Production Server

```bash
# Create Image with make command
cd docker/django-uwsgi-nginx/
make build
# Start containers
cd ../../
docker-compose -f docker-compose-prd.yaml up -d
```

### Container and Ports

| App | Container Name | Port (External) |
|------------|------------------|-----------------|
| Django | pubz-web-prd | 8000 |
| MarinaDB | pubz-db-prd | 3306 |

---
## Licence

[MIT License](./LICENSE)
17 changes: 11 additions & 6 deletions docker-compose-prd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@ services:
volumes:
- storage-prd:/var/lib/mysql
- ./docker/mysql:/etc/mysql/conf.d
restart: always
web:
build:
context: ./
dockerfile: ./docker/django-uwsgi-nginx/Dockerfile
image: pubz:prd-v${PUBZ_VERSION}
container_name: pubz-web-prd-v${PUBZ_VERSION}
image: pubz:web-prd-v${PUBZ_VERSION}
container_name: pubz-web-prd
hostname: pubz-web-prd
environment:
MYSQL_DATABASE: $MYSQL_DATABASE
Expand All @@ -30,9 +28,11 @@ services:
DJANGO_SECRET_KEY: $DJANGO_SECRET_KEY
VIRTUAL_HOST: $VIRTUAL_HOST
ports:
- '80:8080'
- '8000:8000'
depends_on:
- db
restart: always


volumes:
storage-prd:
Expand All @@ -41,3 +41,8 @@ volumes:
type: none
device: $PWD/storage/db/data/
o: bind

networks:
default:
external:
name: shared
9 changes: 2 additions & 7 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,9 @@ services:
ports:
- "7070:80"
django:
build:
context: ./
dockerfile: ./docker/django/Dockerfile
image: pubz:dev-v${PUBZ_VERSION}
container_name: pubz-web-dev-v${PUBZ_VERSION}
# command: python /code/app/manage.py runserver 0.0.0.0:8000
image: pubz:prd-v${PUBZ_VERSION}
image: pubz:web-dev-v${PUBZ_VERSION}
container_name: pubz-web-dev
# command: python /code/app/manage.py runserver 0.0.0.0:8000
environment:
MYSQL_DATABASE: $MYSQL_DATABASE
MYSQL_USER: $MYSQL_USER
Expand Down
3 changes: 3 additions & 0 deletions docker/django-uwsgi-nginx/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
poetry.lock
pyproject.toml
src/
8 changes: 5 additions & 3 deletions docker/django-uwsgi-nginx/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,10 @@ RUN pip3 install uwsgi
RUN echo "daemon off;" >> /etc/nginx/nginx.conf


COPY ./docker/django-uwsgi-nginx/nginx-app.conf /etc/nginx/sites-available/default
COPY ./docker/django-uwsgi-nginx/supervisor-app.conf /etc/supervisor/conf.d/
COPY ./nginx-app.conf /etc/nginx/sites-available/default
COPY ./supervisor-app.conf /etc/supervisor/conf.d/
COPY ./uwsgi.ini /code/
COPY ./uwsgi_params /code/

# COPY requirements.txt and RUN pip install BEFORE adding the rest of your code, this will cause Docker's caching mechanism
# to prevent re-installing (all your) dependencies when you made a change a line or two in your app.
Expand All @@ -91,7 +93,7 @@ COPY --from=builder /root/src/requirements.txt .
RUN pip3 install -r requirements.txt --no-deps

# add (the rest of) our code
COPY ./src /code/
COPY ./src /code/app/

# ENV PYTHONPATH /usr/bin/python3

Expand Down
11 changes: 11 additions & 0 deletions docker/django-uwsgi-nginx/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
PUBZ_VERSION := 0.5.0


help:
@echo If you cannot build image with docker-compose, build container by `make build.`

build:
cp ../../pyproject.toml .
cp ../../poetry.lock .
cp -r ../../src .
docker build -t pubz:web-prd-v${PUBZ_VERSION} .
2 changes: 1 addition & 1 deletion docker/django-uwsgi-nginx/nginx-app.conf
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ upstream django {
server {
# the port your site will be served on, default_server indicates that this server block
# is the block to use if no blocks match the server_name
listen 80 default_server;
listen 8000 default_server;

# the domain name it will serve for
server_name 127.0.0.1; # substitute your machine's IP address or FQDN
Expand Down
3 changes: 2 additions & 1 deletion docker/django-uwsgi-nginx/supervisor-app.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[program:app-uwsgi]
command = /usr/local/bin/uwsgi --ini /code/uwsgi.ini
# command = /usr/local/bin/uwsgi --ini /code/uwsgi.ini
command = /venv/bin/uwsgi --ini /code/uwsgi.ini

[program:nginx-app]
command = /usr/sbin/nginx
2 changes: 1 addition & 1 deletion docker/django/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ help:
build:
cp ../../pyproject.toml .
cp ../../poetry.lock .
docker build -t pubz:dev-v${PUBZ_VERSION} .
docker build -t pubz:web-dev-v${PUBZ_VERSION} .
Loading

0 comments on commit c4c9b60

Please sign in to comment.