Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d771c29

Browse files
ThisIsArekuphalt
authored andcommitted
Added Docker Compose support for production (PokeAPI#183)
* Added Docker Compose support for production * Added rate limiting to nginx
1 parent c4b2d52 commit d771c29

File tree

8 files changed

+197
-1
lines changed

8 files changed

+197
-1
lines changed

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,42 @@ Run the container on host port 8000
138138
docker run -d -p 8000:8000 pokeapi
139139
```
140140

141+
142+
## Docker Compose
143+
144+
There is also a multi-container setup, managed by [Docker Compose](https://docs.docker.com/compose/). This setup allow you to deploy a production-like environment, with separate containers for each services.
145+
146+
Create data volumes for Redis and Postgres
147+
```
148+
docker volume create --name=redis_data
149+
docker volume create --name=pg_data
150+
```
151+
152+
Start the process using
153+
```
154+
docker-compose up
155+
```
156+
You can specify the ```-d``` switch to start in detached mode.
157+
This will bind port 80 and 443. Unfortunately, unlike the ```docker``` command, there is no command line arguments to specify ports. If you want to change them, edit the ```docker-compose.yml``` file.
158+
159+
After that, start the migration process
160+
```
161+
docker-compose exec app python manage.py migrate
162+
```
163+
164+
And then, import the data using the shell
165+
```
166+
docker-compose exec app python manage.py shell
167+
```
168+
169+
You can use the ```build_all()``` method, or individuals data building functions (See _V2 Database setup_)
170+
```
171+
from data.v2.build import build_all
172+
build_all()
173+
```
174+
175+
For the moment, this setup doesn't allow you to use the ```scale``` command.
176+
141177
## Contributing
142178

143179
All contributions are welcome: bug fixes, data contributions, recommendations.

Resources/docker/app/Dockerfile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
FROM python:2.7
2+
3+
ENV PYTHONUNBUFFERED 1
4+
ENV DJANGO_SETTINGS_MODULE 'config.docker-compose'
5+
ENV PYTHONHASHSEED 'random'
6+
7+
RUN mkdir /code
8+
WORKDIR /code
9+
VOLUME /code
10+
11+
ADD requirements.txt /code/
12+
RUN pip install --no-cache-dir -r requirements.txt
13+
ADD . /code/
14+
15+
CMD gunicorn config.wsgi:application -c gunicorn.py.ini
16+
EXPOSE 8000

Resources/docker/web/Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
FROM nginx:alpine
2+
COPY ./nginx/nginx.conf /etc/nginx/nginx.conf

Resources/nginx/nginx.conf

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
worker_processes 1;
2+
3+
events {
4+
worker_connections 1024;
5+
accept_mutex off;
6+
use epoll;
7+
}
8+
9+
http {
10+
include mime.types;
11+
default_type application/octet-stream;
12+
13+
sendfile on;
14+
tcp_nopush on;
15+
tcp_nodelay on;
16+
17+
keepalive_timeout 5;
18+
19+
upstream pokeapi_upstream {
20+
# 'app' is the Django container name in Docker
21+
# DO NOT EDIT IT ALONE or it'll break docker-compose
22+
server app:8000 fail_timeout=0;
23+
}
24+
25+
limit_req_zone $binary_remote_addr zone=api:10m rate=2r/s;
26+
27+
server {
28+
listen 80 deferred;
29+
server_name _;
30+
31+
client_body_timeout 5s;
32+
client_header_timeout 5s;
33+
34+
root /code;
35+
36+
location /media/ {
37+
root /code;
38+
autoindex off;
39+
}
40+
41+
location /static/ {
42+
alias /code/assets/;
43+
autoindex off;
44+
}
45+
46+
location /api/ {
47+
limit_req zone=api burst=10;
48+
49+
proxy_set_header X-Real-IP $remote_addr;
50+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
51+
proxy_set_header Host $http_host;
52+
53+
proxy_redirect off;
54+
55+
proxy_pass http://pokeapi_upstream;
56+
}
57+
58+
location / {
59+
proxy_set_header X-Real-IP $remote_addr;
60+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
61+
proxy_set_header Host $http_host;
62+
63+
proxy_redirect off;
64+
65+
proxy_pass http://pokeapi_upstream;
66+
}
67+
}
68+
}

config/docker-compose.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Docker settings
2+
from .settings import * # NOQA
3+
4+
DATABASES = {
5+
'default': {
6+
'ENGINE': 'django.db.backends.postgresql_psycopg2',
7+
'NAME': 'pokeapi',
8+
'USER': 'ash',
9+
'PASSWORD': 'pokemon',
10+
'HOST': 'db',
11+
'PORT': 5432,
12+
}
13+
}
14+
15+
16+
CACHES = {
17+
"default": {
18+
"BACKEND": "django_redis.cache.RedisCache",
19+
"LOCATION": "redis://cache:6379/1",
20+
"OPTIONS": {
21+
"CLIENT_CLASS": "django_redis.client.DefaultClient",
22+
}
23+
}
24+
}
25+
26+
DEBUG = False
27+
TASTYPIE_FULL_DEBUG = False

docker-compose.yml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
version: '2'
2+
services:
3+
cache:
4+
image: redis:alpine
5+
volumes:
6+
- redis_data:/data
7+
8+
db:
9+
image: postgres
10+
environment:
11+
POSTGRES_PASSWORD: 'pokemon'
12+
POSTGRES_USER: 'ash'
13+
POSTGRES_DB: 'pokeapi'
14+
volumes:
15+
- pg_data:/var/lib/postgresql/data
16+
17+
app:
18+
build:
19+
context: .
20+
dockerfile: ./Resources/docker/app/Dockerfile
21+
volumes:
22+
- /code
23+
links:
24+
- db
25+
- cache
26+
27+
web:
28+
build:
29+
context: ./Resources
30+
dockerfile: ./docker/web/Dockerfile
31+
ports:
32+
- "80:80"
33+
- "443:443"
34+
volumes_from:
35+
- app:ro
36+
links:
37+
- app
38+
39+
volumes:
40+
pg_data:
41+
external: true
42+
redis_data:
43+
external: true

gunicorn.py.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from multiprocessing import cpu_count
2+
3+
bind = '0.0.0.0:8000'
4+
workers = cpu_count() * 2 + 1

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ django-tastypie==0.12.1
1313
django-markdown-deux==1.0.5
1414
djangorestframework>=3.1.0
1515
drf-ujson==1.2.0
16-
gunicorn==0.17.0
16+
gunicorn==19.4.5
1717
markdown2==2.3.0
1818
mimeparse==0.1.3
1919
pilkit==1.1.12

0 commit comments

Comments
 (0)