Skip to content

Commit 9c602a2

Browse files
Merge pull request #58 from HackSoftware/dockerize
Dockerize the project
2 parents 8347705 + 2d20332 commit 9c602a2

File tree

9 files changed

+137
-4
lines changed

9 files changed

+137
-4
lines changed

.github/workflows/django.yml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
name: Django application
22
on: [push]
33
jobs:
4+
docker_build:
5+
runs-on: ubuntu-latest
6+
steps:
7+
- uses: actions/checkout@v2
8+
- name: Build docker
9+
run: docker-compose build
10+
- name: Type check
11+
run: docker-compose run django mypy styleguide_example/
12+
- name: Run migrations
13+
run: docker-compose run django python manage.py migrate
14+
- name: Run tests
15+
run: docker-compose run django py.test
16+
417
build:
518
runs-on: ubuntu-latest
619
services:
@@ -19,7 +32,7 @@ jobs:
1932
- name: Fetch history, because Heroku deploy fails otherwise.
2033
uses: actions/setup-python@v2
2134
with:
22-
python-version: 3.9
35+
python-version: 3.10.2
2336
- name: Install dependencies
2437
run: |
2538
python -m pip install --upgrade pip
@@ -30,6 +43,7 @@ jobs:
3043
run: python manage.py migrate
3144
- name: Run tests
3245
run: py.test
46+
3347
deploy_to_heroku:
3448
runs-on: ubuntu-latest
3549
needs: build

README.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ Few important things:
2020
* It comes with GitHub Actions support, [based on that article](https://hacksoft.io/github-actions-in-action-setting-up-django-and-postgres/)
2121
* It comes with examples for writing tests with fakes & factories, based on the following articles - <https://www.hacksoft.io/blog/improve-your-tests-django-fakes-and-factories>, <https://www.hacksoft.io/blog/improve-your-tests-django-fakes-and-factories-advanced-usage>
2222
* It comes with [`whitenoise`](http://whitenoise.evans.io/en/stable/) setup.
23-
* It can be easily deployed to Heroku.
23+
* It can be easily deployed to Heroku or AWS ECS.
24+
* Dockerized for local development with docker-compose.
2425
* It comes with an example list API, that uses [`django-filter`](https://django-filter.readthedocs.io/en/stable/) for filtering & pagination from DRF.
2526
* It comes with [`mypy`](https://mypy.readthedocs.io/en/stable/) configured, using both <https://github.com/typeddjango/django-stubs> and <https://github.com/typeddjango/djangorestframework-stubs/>
2627
* Basic `mypy` configuration is located in [`setup.cfg`](setup.cfg)
@@ -201,7 +202,7 @@ Example data structure:
201202
}
202203
```
203204

204-
## Helpful commands
205+
## Helpful commands for local development without docker-compose
205206

206207
To create Postgres database:
207208

@@ -227,6 +228,26 @@ To start Celery Beat:
227228
celery -A styleguide_example.tasks beat -l info --scheduler django_celery_beat.schedulers:DatabaseScheduler
228229
```
229230

231+
## Helpful commands for local development with docker-compose
232+
233+
To build and run everything
234+
235+
```
236+
docker-compose up
237+
```
238+
239+
To run migrations
240+
241+
```
242+
docker-compose run django python manage.py migrate
243+
```
244+
245+
To shell
246+
247+
```
248+
docker-compose run django python manage.py shell
249+
```
250+
230251
## Heroku
231252

232253
The project is ready to be deployed on Heroku. There's a current deployment that can be found - <https://hacksoft-styleguide-example.herokuapp.com/>

docker-compose.yml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
version: "3.9"
2+
3+
services:
4+
db:
5+
image: postgres:14.1
6+
environment:
7+
- POSTGRES_DB=styleguide_example_db
8+
- POSTGRES_USER=postgres
9+
- POSTGRES_PASSWORD=postgres
10+
11+
rabbitmq:
12+
image: "rabbitmq:alpine"
13+
14+
healthcheck:
15+
test: rabbitmq-diagnostics -q ping
16+
interval: 30s
17+
timeout: 30s
18+
retries: 3
19+
20+
django:
21+
build:
22+
context: .
23+
dockerfile: docker/local.Dockerfile
24+
command: python manage.py runserver 0.0.0.0:8000
25+
environment:
26+
- DATABASE_URL=postgres://postgres:postgres@db:5432/styleguide_example_db
27+
- CELERY_BROKER_URL=amqp://guest:guest@rabbitmq:5672//
28+
volumes:
29+
- .:/app
30+
ports:
31+
- "8000:8000"
32+
depends_on:
33+
- db
34+
- rabbitmq
35+
restart: on-failure
36+
37+
celery:
38+
build:
39+
context: .
40+
dockerfile: docker/local.Dockerfile
41+
command: celery -A styleguide_example.tasks worker -l info --without-gossip --without-mingle --without-heartbeat
42+
environment:
43+
- DATABASE_URL=postgres://postgres:postgres@db:5432/styleguide_example_db
44+
- CELERY_BROKER_URL=amqp://guest:guest@rabbitmq:5672//
45+
volumes:
46+
- .:/app
47+
depends_on:
48+
- db
49+
- rabbitmq
50+
restart: on-failure
51+
52+
beats:
53+
build:
54+
context: .
55+
dockerfile: docker/local.Dockerfile
56+
command: celery -A styleguide_example.tasks beat -l info --scheduler django_celery_beat.schedulers:DatabaseScheduler
57+
environment:
58+
- DATABASE_URL=postgres://postgres:postgres@db:5432/styleguide_example_db
59+
- CELERY_BROKER_URL=amqp://guest:guest@rabbitmq:5672//
60+
volumes:
61+
- .:/app
62+
depends_on:
63+
- db
64+
- rabbitmq
65+
restart: on-failure

docker/beats_entrypoint.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
echo "--> Starting beats process"
2+
celery -A styleguide_example.tasks worker -l info --without-gossip --without-mingle --without-heartbeat

docker/celery_entrypoint.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
echo "--> Starting celery process"
2+
celery -A styleguide_example.tasks beat -l info --scheduler django_celery_beat.schedulers:DatabaseScheduler

docker/local.Dockerfile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# This docker file is used for local development via docker-compose
2+
# Creating image based on official python3 image
3+
FROM python:3.10
4+
5+
# Fix python printing
6+
ENV PYTHONUNBUFFERED 1
7+
8+
# Installing all python dependencies
9+
ADD requirements/ requirements/
10+
RUN pip install -r requirements/local.txt
11+
12+
# Get the django project into the docker container
13+
RUN mkdir /app
14+
WORKDIR /app
15+
ADD ./ /app/

docker/production.Dockerfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# This docker file is used for production
2+
# Creating image based on official python3 image
3+
FROM python:3.10
4+
5+
# Installing all python dependencies
6+
ADD requirements/ requirements/
7+
RUN pip install -r requirements/production.txt
8+
9+
# Get the django project into the docker container
10+
RUN mkdir /app
11+
WORKDIR /app
12+
ADD ./ /app/

docker/web_entrypoint.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
echo "--> Starting web process"
2+
gunicorn config.wsgi:application -b 0.0.0.0:80

runtime.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
python-3.9.6
1+
python-3.10.2

0 commit comments

Comments
 (0)