PyCharm Configuration for Containerized Development
Overview
When using containerized development mode, the app container exposes an SSH server on port 2222. This allows PyCharm to connect to the container as a remote interpreter, providing full IDE support including code completion, debugging, and run configurations.
Prerequisites
- PyCharm Professional (remote interpreters are not available in the Community edition)
- Containerized mode enabled (
containerized = trueinpyproject.toml) - The dev_tool running with the containers started (use "(Re)build the development environment" from the Home tab if this is your first time)
Step 1: Add the Remote Interpreter
- Open Settings (Ctrl + Alt + S)
- Navigate to Project: <project_name> > Python Interpreter
- Click Add Interpreter > On SSH...
SSH Connection
-
Select New and enter the following connection details:
Field Value Host localhostPort 2222Username stratus -
Click Next and enter the password:
stratus
Note:
If PyCharm prompts you about a host key verification, click Yes to accept it. If you have previously connected and the container has been rebuilt, you may need to remove the old host key from your
known_hostsfile. See the Troubleshooting section below.
Python Environment
- Select Existing environment
- Set the interpreter type to Python
- Set the Python path to:
/usr/local/bin/python3(Python 3.11, system) - Click Create to finish
Step 2: Create a Run Configuration
- Go to Run > Edit Configurations... (or click the configurations dropdown in the toolbar)
- Click the + button and select Python
- Configure the following settings:
General
| Field | Value |
|---|---|
| Name | <project_name> |
| Python interpreter | Select the SSH interpreter created in Step 1 |
| Script | manage.py |
| Script parameters | runserver 0.0.0.0:8000 |
Working Directory
- Set the working directory to
/app
Note:
This is the container path where your project is mounted, not the local path on your machine.
Environment Variables
- Click the ... button next to the Environment variables field
-
Add the following variable:
Name Value PYTHONUNBUFFERED1 -
Enable the Paths to ".env" files option and add the path to your local
development.envfile:Replace
<Username>and<path_to_project>with the actual path on your machine. -
Click Apply and then OK
Step 3: Run the Server
- Select the run configuration you just created from the dropdown in the toolbar
- Click the Run button (or press Shift + F10)
- The Django development server will start inside the container and be accessible at
http://localhost:8000
You can also use the Debug button (Shift + F9) to run the server with the PyCharm debugger attached, allowing you to set breakpoints and step through code.
Path Mappings
PyCharm should automatically detect the path mapping between your local project directory and the /app directory inside the container. If it does not, you can configure it manually:
- Open Settings > Project: <project_name> > Python Interpreter
- Click the gear icon next to the interpreter and select Edit...
- Expand SSH Interpreter and check the Path mappings section
-
Add a mapping:
Local Path Remote Path C:\Users\<Username>\<path_to_project>/app
Troubleshooting
Host Key Verification Failed
If you rebuild your containers, the SSH host keys will change and PyCharm may refuse to connect. To resolve this, remove the old entry from your known_hosts file:
- Open the file
%USERPROFILE%\.ssh\known_hostsin a text editor - Find and delete the line containing
[localhost]:2222 - Save the file and try connecting again
Connection Refused on Port 2222
Ensure that the containers are running. Open the dev_tool and verify that the app container is active. If the containers are not running, use "(Re)build the development environment" from the Home tab to start them.
Interpreter Not Finding Packages
If PyCharm reports missing packages that are installed in the container, try the following:
- Open Settings > Project: <project_name> > Python Interpreter
- Click the reload icon to refresh the package list
- If packages are still missing, rebuild the container with the dev_tool to reinstall dependencies