This article is ** Part.4 ** of articles related to ** Learn how to use Docker through Django + MySQL environment construction ** It corresponds to.
In this article, we'll initialize the django project and try running the development server using the previously created docker-compose.yml
.
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 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.
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.
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 /
**.
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
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
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