Configure an interpreter using Docker Compose
Prerequisites
Make sure that the following prerequisites are met:
You have a stable Internet connection.
Ensure that you have a stable Internet connection, so that PyCharm can download and run
busybox:latest
. Once you have successfully configured Docker, you can go offline.Docker is installed. You can install Docker on the various platforms, but here we'll use the Windows installation.
Note that you might want to repeat this tutorial on different platforms; then use Docker installations for macOS and Linux (Ubuntu, other distributions-related instructions are available as well).
Before you start working with Docker, make sure that the Docker plugin is enabled. The plugin is bundled with PyCharm and is activated by default. If the plugin is not activated, enable it on the Plugins page of the IDE settings Ctrl+Alt+S as described in Manage plugins.
In the Settings/Preferences dialog Ctrl+Alt+S, select , and select Docker for <your operating system> under Connect to Docker daemon with. For example, if you're on macOS, select Docker for Mac. See more detail in Docker settings.
Note that you cannot install any Python packages into Docker-based project interpreters.
Preparing an example
Let's use a Django application with a PostgreSQL database running in a separate container.
Press Ctrl+Alt+S to open the IDE settings and select Languages & Frameworks | Django. Ensure that the Enable Django Support checkbox is selected. Then click OK to close the project settings.
Download the project archive from GitHub, unpack it, and save it on the local drive.
Select
from the main menu and specify the path to the downloaded project.PyCharm can suggest creating a virtual environment based on the project requirements recorded in the requirements.txt file.
Click OK to create an environment.
With this done, your example is ready, and you can start configuring docker containers. For this Django application, let's create two containers: one for a database, and one for the application itself. The Docker Compose will link the two containers together.
Adding files for Docker and Docker Compose
In the Project tool window, right-click the project root and select from the context menu. Enter the filename: Dockerfile.
Copy and paste the following code in the Dockerfile file:
FROM python:3.6.7 WORKDIR /app # By copying over requirements first, we make sure that Docker will cache # our installed requirements rather than reinstall them on every build COPY requirements.txt /app/requirements.txt RUN pip install -r requirements.txt # Now copy in our code, and run it COPY . /app EXPOSE 8000 CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]Then right-click the project root again, select docker-compose.yml file.
from the context menu, and create theCopy and paste the following code into the docker-compose.yml file.
version: '2' services: web: build: . ports: - "8000:8000" volumes: - .:/app links: - db db: image: "postgres:9.6" ports: - "5432:5432" environment: POSTGRES_PASSWORD: hunter2This file defines two services:
web
anddb
, and links them together.
Configuring Docker
Now that you've prepared the example, let's configure Docker.
Press Ctrl+Alt+S to open the IDE settings and select Build, Execution, Deployment | Docker.
Click to create a Docker server. Accept the suggested default values:
Click OK to save changes.
Configuring Docker Compose as a remote interpreter
Let's now define a remote interpreter based on Docker-Compose.
Ensure that you have downloaded and installed Python on your computer.
Open the Add Python Interpreter dialog by either way:
When you're in the Editor, the most convenient way is to use the Python Interpreter selector in the . Click the selector and select
If you are in the Settings/Preferences dialogCtrl+Alt+S, select . Click the icon and select Add.
In the dialog that opens, select the Docker Compose option, from the drop-down lists select the Docker server, Docker Compose service (here
web
), configuration file (heredocker-compose.yml
) and Python interpreter path (herepython
).Why we've chosen
web
? This choice is explained by the fact, that after configuring a Docker-Compose-based interpreter, we'll be able to create regular run configurations that will alter the behavior of the container we selected. Therefore, if we want to debug the code in a container, that's the one we should select here.Next, wait while PyCharm starts your Docker Compose configuration to scan and index:
Using the Docker tool window
Since you've configured Docker, the Services tool window appears at the bottom of PyCharm's main window. You can click in the gutter next to the services
group to launch db
and web
services.
Refer to Docker for more details on managing docker containers in the Services tool windows.
Configuring database credentials
Modify the DATABASES
section of the settings.py file in your Django project to add database configuration details:
Running your application under Docker-Compose
To execute a Django application, run a migration. Select
from the main menu and entermigrate
:(See Run tasks of manage.py for details.)
Run the Django server run/debug configuration. To do that, from the main menu select Django Server:
. In the dialog that opens click and selectThe only thing you should pay attention to, is that Host field must be set to 0.0.0.0 - to make sure that we listen to requests coming from outside the Docker container.
To run the newly created configuration, select
from the main menu.
To see output in your web browser, go to http://localhost:8000 (in the address bar, change 0.0.0.0 to localhost):
With the run/debug configuration created, you can also debug your application under Docker Compose. To do that, set a breakpoint (for a Django application, you can set it in a template) and from the main menu select , or just click next to the run/debug configuration drop-down with the RunDjangoApp
run/debug configuration selected.
For more details on debugging application in a container , see Debugging in a Docker container.
Summary
Let's summarize what has been done with the help of PyCharm:
We downloaded a Django application from GitHub and opened it.
We added specific Docker Compose files to our project.
We configured a remote interpreter based on Docker Compose.
We ran our Django application in the Docker Compose container.