PyCharm 2017.3 Help

Using Docker Compose as a Remote Interpreter

This feature is supported in the Professional edition only.

Prerequisites

Make sure that the following prerequisites are met:

  • 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 Integration 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 Settings / Preferences Dialog as described in Enabling and Disabling Plugins.

  • Before you start working with Docker, make sure that the Python 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 Settings / Preferences Dialog as described in Enabling and Disabling Plugins.

  • Also, for Windows, right-click the Docker whale icon, choose Settings on the context menu, and in the General page select the Expose daemon... checkbox:
    /help/img/idea/2017.3/py_docker_general_settings.png

Preparing an example

We could have actually repeated the same example as was used for Docker, but for Docker Compose it makes no sense - too simple... So we'll use the other example - a simple Django application. So download this project and open it in PyCharm (File | Open).

For this Django application, we should create two containers: one for a database, and one for the application itself. We'll use the Docker Compose to link the two containers together.

Adding files for Docker and Docker-Compose

In the Project tool window, right-click the project root and choose New | File (Alt+Insert), enter the file name (here Dockerfile) and enter the following code:

FROM python:3.6 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"]

Next, repeat the same steps for the docker-compose.yml file and enter the following code:

version: '2' services: web: build: . ports: - "8000:8000" volumes: - .:/app links: - db db: image: "postgres:9.6" ports: - "5432:5432" environment: POSTGRES_PASSWORD: hunter2

Let's look at the docker-compose.yml file. This file defines 2 services: web and db, and links them together.

Configuring Docker

Now that we've prepared our example, let's configure Docker. To do that, open Settings dialog (Ctrl+Alt+S or click /help/img/idea/2017.3/settings.png on the main toolbar) and click the Docker page under the Build, Execution, Deployment node. Click /help/img/idea/2017.3/add.png to create a Docker server.

Accept the suggested default values:

/help/img/idea/2017.3/py_docker_settings.png

Next, apply changes.

Configuring Docker Compose as a remote interpreter

Let's now define the remote interpreter based on Docker-Compose.

To do it, open the Settings dialog (press Ctrl+Alt+S or click /help/img/idea/2017.3/settings.png on the main toolbar).

Click the Project Interpreter page, on this page click /help/img/idea/2017.3/gear_icon.png next to the Project Interpreter field, and choose Add Remote from the drop-down list:

/help/img/idea/2017.3/py_remote_interpreter_choose.png

Next, in the dialog box that opens, select the Docker-Compose option, from the drop-down lists select the Docker server (if the server is missing, click New...), Docker-Compose service (here web), configuration file (here docker-compose.yml)and image name (here python).

Then you can take your time waiting while PyCharm gets your remote interpreter and installs the dependencies from the requirements.txt file:

/help/img/idea/2017.3/py_docker_pulling.png

Using the Docker tool window

Since we've configured Docker, the Docker tool window button appears at the bottom of PyCharm's main window:

/help/img/idea/2017.3/py_docker_tool_window_button.png

Click this button and see your container running:

/help/img/idea/2017.3/py_docker_running_container.png

Running your application under Docker-Compose

First, as we are executing a Django application, we must run a migration.

To do that, choose Tools | Run 'manage.py' task and enter migrate:

/help/img/idea/2017.3/py_docker_run_manage_py_task.png

Next, create the ordinary Django server run/debug configuration. To do that, on the main menu choose Run | Edit Configurations...; in the dialog box that opens click /help/img/idea/2017.3/add.png and select Django Server:

/help/img/idea/2017.3/py_docker_run_django_server.png

The 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.

Launch this configuration (Run | Run 'RunDjangoApp'):

/help/img/idea/2017.3/py_docker_compose_run.png

To see output in your web browser, go to http://localhost:8000 (in the address bar, change 0.0.0.0 to localhost):

/help/img/idea/2017.3/py_docker_django_app_result.png

Debugging your application under Docker-Compose

gem 'debase' gem 'ruby-debug-ide'

For example:

/help/img/idea/2017.3/rm_debug_gems.png

Next, execute the docker build . command. By the way, PyCharm suggests an intention action in the Gemfile for running the docker build . command (if these gems are missing):

/help/img/idea/2017.3/rm_docker_build_intention.png

Next, let's launch our Django application in the debug mode under Docker Compose. To do that, set a breakpoint (here the breakpoint is set in a template) and on the main menu choose Run | Debug 'RunDjangoApp' , or just click /help/img/idea/2017.3/debug.png next to the run/debug configuration drop-down with the RunDjangoApp run/debug configuration selected:

/help/img/idea/2017.3/py_debug_docker_django.png

The result is shown below:

/help/img/idea/2017.3/py_docker_django_app_debug.png

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 the remote interpreter based on Docker Compose.
  • We ran our Django application in the Docker Compose container.
  • We debugged our Django application's template in the Docker Compose container.
  • Finally, we launched the Docker tool window and saw the details visible in the Terminal.

See Also

Last modified: 15 September 2017