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.
> pip install django
> django-admin startproject sampleApp
> cd sampleApp
sampleApp> python manage.py migrate
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
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
Open the workspace folder in PyCharm. Then select [File]-> [Settings]-[Python Interpreter] to open the screen for adding an interpreter.
Select Docker Compose and open the [Server] new creation screen.
Create with OK without changing anything in particular.
Then select [app] from the [Service] pull-down menu and click OK to complete the setting.
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.
This completes the setting "This project uses the container interpreter created from docker-compose.yml directly under the workspace folder": cat2:
From here, let's run server as usual.
Django settings from [File] → [Settings]
[Add Configurations]
[+]→[Django Server]
Run! You can also debug from the button with the bug mark next to it.
If you access <http: // localhost: 8000> from your browser and the Django test page is displayed, it's OK!
If you look at the console, you can see that it is running inside the sampleApp container.
Of course you can also use breakpoints in debug mode!
It's a paid function that you can use the remote interpreter so easily. This will also improve development with containers: cat2: