Remote debug Django environment created with docker-compose with VS Code

Prepare django environment with docker-compose

Create the required files

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

Try to move

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.

Display the management page

I don't know if I'm really connected to postgres, so let's display the management page.

migration

Create the system tables that Django needs.

docker-compose run web python3 manage.py makemigrations
docker-compose run web python3 manage.py migrate

Created by administrator

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): 

Log in to the administration page

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.

Remote debug settings

As a prerequisite, it is assumed that Remote Development is installed in VS Code.

Add configuration file

  1. Click the Remote Host button (mark at the bottom left of VS Code)
  2. Select Remote-Containers: Add Development Container configuration Files ...
  3. Select From'docker-compose.yaml'
  4. Select 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",
}

Connect to container

Save the workspace around here. If it is not a workspace, an error will occur in the next step.

  1. Click the Remote Host button
  2. Click Remote-Containers: Open Folder in Containers ...
  3. Select the directory where the .devcontainer resides

Remote debugging

  1. Click the run icon on the left side of the screen
  2. Click Create launch.json file
  3. Select 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

Remote debug Django environment created with docker-compose with VS Code
Debug Python with VS Code
[No venv required] The strongest Python development environment created with Remote Containers [VS Code / Docker]
Debug settings in virtual environment when using Pipenv with VS Code
Build the fastest Django development environment with docker-compose
Debug with VS Code using boost python numpy
Use Python in Anaconda environment with VS Code
Make your Python environment "easy" with VS Code
Build a python execution environment with VS Code
Cross-compiling Raspberry Pi and building a remote debugging development environment with VS Code
Python local development environment construction template [Flask / Django / Jupyter with Docker + VS Code]
Create a django environment with docker-compose (MariaDB + Nginx + uWSGI)
[Django] Use VS Code + Remote Containers to quickly build a Django container (Docker) development environment.
Python with VS Code (Windows 10)
Minimal website environment with django
Try running python in a Django environment created with pipenv
Build a Django development environment with Docker! (Docker-compose / Django / postgreSQL / nginx)
Try Fortran with VS Code up to debug settings. [Win10]
Build a Go development environment with VS Code's Remote Containers
Build a Python environment with WSL + Pyenv + Jupyter + VS Code
I "An editor that can't do remote debugging of Django ..." VS Code "That's possible." ~ Docker-compose edition ~
Django development environment construction with Docker-compose + Nginx + uWSGI + MariaDB (macOS edition)
How to develop containers on remote servers with VS Code Remote-Containers
Create a simple Python development environment with VS Code and Docker
Install python with mac vs code
How to build Python and Jupyter execution environment with VS Code
Build jupyter notebook environment with Visual Studio Code (VS Code) Mac version
[DynamoDB] [Docker] Build a development environment for DynamoDB and Django with docker-compose
Comfortable Jupyter Lab (Python) analysis environment created with Docker + VSCode + Remote Container
Prepare a Python virtual environment for your project with venv with VS Code
I was addicted to creating a Python venv environment with VS Code
Steps to create a Python virtual environment with VS Code on Windows
CSS environment created in 10 minutes using Django
Build a Fast API environment with docker-compose
Pass PYTHONPATH in 1 minute with VS Code
Display Japanese graphs with VS Code + matplotlib
VS Code Pylint is annoying with import! !! !! !!
Easy Python data analysis environment construction with Windows10 Pro x VS Code x Docker
VS Code + Azure Functions + Python environment construction procedure
Environment construction with VSCode + Remote Container (Go / Application)
Project cannot be created with Python3.5 (Windows) + django1.7.1
Build Django + NGINX + PostgreSQL development environment with Docker
Build Python development environment with Visual Studio Code
Start Django in a virtual environment with Pipenv
I tried Flask with Remote-Containers of VS Code
[Python] Build a Django development environment with Docker
Build a Django environment with Vagrant in 5 minutes
Create Nginx + uWSGI + Python (Django) environment with docker
Build a Django development environment with Doker Toolbox
A simple to-do list created with Python + Django
Quickly build a Python Django environment with IntelliJ
Use Docker development container conveniently with VS Code
Set up TinyGo development environment for VS Code
I tried to build an environment with WSL + Ubuntu + VS Code in a Windows environment
Edit and debug the code in the Raspberry Pi with VS Code's SSH connection feature
Steps to attach and debug from VS Code to Jupyter Lab on a remote server
Environment construction for those who want to study python easily with VS Code (for Mac)