Edit the config file and run docker-compose up (Django + MySQL ④)

About this article

This article is ** Part.4 ** of articles related to ** Learn how to use Docker through Django + MySQL environment construction ** It corresponds to.

  1. Build a Python virtual environment using venv
  2. Consider the description of Dockerfile
  3. Consider the description of docker-compose.yml
  4. ** Edit the configuration file and execute docker-compose up (this article) **
  5. Adjust container startup timing between dependent services

Introduction

In this article, we'll initialize the django project and try running the development server using the previously created docker-compose.yml.

Make initial settings for django

Creating a local configuration file

Before editing settings.py, create local_settings.py in the same directory as settings.py. If you want to publish to GitHub etc., remove this file from the management target.

config/local_settings.py


SECRET_KEY_LS = '**************************************************'
DB_NAME = "************"
DB_USER = "************"
DB_PASSWORD = "************"

The SECRET KEY in config / settings.py generated at the time of $ django-admin startproject is set to SECRET_KEY_LS, and the description is moved here. For DB_NAME, DB_USER, and DB_PASSWORD, write the same contents as the .env file created last time, and when connecting Django to the DB of the MySQL container, from settings.py as follows. Import and use.

Edit configuration file

Edit settings.py.

config/settings.py


import os
import from .local_settings import SECRET_KEY_LS, DB_NAME, DB_USER, DB_PASSWORD

# (Omission)

SECRET_KEY = SECRET_KEY_LS

First, import the necessary variables, etc., and substitute the previous SECRET_KEY_LS for SECRET_KEY.

Then add 127.0.0.1 and localhost to the same file ʻALLOWED_HOSTS`.

config/settings.py


ALLOWED_HOSTS = ["127.0.0.1", "localhost"]

The contents of "localhost " are the same because the name is resolved to " 127.0.0.1 " by the description in ~ / etc / hosts in the container, but for the sake of usability, both are registered here. I will do it.

The application server itself runs on 0.0.0.0 as specified by command in docker-compose.yml, but by limiting the description here to localhost, ** from other than the host machine. You should be able to flip the connection **. (As long as I tried to connect from another terminal under the same WiFi environment, it seemed that the settings were as intended.)

Finally, edit the items related to the DB connection settings. I have already imported what I need from local_settings.py earlier, so I just apply each one.

config/settings.py


# Database

DATABASES = {
    'default': {
        'ENGINE': "django.db.backends.mysql",
        'NAME': DB_NAME,
        'USER': DB_USER,
        "PASSWORD": DB_PASSWORD,
        'HOST': "db",
        "PORT": "3306"
    }
}

For the HOST part, you can enter the ** MySQL service name ** named in docker-compose.yml, so here it is " db ". PORT was specified by the MySQL default 3306 as before.

This completes the basic settings.

Run the server

With the settings up to this point, everything from ** image and container startup to Django server execution **

$ docker-compose up

You should be able to do it with a single command.

However, when I actually run it, I run into problems under certain conditions. About this, the next article "5. Adjust container startup timing between dependent services" Please refer to.

About operation check

As a log at the time of server execution,

djst_django | Starting development server at http://0.0.0.0:8000/
djst_django | Quit the server with CONTROL-C.

However, due to the setting, the actual operation check is not the displayed http://0.0.0.0:8000/, but ** http://172.0.0.1:8000/ orhttp. It will be done from: // localhost: 8000 /**.

Directory structure

django_starter
    ├── .venv
    │   └── (Abbreviation)
    ├── config
    │     ├── __init__.py
    │     ├── asgi.py
    │     ├── local_settings.py    <- New!
    │     ├── settings.py
    │     ├── urls.py
    │     ├── wait_for_db.py       <-See another article
    │     └── wsgi.py
    ├── mysql
    │     ├── data                 <-Automatically generated
    │     │   └── (Abbreviation)
    │     └── my.cnf
    ├── .env
    ├── docker-compose.yml
    ├── Dockerfile
    ├── manage.py
    └── requirements.txt

Various commands

Finally, write down your favorite commands here.

# $ pwd
# (Abbreviation)/django_starter/

#django related commands( shell,migrate etc.)
$ docker-compose run web python manage.py [command]

#Run development server
$ docker-compose up

#Package update
(.venv) $ pip install [package name]
(.venv) $ pip freeze >> requirements.txt

#Enter the container
$ docker-compose run --rm web /bin/bash #Enter with bash
#Or...
$ docker-compose up -d #After running in the background...
$ docker exec -it [Container name] /bin/bash #Enter with bash

#Stop container
$ docker-compose down

At the end

This completes the purpose of this article group, ** Building a Django + MySQL environment using docker-compose **. After that, I'm going to manage and use Django's template repository with the settings around the front end to my liking.

As an impression that I worked on it, for me as a beginner, it became a chance to learn not only Docker but also basics around terminals and networks **, and I feel that I learned a lot by trying it. .. However, looking at the logs that still flow to the terminal (especially for MySQL), there are a lot of things that need to be improved ... I would like to continue learning and aim for a state where I can use it in a more suitable form for actual battles, such as using it in a production environment, such as Amazon ECS.

Thank you for visiting our website.

.

Recommended Posts

Edit the config file and run docker-compose up (Django + MySQL ④)
Create custom Django commands and run them from the command line
Run Pylint and read the results
Consider the description of Dockerfile (Django + MySQL②)
Create and run embulk config in Jupyter
Difference between docker-compose env_file and .env file
Find it in the procession and edit it
Edit Django shift table HTML and CSS
Consider the description of docker-compose.yml (Django + MySQL ③)
Create a Python image in Django without a dummy image file and test the image upload
Python Memorandum: Refer to the text and edit the file name while copying the target file