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
Django
projectdocker-compose.yml
is located.docker-compose run web django-admin.py startproject Arbitrary project name.
web
directory (directory for Django
) on the host side will be as follows.┣web
┃ ┗ Arbitrary project name directory
┃ ┗Dockerfile
┃ ┗requirements.txt
┃ ┗manage.py
PostgreSQL
settings.py
file under the project directory created in step 1.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,
}
}
WSGI
server (application server) settingsWeb Server Gateway Interface
. As the name implies, it is a generic interface between a web server and a web application.Any project name/settings.py
WSGI_APPLICATION = 'Any project name.wsgi.application'
Up to this point, when the settings are complete, launch the container.
docker-compose up
docker-compose run web python manage.py startapp any application name
web
directory (directory for Django
) on the host side will be as follows.┣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
in
settings.py` looks like this: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
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