Skip to content

Commit 5783cd0

Browse files
committed
add deploy setup
1 parent d3b493d commit 5783cd0

File tree

6 files changed

+88
-18
lines changed

6 files changed

+88
-18
lines changed

compose/django/Dockerfile

+3-2
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,15 @@ RUN set -x \
3535
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
3636

3737
# Build Python virtualenv
38+
ARG DJANGO_REQUIREMENTS
3839
COPY ./requirements /code/requirements
3940
RUN set -ex \
4041
&& pip install -U virtualenv \
4142
&& virtualenv /venv \
42-
&& LIBRARY_PATH=/lib:/usr/lib /bin/sh -c "/venv/bin/pip install -r /code/requirements/production.txt"
43+
&& LIBRARY_PATH=/lib:/usr/lib /bin/sh -c "/venv/bin/pip install -r /code/requirements/$DJANGO_REQUIREMENTS.txt"
4344

4445
COPY . /code/
4546

4647
RUN /venv/bin/python /code/manage.py collectstatic --noinput
4748

48-
CMD ["/code/compose/django/gunicorn.sh"]
49+
# CMD ["/code/compose/django/gunicorn.sh"]

docker-compose.deploy.yml

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
version: '3'
2+
3+
services:
4+
queue:
5+
image: rabbitmq:3.6-management-alpine
6+
hostname: queue
7+
env_file: .env
8+
ports:
9+
- "5672"
10+
- "15672:15672" # management plugin
11+
worker:
12+
env_file: .env
13+
environment:
14+
- C_FORCE_ROOT=1
15+
build:
16+
context: .
17+
dockerfile: ./compose/django/Dockerfile
18+
args:
19+
DJANGO_REQUIREMENTS: production
20+
command: /venv/bin/celery -A eatsmart worker --loglevel=DEBUG
21+
links:
22+
- db
23+
- queue
24+
depends_on:
25+
- db
26+
- queue
27+
app:
28+
depends_on:
29+
- db
30+
- queue
31+
32+
33+
# http://vsupalov.com/docker-env-vars/
34+
# https://github.com/docker/docker-bench-security

docker-compose.override.yml

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ version: '3'
22

33
services:
44
app:
5-
environment:
6-
- DJANGO_SETTINGS_MODULE=eatsmart.settings.docker
75
build:
8-
context: .
9-
dockerfile: ./compose/django/Dockerfile.override
6+
args:
7+
DJANGO_REQUIREMENTS: dev
8+
command: /code/compose/django/runserver.sh
109
volumes:
1110
- .:/code

docker-compose.yml

+6-10
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,24 @@ version: '3'
33
services:
44
db:
55
restart: always
6-
image: mdillon/postgis:9.3
6+
image: mdillon/postgis:9.3-alpine
77
volumes:
88
- ./pgdata:/var/lib/postgresql/data
99
expose:
1010
- "5432"
11-
queue:
12-
hostname: queue
13-
image: rabbitmq:3.6-alpine
14-
environment:
15-
- RABBITMQ_DEFAULT_USER=admin
16-
- RABBITMQ_DEFAULT_PASS=admin
17-
ports:
18-
- "5672:5672" # we forward this port because it's useful for debugging
19-
- "15672:15672" # here, we can access rabbitmq management plugin
2011
app:
2112
env_file: .env
2213
build:
2314
context: .
2415
dockerfile: ./compose/django/Dockerfile
16+
args:
17+
DJANGO_REQUIREMENTS: production
18+
command: /code/compose/django/gunicorn.sh
2519
links:
2620
- db:db
2721
ports:
2822
- "8000:8000"
2923
depends_on:
3024
- db
25+
26+
# http://vsupalov.com/docker-env-vars/

docs/docker.rst

+41-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@ Install `Docker for Mac <https://www.docker.com/docker-mac>`_ .
1111
Initial Local Development Setup
1212
-------------------------------
1313

14-
Configure environment to use docker settings file::
14+
The default development environment will provide you with running PostgreSQL and Django containers:
15+
16+
* PostgreSQL: v9.3 w/ PostGIS v2.3. ``./pgdata`` is mounted from the host environment to persist data.
17+
* Django: Mounts the current directory into the container and uses ``manage.py runserver``.
18+
19+
The configuration is loaded from ``docker-compose.override.yml`` and ``docker-compose.yml``, respectively.
20+
21+
Configure environment to use the docker settings file::
1522

1623
echo "DJANGO_SETTINGS_MODULE=eatsmart.settings.docker" > .env
1724

@@ -38,4 +45,37 @@ The easiest way to get data is to load a db dump::
3845

3946
You'll see a few errors for ``auth_permission``, ``django_content_type``, and ``django_migrations`` tables, but it will still work.
4047

48+
49+
Deploy Setup
50+
------------
51+
52+
This environment will provide you with PostgreSQL, RabbitMQ, Celery, and Django containers. The configuration is loaded from ``docker-compose.dev.yml`` and ``docker-compose.yml``, respectively.
53+
54+
Create environment variables in ``.env``::
55+
56+
DJANGO_SETTINGS_MODULE=eatsmart.settings.deploy
57+
ENVIRONMENT=LOCAL # LOCAL = testing production env with compose
58+
SECRET_KEY=<fill-me-in>
59+
DOMAIN=localhost
60+
DATABASE_URL=postgis://postgres:postgres@db:5432/postgres
61+
RABBITMQ_DEFAULT_USER=admin
62+
RABBITMQ_DEFAULT_PASS=admin
63+
RABBITMQ_DEFAULT_HOST=queue
64+
65+
Build the containers::
66+
67+
docker-compose -f docker-compose.yml -f docker-compose.deploy.yml build
68+
69+
Bring up the containers in this order::
70+
71+
docker-compose -f docker-compose.yml -f docker-compose.deploy.yml up -d db queue
72+
docker-compose -f docker-compose.yml -f docker-compose.deploy.yml up -d worker app
73+
74+
Some useful commands::
75+
76+
docker-compose -f docker-compose.yml -f docker-compose.deploy.yml logs -f
77+
docker-compose -f docker-compose.yml -f docker-compose.deploy.yml images
78+
docker-compose -f docker-compose.yml -f docker-compose.deploy.yml ps -q | xargs docker stats
79+
80+
4181
.. docker-compose run app /venv/bin/python manage.py migrate

eatsmart/settings/deploy.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777

7878
# Uncomment if using celery worker configuration
7979
CELERY_SEND_TASK_ERROR_EMAILS = True
80-
BROKER_URL = 'amqp://eatsmart_%(ENVIRONMENT)s:%(BROKER_PASSWORD)s@%(BROKER_HOST)s/eatsmart_%(ENVIRONMENT)s' % os.environ # noqa
80+
BROKER_URL = 'amqp://%(RABBITMQ_DEFAULT_USER)s:%(RABBITMQ_DEFAULT_PASS)s@%(RABBITMQ_DEFAULT_HOST)s/' % os.environ # noqa
8181

8282
# Environment overrides
8383
# These should be kept to an absolute minimum

0 commit comments

Comments
 (0)