Create Dockerfile, docker-compose.yaml, requirements.txt by referring to Quick Start: Compose and Django.
Dockerfile
FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/
I want to access postgres with a tool, so I publish post. Then postgres was angry that he didn't have the superuser password, so he responded with the environment variable in the error message. Of course, it is better to set the password of the super user, but this time it is a good environment to move, so it is easy.
docker-compose.yaml
services:
db:
image: postgres
environment:
POSTGRES_HOST_AUTH_METHOD: "trust"
ports:
- "5432:5432"
web:
build: .
command: python3 manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
requirements.txt tried to remove the version specification of Django.
requirements.txt
Django
psycopg2
Create a project.
docker-compose run web django-admin.py startproject composeexample .
Set the database. Try to connect to Postgres.
composeexample/settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'HOST': 'db',
'PORT': 5432,
}
}
Start the server!
docker-compose up
Run localhost: 8000
from your browser and if the django page is displayed, it's successful.
I don't know if I'm really connected to postgres, so let's display the management page.
Create the system tables that Django needs.
docker-compose run web python3 manage.py makemigrations
docker-compose run web python3 manage.py migrate
Create a user to log in to the admin page.
docker-compose run web python3 manage.py createsuperuser
Username (leave blank to use 'root'):
Email address: [email protected]
Password:
Password (again):
Running localhost: 8000 / admin
from your browser will bring up the login page.
Try logging in with the superuser you just created.
If you can log in, it's OK.
As a prerequisite, it is assumed that Remote Development is installed in VS Code.
Remote-Containers: Add Development Container configuration Files ...
From'docker-compose.yaml'
web
Modify the resulting devcontainer.json
so that it can be attached to the container for remote debugging.
You don't have to modify the docker-compose.yml
that is created with it.
json:.devcontainer/devcontainer.json
{
"name": "Existing Docker Compose (Extend)",
"dockerComposeFile": [
"../docker-compose.yaml",
"docker-compose.yml"
],
"service": "web",
"workspaceFolder": "/workspace",
"settings": {
"terminal.integrated.shell.linux": "/bin/bash"
},
//Added Docker and python extensions
"extensions": [
"ms-azuretools.vscode-docker",
"ms-python.python"
],
//Stop the container at the end of VS Code
"shutdownAction": "stopCompose",
}
Save the workspace around here. If it is not a workspace, an error will occur in the next step.
Remote-Containers: Open Folder in Containers ...
Docker Debug in Container
Edit the created launch.json
.
json:.vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "django container",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/manage.py",
"console": "integratedTerminal",
"args": [
"runserver",
"--noreload",
"0.0.0.0:8888"
],
"django": true
}
]
}
Since the interpreter is set, create .vscode/settings.json
and make the following settings.
{
"python.pythonPath": "/usr/local/bin/python"
}
Recommended Posts