[Django] Build a Django container (Docker) development environment quickly with PyCharm

Introduction

I usually use PyCharm for business, but it took me a while to find out about building a Django container development environment, so I will post the procedure to build it with the minimum configuration.

Advance preparation

1. 1. Create a Django project

> pip install django
> django-admin startproject sampleApp
> cd sampleApp
sampleApp> python manage.py migrate

2. Creating a Dockerfile

Create a Dockerfile with the following content in the folder where you created the Django app.

FROM python:3.8

#Install if you have the required packages
# RUN apt install ~~~

RUN pip intall django

3. 3. Create docker-compose.yml

Similarly, create the following contents in the folder where you created the Django app.

docker-compose.yml


version: '3'
services:
  app:
    build: .
    ports:
      - "8000:8000"
    volumes:
      - './:/app/sampleApp'
    working_dir: '/app/sampleApp'
    container_name: sampleApp
    privileged: true
    tty: true

That's all for preparation. The directory structure up to this point is as follows.

Django-docker-sample-pycharm #This workspace folder
|   docker-compose.yml
|   Dockerfile
|
\---sampleApp
    |   db.sqlite3
    |   manage.py
    |
    \---sampleApp
        |   asgi.py
        |   settings.py
        |   urls.py
        |   wsgi.py
        |   __init__.py

Remote interpreter settings

Open the workspace folder in PyCharm. Then select [File]-> [Settings]-[Python Interpreter] to open the screen for adding an interpreter. image.png

Select Docker Compose and open the [Server] new creation screen. image.png

Create with OK without changing anything in particular. image.png

Then select [app] from the [Service] pull-down menu and click OK to complete the setting. image.png

It is OK if the remote interpreter is displayed on the interpreter setting screen. I was running "pip install django" in my Dockerfile, so Django is showing up in the list of packages. Let's complete the setting with OK. image.png

This completes the setting "This project uses the container interpreter created from docker-compose.yml directly under the workspace folder": cat2:

Let's do it!

From here, let's run server as usual.

Django settings from [File] → [Settings] image.png

[Add Configurations] image.png

[+]→[Django Server] image.png

Set Name and Host and OK image.png

Run! You can also debug from the button with the bug mark next to it. image.png

If you access <http: // localhost: 8000> from your browser and the Django test page is displayed, it's OK! image.png

If you look at the console, you can see that it is running inside the sampleApp container. image.png

Of course you can also use breakpoints in debug mode! image.png

It's a paid function that you can use the remote interpreter so easily. This will also improve development with containers: cat2:

Recommended Posts

[Django] Build a Django container (Docker) development environment quickly with PyCharm
Build a development environment with Poetry Django Docker Pycharm
[Python] Build a Django development environment with Docker
Build a Django development environment with Docker! (Docker-compose / Django / postgreSQL / nginx)
[Memo] Build a development environment for Django + Nuxt.js with Docker
Build a C language development environment with a container
Build Django + NGINX + PostgreSQL development environment with Docker
Build a Django development environment with Doker Toolbox
[Django] Use VS Code + Remote Containers to quickly build a Django container (Docker) development environment.
Quickly build a Python Django environment with IntelliJ
[DynamoDB] [Docker] Build a development environment for DynamoDB and Django with docker-compose
Create a Todo app with Django ① Build an environment with Docker
Easily build a development environment with Laragon
[Linux] Build a jenkins environment with Docker
[Linux] Build a Docker environment with Amazon Linux 2
I made a development environment for Django 3.0 with Docker, Docker-compose, Poetry
Build the fastest Django development environment with docker-compose
Go (Echo) Go Modules × Build development environment with Docker
Build a Python machine learning environment with a container
Build a Django development environment using pyenv-virtualenv on Mac
How to build a Django (python) environment on docker
Build a machine learning application development environment with Python
Launch Django on a Docker container with docker-compose up
Build a Django environment for Win10 (with virtual space)
Build a lightweight Fast API development environment using Docker
Flutter in Docker-How to build and use a Flutter development environment inside a Docker container
How to run a Django application on a Docker container (development and production environment)
Build a local development environment with WSL + Docker Desktop for Windows + docker-lambda + Python
Build a go environment using Docker
Build a deb file with Docker
Build a Go development environment with VS Code's Remote Containers
Build Mysql + Python environment with docker
Deploy a Django application with Docker
Build PyPy execution environment with Docker
Build a comfortable development environment with VSCode x Remote Development x Pipenv
Build a web application with Django
How to build a python2.7 series development environment with Vagrant
Create a simple Python development environment with VSCode & Docker Desktop
Build a Flask development environment at low cost using Docker
Rebuild Django's development environment with Docker! !! !! !!
Build a Python environment on your Mac with Anaconda and PyCharm
Create a development environment for Go + MySQL + nginx with Docker (docker-compose)
Create a simple Python development environment with VS Code and Docker
Build Jupyter Lab (Python) environment with Docker
Build a Tensorflow environment with Raspberry Pi [2020]
Build a Fast API environment with docker-compose
Get a local DynamoDB environment with Docker
Build a python virtual environment with pyenv
Build a modern Python environment with Neovim
Build NGINX + NGINX Unit + MySQL environment with Docker
Learning history to participate in team application development with Python ~ Build Docker / Django / Nginx / MariaDB environment ~
Build a Python + bottle + MySQL environment with Docker on RaspberryPi3! [Easy construction]
Build a CentOS Linux 8 environment with Docker and start Apache HTTP Server
I tried to build a Mac Python development environment with pythonz + direnv
Steps to build a Django environment with Win10 WSL Ubuntu18.04 + Anaconda + Apache2
Build a LAMP environment on your local Docker
Build a WardPress environment on AWS with pulumi
Build Python development environment with Visual Studio Code
Build a Django environment on Raspberry Pi (MySQL)
Build a python environment with ansible on centos6
Building a Python environment with WLS2 + Anaconda + PyCharm