スポンサーリンク

Django3.2+PostgreSQL+Nginx with Docker and docker-compose

Development Environment
スポンサーリンク

Development tools to be prepared

  • Docker 20.10
  • docker-compose 1.27.4
  • git 2.25

If you have not installed Docker and docker-compose

MacOS

Install Docker from the official website.

Docker Desktop for Mac

Windows
Setting Up Docker and docker-compose on WSL2
Setting Up Docker and docker-compose on WSL2.Installing WSL2. Installing Docker 20.10.1 . Installing docker-compose 1.27.4

Containers to be created with docker-compose

  • Django 3.2 (Python 3.9.6)
  • PostgreSQL 13.4
  • Nginx 1.20.1

If you want to use MySQL for your DB, please read the following article.

Django+MySQL+Nginx with Docker and docker-compose
Docker and docker-compose make it easy to create a Django+ Nginx+MySQL development environment. Python 3.8.5, MySQL8.0, and Nginx1.18 to install.

Clone the Dockerfile repository

The Dockerfile for Django development is available on github, so please clone it!

GitHub - E-handson/docker-django-postgre
Contribute to E-handson/docker-django-postgre development by creating an account on GitHub.

Start a terminal and run the clone command.

$ git clone https://github.com/E-handson/docker-django-postgre

After the clone is complete, the “docker-django-postgre” directory will be created.
Execute the following command.

$ cd docker-django-postgre

You will find docker-compose.yml under the docker-django-postgre directory.
The docker-compose command can be executed directly under the directory where docker-compose.yml exists.

Running Docker

First, execute the following command to build the Docker service.

$ docker-compose build

Next, docker-compose up

$ docker-compose up

Let’s run the “docker-compose ps” command to see if Docker is running.

$ docker-compose ps
       Name                   Command                         State      Ports
---------------------------------------------------------------------------------------------
docker-django-postgre_app_1  uwsgi --socket :8001 --mod ...    Up         8001/tcp
docker-django-postgre_db_1   docker-entrypoint.sh postgres     Up         0.0.0.0:5433->5432/tcp
docker-django-postgre_web_1  /docker-entrypoint.sh ngin ...    Up         80/tcp, 0.0.0.0:8000->8000/tcp
  • docker-django-postgre_app_1 → Python Container
  • docker-django-postgre_db_1 → PostgreSQL Container
  • docker-django-postgre_web_1 → Nginx Container

If the “State” column is “Up”, the container is up and running.

スポンサーリンク

Installing Django

Install Django in the “app” container

Execute the following command.

$ docker-compose exec app django-admin.py startproject app .

When you are done, the “src” directory will have been created.

$ ll
drwxrwxr-x 8 user user 4096 Dec 24 01:43 .git/
-rw-rw-r-- 1 user user 486 Dec 24 01:43 README.md
drwxrwxr-x 5 user user 4096 Dec 24 01:43 docker/
-rw-rw-r-- 1 user user 719 Dec 24 01:43 docker-compose.yml
drwxr-xr-x 3 root root 4096 Dec 24 01:46 src/

The Django source files have been created under the “src” directory. Restart Docker.

$ docker-compose restart

Accessing localhost:8000 will bring up the Django welcome page.

PostgreSQL configuration

Edit the file “./src/app/settings.py”.

If you want to use PostgreSQL with Django, you need to install a library called “psycopg2-binary”.
In this case, we installed it beforehand when we started the container, so you don’t need to do anything special, but it might be a good idea to keep it in mind.

スポンサーリンク

Before

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}




After

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'django_local',
        'USER': 'django_user',
        'PASSWORD': 'secret',
        'HOST': 'db',
        'POST': '5432'
    }
}
Let’s try to run the migration to see if it connects to PostgreSQL. Execute the following command.
$ docker-compose exec app ./manage.py migrate
Operations to perform:
 Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
 Applying contenttypes.0001_initial... OK
 Applying auth.0001_initial... OK
 Applying admin.0001_initial... OK
     :
     :

If the connection is successful, migrate will be executed and the authentication and other tables will be created in PostgreSQL.
This completes the development environment setup!

コメント