You don't need to be familiar with python or Django because the database server focuses on building a "postgres" environment. To start the project, create a working directory in any location. When using docker-compose, the directory name where the docker-compose.yml file is located is used as a prefix for the container name and volume name, so in the actual environment it is ** meaningful such as the project name. Create a directory name **.
-#Create a directory called django
% mkdir django
-#Move to django directory
% cd django
Don't include unnecessary files to make the django directory the build context.
% vim Dockerfile
Dockerfile
#Specify the image of the execution environment of python3
FROM python:3
#Specify 1 for the environment variable PYTHONUNBUFFERED. You can invalidate the buffer by setting something in this environment variable.
ENV PYTHONUNBUFFERED 1
#Create code directory
RUN mkdir /code
#Move working directory to code directory
WORKDIR /code
#Requirements in the build context.Put txt in code directory
COPY requirements.txt /code/
#You are running a pip installation. pip is a python package tool
# -requirements specified by r.Execute the installation of the package described in txt.
# requirements.txt is created in the build context but describes the package name of the django and Postgres drivers.
RUN pip install -r requirements.txt
#All the contents of the build context/It is placed in the code.
COPY . /code/
% vim requirements.txt
-# version2.0 Django installation
Django==2.0
-#Driver for connecting to postgres with python
psycopg2
docker-compose.yml
version: '3'
services:
db:
image: postgres
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
web:
build: .
command: python3 manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
It is assumed that two containers, "db" and "web", will start in ** services **. Since dots are defined in build in ** web **, build and use the image from the Dockerfile defined earlier. Use "postgres" SQL image for db. ** command ** means the command executed when the container starts. Here, "manage.py" is executed with "python3", and the IP address and port number to listen to are specified as "runserver" that starts a lightweight server for development as an argument. "Manage.py" is a file that is automatically generated when you install Django. However, the command described here will be overwritten if the command is passed when the container is executed. Therefore, if you do not pass the command as an argument, the docker-compose command will be executed. ** valunes ** bind mounts the current directory to / code. ** ports ** is published at 8000 and specified to be transferred to container 8000. It is necessary to match the port number of "runserver" with the port number of the forwarding destination. ** depends_on ** Define a dependency so that the "db" service starts before starting the "web" service.
% docker -compose run web django-admin.py startproject examplepj .
Specify the "web" service defined in yml in the argument of docker-compose run. After that, the command to be executed when the web service container is started. django-admin.py is also created by installing Django. Here, run django-admin.py and create a django project with "start project". The project name is examplepj and the save destination is the current directory. The current directory will be the / code directory specified in the Dockerfile.
% ls -l
You can see that the current current directory and the container's / code directory are bind-mounted.
% vim examplepj/settings.py
settings.py
ALLOWED_HOSTS = ['*']
----abridgement----
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql', #Specify postgresql for ENGINE
'NAME': 'postgres', #Postgres specification
'USER': 'postgres', #Postgres specification
'PASSWORD': 'password',
'HOST': 'db', #docker-Since the container launched by compose can perform name resolution by service name, the HOST can be specified as db.
'PORT': 5432, #Specify the default port 5432 for postgresql
}
}
% docker-compose up -d
Starting django_db_1 ... done
Creating django_web_1 ... done
You can confirm that it is connected with Ip: 8000. Note that if you run a management script such as manage.py, it may fail if you don't run it in a container. Since the execution environment of python is prepared in the container, the script execution of python itself is executed in the container.
-#Create polls application files in your project
% docker-compose run web python3 manage.py startapp polls
-# views.py page changes
% vim polls/views.py
views.py
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
This defines the message that will be displayed when the URL "polls" is accessed.
% vim polls/urls.py
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
It is defined to display the result of the created index method.
% vim examplepj/urls.py
urls.py
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
This will bring up the index page at ip: 8000 / polls /. By adding the source code in this way, you can proceed with development while running django in the container.
% docker-compose stop
Recommended Posts