Make Django's environment Docker (Docker + Django + Gunicorn + nginx) Part 3

Last time mainly describes docker-compose.yml. This time, I'll describe the settings for the Django application.

It is open to the public below. Please refer to it together with the article. Dockerize Django Environment


Initialize your Django project

  1. Create a Django project
docker-compose run web django-admin.py startproject Arbitrary project name.
┣web
┃ ┗ Arbitrary project name directory
┃  ┗Dockerfile
┃  ┗requirements.txt
┃  ┗manage.py
  1. Connect to PostgreSQL

Arbitrary project name directory/settings.py


# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases

DATABASES = {
     'default': {
         'ENGINE': 'django.db.backends.postgresql',
         'NAME': 'postgres',
         'USER': 'postgres',
         'HOST': 'postgres',
         'PORT': 5432,
     }
 }
  1. WSGI server (application server) settings

Any project name/settings.py


WSGI_APPLICATION = 'Any project name.wsgi.application'
  1. Launch the container

Up to this point, when the settings are complete, launch the container.

docker-compose up

Create an application

  1. Finally, we will start application development. Execute the following command to create an application.
docker-compose run web python manage.py startapp any application name
┣web
┃ ┗ Arbitrary project name directory/
┃  ┗Dockerfile
┃  ┗requirements.txt
┃  ┗manage.py
┃ ┗ Arbitrary application name directory/
┃  ┃  ┗__init__.py
┃  ┃  ┗admin.py
┃  ┃  ┗apps.py
┃  ┃  ┗migrations/
┃  ┃  ┃  ┗__init__.py
┃  ┃  ┗models.py
┃  ┃  ┗tests.py
┃  ┃  ┗views.py

2.admin (enable admin screen) and load static files

Any arbitrary project name directory/settings.py


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]
docker-compose exec web python manage.py migrate

Any arbitrary project name directory/settings.py


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
docker-compose run web python manage.py collectstatic --noinput
docker-compose exec -it web python manage.py createsuperuser

Outlook


Summary

I realized that building an environment with docker does not meet the specifications of each framework and middleware. Now that you can build an environment with the combination of Docker + Django, it seems that you can proceed with the development of individual projects. As I wrote in the outlook, I would like to take on the challenge because it is an advantage of Docker that it is possible to easily configure microservices. If you find any errors in the published information, we would like to investigate and correct them if you can point them out. Thank you for your cooperation.

Recommended Posts

Make Django's environment Docker (Docker + Django + Gunicorn + nginx) Part 2
Make Django's environment Docker (Docker + Django + Gunicorn + nginx) Part 3
Build Django + NGINX + PostgreSQL development environment with Docker
Create Nginx + uWSGI + Python (Django) environment with docker
Docker + Django + React environment construction
[With image diagram] Nginx + gunicorn + Flask converted to Docker [Part 2]
Build a Django development environment with Docker! (Docker-compose / Django / postgreSQL / nginx)
Rebuild Django's development environment with Docker! !! !! !!
Create Python + uWSGI + Nginx environment with Docker
Django + Docker
Nginx setting summary (Flask + Docker + Gunicorn edition)
Build NGINX + NGINX Unit + MySQL environment with Docker
Build Python + django + nginx + MySQL environment using docekr
Make pyautogui [super-appropriate] compatible with multi-display environment Part2
[Python] Build a Django development environment with Docker
Using Flask with Nginx + gunicorn configuration [Local environment]
Django environment construction
Django begins part 1
Django begins part 4
django environment construction
Django + Docker command
Launch a Python web application with Nginx + Gunicorn with Docker
How to build a Django (python) environment on docker
Build a development environment with Poetry Django Docker Pycharm
Create a django environment with docker-compose (MariaDB + Nginx + uWSGI)
Make pyautogui [super-appropriate] compatible with multi-display environment Part1
Make PLEN Control Server compatible with ViVi PLEN2
[Python] Let's make matplotlib compatible with Japanese
Make Django's environment Docker (Docker + Django + Gunicorn + nginx) Part 2
How to make Linux compatible with Japanese keyboard
Make Django's environment Docker (Docker + Django + Gunicorn + nginx) Part 3
Make your Python environment "easy" with VS Code
How to make a shooting game with toio (Part 1)