Create a Python (Django) learning environment with Docker so that you can debug with VS Code (almost your own procedure memo)

About this article

As the title says. I've built a learning environment for studying Django, so I'll leave a note of the steps here. There is no explanation at all, so I'm not interested in what kind of tools are included or how it works, or I already know it, so let me just build the environment! It is an article for people who say. Or rather, it's my memorandum. I am running in a Windows 10 environment.

Thing you want to do

――I want to study Django --I want to do step execution using a debugger with VS Code --I don't want the Python virtual environment to be annoying, I don't want to pollute the PC environment

⇒ You should create an environment with Docker!

Set up WSL2

If WSL2 (Windows Subsystem for Linux 2) has not been set up, set up WSL2 first. The procedure is as follows. https://docs.microsoft.com/ja-jp/windows/wsl/install-win10

You can use any Linux distribution. I chose Ubuntu.

Install Docker Desktop

Install Docker Desktop. https://www.docker.com/products/docker-desktop There is no problem if you install with the default according to the installer.

Install VS Code

Download and install VS Code. https://azure.microsoft.com/ja-jp/products/visual-studio-code/

Next, install the extension called Remote Containers. https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers

Get the development environment file

Originally, I would write a Dockerfile from here, create a VS Code configuration file, and go crazy, but since I created a set of environment files for learning, please get it from the following. https://github.com/nendo-code/djangostudyenv

Start and debug Django

In VSCode, open the "django studyenv" folder you got earlier open.png

Press the green icon at the bottom left of the VS Code screen. midori.png When the command palette is displayed, select "Remote-Containers: Open Folder in Container". A dialog for selecting a folder will appear, so specify the folder for the "django studyenv" you obtained earlier. remote.png

Environment construction (and installation of VS Code extension) by Docker will start. Click "Starting Dev Container (show log)" to display the progress of environment construction. showlog.png

Now that you have a Django development environment built on your Docker container, let's create a Django project. Open a terminal and execute the following command.

$ cd /code/web
$ django-admin startproject app

This will create a django project called "app". Select Python: Django from the Run menu on the left side of VS Code and press the Start Debugging button. django.png

This will start the application created with django, so try it http://localhost:8000/ To access. If the start page of django is displayed as shown below, it is successful. django_success.png

This completes the environment construction. You can then put breakpoints in your code to stop them, step through them, or develop with a debugger. devug.png

Customize

If you want to customize the environment, you can customize it by modifying the following parts.

・ I want to put VS Code extension on the container side ⇒ Add the extension you want to include in "extensions" of .devcontainer/devcontainer.json.

-I want to change the name of the django project folder, I want to change the runtime options ⇒ Edit the configurations in .vscode/launch.json.

Reference article

https://docs.docker.jp/compose/django.html https://docs.djangoproject.com/ja/3.1/intro/tutorial01/ https://qiita.com/nokonoko_1203/items/33a05c86f359027afb33 https://qiita.com/firedfly/items/00c34018581c6cec9b84

Recommended Posts

Create a Python (Django) learning environment with Docker so that you can debug with VS Code (almost your own procedure memo)
Create a simple Python development environment with VS Code and Docker
Prepare a Python virtual environment for your project with venv with VS Code
A memo that allows you to change Pineapple's Python environment with pyenv
Steps to create a Python virtual environment with VS Code on Windows
[Python] Build a Django development environment with Docker
You can easily create a GUI with Python
Create Nginx + uWSGI + Python (Django) environment with docker
Make your Python environment "easy" with VS Code
Build a python execution environment with VS Code
Python local development environment construction template [Flask / Django / Jupyter with Docker + VS Code]
Memo to create your own Box with Pepper's Python
A memo about building a Django (Python) application with Docker
Remote debug Django environment created with docker-compose with VS Code
Until you create a machine learning environment with Python on Windows 7 and run it
Until you can install your own Python library with pip
[Memo] Build a development environment for Django + Nuxt.js with Docker
Create a VS Code + Docker development environment on a Linux VM
Debug Python with VS Code
Create a simple Python development environment with VSCode & Docker Desktop
Create a web API that can deliver images with Django
Create a Todo app with Django ① Build an environment with Docker
Build a Python environment with WSL + Pyenv + Jupyter + VS Code
[Python] Create a screen for HTTP status code 403/404/500 with Django
Create a Python environment for professionals in VS Code on Windows
Create a program that can generate your favorite images with Selenium
If you know Python, you can make a web application with Django
Try to create a python environment with Visual Studio Code & WSL
Create a C ++ and Python execution environment with WSL2 + Docker + VSCode
Create a virtual environment with Python!
[Python] Based on your favorite desktop image, let's automatically create a terminal / Vim / VS Code theme that suits your feelings with one command.
Build a development environment using Jupyter and Flask with Python in Docker (supports both VS Code / code-server)
I was addicted to creating a Python venv environment with VS Code
[Python] Create a virtual environment with Anaconda
Create Python + uWSGI + Nginx environment with Docker
Easy Python data analysis environment construction with Windows10 Pro x VS Code x Docker
[Machine learning] Create a machine learning model by performing transfer learning with your own data set
VS Code + Azure Functions + Python environment construction procedure
Create a virtual environment with conda in Python
Create a page that loads infinitely with python
Debug with VS Code using boost python numpy
Create a python3 build environment with Sublime Text3
A memo when creating a python environment with miniconda
Use Python in Anaconda environment with VS Code
Quickly build a Python Django environment with IntelliJ
[Docker] Create a jupyterLab (python) environment in 3 minutes!
Build a Python machine learning environment with a container
Create an exe file that works in a Windows environment without Python with PyInstaller
[Django] Use VS Code + Remote Containers to quickly build a Django container (Docker) development environment.
[For super beginners] Python environment construction & scraping & machine learning & practical application that you can enjoy by moving with copy [Let's find a good rental property with SUUMO! ]
A memo to create a virtual environment (venv) before Django
Japanese can be used with Python in Docker environment
Create a python development environment with vagrant + ansible + fabric
[Blender × Python] Create your own function & summary so far
How to build a Django (python) environment on docker
Build a machine learning application development environment with Python
Create a Layer for AWS Lambda Python with Docker
A memo that I touched the Datastore with python
Build a development environment with Poetry Django Docker Pycharm
Memo for building a machine learning environment using Python
Create a django environment with docker-compose (MariaDB + Nginx + uWSGI)