Boilerplate for developing and deploying Django/React on Docker
-
Run the stack
docker-compose up
-
Create a new Database
docker exec djangodrfreactdocker_db_1 createdb -Upostgres webapp
-
Create a new App
docker exec -it djangodrfreactdocker_web_1 python manage.py startapp dummyApp
-
Access the development project through http://localhost:8000/
-
Create deploy/local_settings.py (Don't source control it, keep it a secret!). Example:
DEBUG = False SECRET_KEY = 'h5f-7@dl)r)qpd3^gnu=p)7p$i*zeahqk0@gh90xq9c40vu0(#' ALLOWED_HOSTS = ['*'] # set this to your real host in production! DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'webapp', 'USER': 'postgres', 'PASSWORD': 'postgres', 'HOST': 'db', 'PORT': '', } } STATIC_ROOT = '/webapp/static' MEDIA_ROOT = '/webapp/media'
-
Rebuild image and run the stack
docker build -f Dockerfile.prod -t webapp:latest . cd deploy/ && docker-compose up
-
Create database and run migrations
docker exec deploy_db_1 createdb -Upostgres webapp docker exec deploy_web_1 python3 manage.py migrate
-
Access the production project through http://localhost:8700/
You will get an Nginx error 404 because Debug is set to False. You can confirm that this is working either by creating an app or by enabling Debug.
1. How is NGINX connecting to the uwsgi server?
Through Unix socket. This is located at /webapp/app.sock. Current permissions are very open (666).
2. How do I connect to the container for troubleshooting?
You can 'exec' inside a running container as follows:
docker exec -it deploy_web_1 bash
3. Do I need to rebuild the Docker image everytime I update the code?
Since the application source code is mounted as a data volume, you do not need to rebuild the whole image. You only need to restart the docker container as follows:
docker restart deploy_web_1
4. Is this production ready?
Not really, but almost. Check out the security, and data volumes for the database and you'll be all set!
5. Do you I need to install Django on local devbox?
No you don't need to. You can, for example, create your Django app through the Docker container:
docker exec -it djangodrfreactdocker_web_1 python manage.py startapp dummy2
- Django
- Django Rest Framework
- Docker
- Support for React & Webpack static Assets