-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathdocker-django-tips-tricks
68 lines (48 loc) · 1.44 KB
/
docker-django-tips-tricks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# https://stackoverflow.com/questions/59568111/how-to-make-docker-build-run-python-manage-py-migrate
# https://www.youtube.com/watch?v=vJAfq6Ku4cI&ab_channel=DotJA ---> https://github.com/dotja/django-docker-compose/blob/main/docker-compose.yml
First of all you should not run migrations in your custom Dockerfile. A good practice is creating entrypoint.sh.
This is example entrypoint file:
#!/bin/bash
set -e
echo "${0}: running migrations."
python manage.py makemigrations --merge
python manage.py migrate --noinput
echo "${0}: collecting statics."
python manage.py collectstatic --noinput
cp -rv static/* static_shared/
gunicorn yourapp.wsgi:application \
--env DJANGO_SETTINGS_MODULE=yourapp.production_settings \
--name yourapp \
--bind 0.0.0.0:8000 \
--timeout 600 \
--workers 4 \
--log-level=info \
--reload
Additionally I recommend using docker-compose, which helps to organize your deployment in one place.
Example:
version: '3'
web:
build:
context: .
dockerfile: Dockerfile
command:
- /bin/sh
- '-c'
- '/code/entrypoint.sh'
ports:
- '8000:8000'
volumes:
- '.:/code'
- 'media_volume:/media'
And example Dockerfile
FROM python:3.6.8
RUN apt-get update;
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
ADD requirements.txt /code
ADD entrypoint.sh /code
WORKDIR /code
RUN chmod +x *.sh
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
ADD . /code