PyCharm 2017.3 Help

Docker: Using Docker as a Remote Interpreter

This feature is supported in the Professional edition only.

Introduction

PyCharm integration with Docker allows you to run your applications in the variously configured development environments deployed in Docker containers.

Prerequisites

Make sure that the following prerequisites are met:

  • Docker is installed, as described on the page Docker Docs. 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

Create a Python project QuadraticEquation, add the quadratic_equation.py file and enter the following code:

import math class Solver: def demo(self, a, b, c): d = b ** 2 - 4 * a * c if d > 0: disc = math.sqrt(d) root1 = (-b + disc) / (2 * a) root2 = (-b - disc) / (2 * a) return root1, root2 elif d == 0: return -b / (2 * a) else: return "This equation has no roots" if __name__ == '__main__': solver = Solver() while True: a = int(input("a: ")) b = int(input("b: ")) c = int(input("c: ")) result = solver.demo(a, b, c) print(result)

Configuring Docker as a remote interpreter

Now that we've prepared our example, let's define a Docker-based remote interpreter.

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

Next, 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

In the dialog box that opens, select the Docker option, from the drop-down lists select the Docker server (if the server is missing, click New...) and image name.

Python interpreter path should have the default value:

/help/img/idea/2017.3/py_choose_docker.png

As a result, in the Settings dialog, you should see something like this:

/help/img/idea/2017.3/py_docker_interpreter.png

Apply changes and close the dialog.

Running your application in a Docker container

In the left gutter, next to the main clause, click the /help/img/idea/2017.3/run_from_left_gutter_icon.png button, and choose Run 'quadratic_equation.py' command. You see that your script runs in the Docker container:

/help/img/idea/2017.3/py_run_in_docker_container.png

As you can see, the prefix in the Run tool window shows the container ID.

Debugging your application in a Docker container

Next, let's debug our application. For that, let's put a breakpoint on the line that calculates d, then click the /help/img/idea/2017.3/run_from_left_gutter_icon.png and choose Debug 'quadratic_equation'.

As you see in the Console tab of the Debug tool window, the debugger runs also in the Docker container:

/help/img/idea/2017.3/py_docker_debug.png

But now this container has a different id, and hence - different name. You can see it in the Terminal: type the docker ps command and see the container id and name:

/help/img/idea/2017.3/py_terminal_docker_ps.png

Docker tool window

But is it possible to see all the containers without the Terminal? PyCharm says - yes. You can use the Docker Tool Window as the UI for the Docker command-line client.

If you have configured Docker as a remote interpreter, you will see the Docker tool window button at the bottom side of the main PyCharm window:

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

Click this button and see that the Docker tool window successfully opens:

/help/img/idea/2017.3/py_docker_tool_window.png

Let's look at this tool window more attentively. What do we see here?

  • First, we are connected to a Docker daemon:
    /help/img/idea/2017.3/py_docker_tool_window_connected.png
  • Second, if we open the Run tool window, we'll see that the Docker prefix corresponds to the container ID in the Properties tab of the Docker tool window:
    /help/img/idea/2017.3/py_correspondence_between_docker_and_run_tool_window.png
  • Third, if we open the Debug tool window, we'll see that the Docker prefix (another one!) corresponds to the another container ID in the Properties tab of the Docker tool window:
    /help/img/idea/2017.3/py_correspondence_between_docker_and_debug_tool_window.png
  • And finally, we see the strange names of the containers - they are human-readable and generated by Docker itself.

Summary

Let's summarize what has been done with the help of PyCharm:

  • We created a project and added a Python script.
  • We configured the remote interpreter.
  • We ran and debugged our script in the Docker containers.
  • Finally, we launched the Docker tool window and saw all the details visible in the Terminal.

See Also

Languages, Frameworks and Technologies:

Reference:

Last modified: 15 September 2017