A summary of the procedure for creating a project and application using Django on docker and displaying it on the browser using the development server.
mkdir django
cd django
Create a file that specifies the package to install.
touch requirements.txt
requirements.txt
Django>=3.0,<4.0
psycopg2-binary>=2.8
Install Django and Psycopg.
Psycopg is a tool for using PostgreSQL efficiently. -binary is an OS-only language version, so no compilation is required.
Create a dockerfile to create an image of python3.
touch dockerfile
dockerfile
FROM python:3
ENV PYTHONUNBUFFERED=1
RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/
・ From image name
Pull the specified image from docker hub. (Find local if not on dockerhub)
・ ʻENV variable name` Setting environment variables. Put 1 in the variable PYTHONUNBUFFERED. Make console standard output (stdout) and standard error output (stderr) output immediately when an error occurs (disable buffer, same as python's -u option)
・ WORKDIR directory
Makes the specified directory (/ code) the root directory
· COPY host container
Copy the host requirements.txt under / code / of the container
· Pip install -r file path
Install the package written in the specified file.
The file generally uses requirements.txt. 「-r」 = 「--requirement」
touch docker-compose.yml
docker-compose.yml
version: "3.8"
services:
db:
image: postgres
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8100:8000"
depends_on:
- db
Prepare two containers in the image.
・ Version:" 3.8 "
Specify the version of the description method of docker-compose.yml.
It is necessary to specify because the writing method differs depending on the version.
・ Services
The container to create. Create two, db and web.
・ ʻImages` Specify the image to use. Create a container by pulling postgres from docker hub.
・ ʻEnvironment` Set environment variables. Set DB to postgres. The user name and PW are optional.
· Build directory path
Create a container from the dockerfile in the specified directory.
If no file name is specified, select docker file. If you have a different name, specify the file name as well.
・ Command
A command to execute inside a container. Start the server at localhost: 8000.
Run runserver in manage.py. manage.py is a command line utility. Import useful commands.
· Volumes: -Host: Container
Link (mount) the host directory with the specified directory in the container.
The project folder on the host and / code in the container are synchronized. ** For files that you do not want to synchronize, create .dockerignore ** and describe the file / folder name.
Accessing localhost: 8100 connects to port 8000 in the container.
・ Depends_on
Connect the web container to the db container. Previously, link was used, but v2.0 and above can be connected to the container in docker-compose.yml via a network without any special description. (Does depend_on unnecessary?)
docker-compose run web django-admin startproject mysite
-Docker-compose run [service name] [command]
Create a container with the specified service name and execute the command.
Create an image based on the docker-comopse.yml file in the executed folder, create a container, and start it all at once.
Here, start the web service.
· Django-admin startporject PJ name
Create a PJ.
A directory is created with the specified PJ name (mysite).
File to be created
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py
mysite/settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': 'postgres',
'HOST': 'db',
'PORT': 5432,
}
}
(Reference) Default description
# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
docker-compose up
abridgement
web_1 | Django version 3.1.2, using settings 'mysite.settings'
web_1 | Starting development server at http://0.0.0.0:8100/
web_1 | Quit the server with CONTROL-C.
Start the created container.
The specified server starts up. To see the status, open another bash window and run docker ps
.
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9615ecd4098f django_web "python manage.py ru…" 16 minutes ago Up About a minute 0.0.0.0:8100->8000/tcp django_web_1
If you want it to start in the background, use ʻup -d`.
You can open the page by accessing localhost: 8100 (you can access loaclhost: 8000 in the container)
##Launch bash in a running container
$ docker exec -it django_web_1 bash
root@faef5f41e1c7
##Creating a django app
root@faef5f41e1c7:/code# python manage.py startapp polls
##Check the folder
root@faef5f41e1c7:/code# ls
Dockerfile docker-compose.yml manage.py mysite polls requirements.txt
Succeeded in creating polls in the container. A directory is also created on the host side. (Because volume is specified and synchronized)
I'm running similar commands, startproject and startapp, with django commands.
django-admin startproject mysite
python manage.py startapp polls
There is only one project, and there can be multiple apps.
Only one app called polls is created in the project called mysite.
Since the editor is not installed in the container, exit the container once.
root@faef5f41e1c7:/code# exit
polls/views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, This is Django Polls")
polls/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
mysite/urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
Recognize polls urls.py as path and make it accessible with polls /.
After writing, load the page with localhost: 8100 / polls
.
The display is completed successfully.
Recommended Posts