スポンサーリンク

Django+MySQL+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

Clone the Dockerfile repository

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

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

First, start a terminal.
Execute the following command.

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

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

$ cd docker-django

You will find docker-compose.yml under the docker-django 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_app_1     uwsgi --socket :8001 --mod ...    Up         8001/tcp
docker-django_db_1      docker-entrypoint.sh --def ...    Up         3306/tcp, 33060/tcp
docker-django_web_1     /docker-entrypoint.sh ngin ...    Up         80/tcp, 0.0.0.0:8000->8000/tcp

docker-django_app_1 → Python Container
docker-django_db_1 → MySQL Container
docker-django_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.

MySQL configuration

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

Before

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




After

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'django_local',
        'USER': 'django_user',
        'PASSWORD': 'secret',
        'HOST': 'db',
        'POST': 3306
    }
}
Let’s try to run the migration to see if it connects to MySQL. 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 MySQL.
This completes the development environment setup!

スポンサーリンク

Docker Container Configuration

Repository configuration

docker-django
├── docker-compose.yml
├── docker
│    ├── mysql
│    │    ├── Dockerfile
│    │    └── my.cnf
│    ├── nginx
│    │    ├── default.conf
│    │    └── uwsgi_params
│    └── python
│       ├── Dockerfile
│       └── requirements.txt
└── src

“src” directory does not exist when you do a git clone.
It will be created after Django is installed.

Container configuration

As you can see in “docker-compose ps”, it consists of three containers.

  1. app container – Python 3.8.5 installed
  2. db container – MySQL8.0 installed
  3. web container – Nginx1.18 installed
app container

This is the container where Django is installed.
The Django source installed in the “/workspace” of the app container is mounted under the local src/ distribution.

You can also describe the package you want to install by pip in the requirements.txt file, so that it will be installed in the container when you rebuild it.

db container

The user and database are created at build time.

DB name:django_local 
DB user:django_user 
password:secret
web container

The configuration is written in default.conf on the local side, and
The file is then mounted on the container.
The UWSGI configuration is also mounted on the web container.

スポンサーリンク

コメント