PyCharm 2022.2 Help

Configure an interpreter using Docker

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 in the Docker documentation.

    You can install Docker on 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).

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

  • 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 Install plugins.

    In the Settings/Preferences dialog (Ctrl+Alt+S), select Build, Execution, Deployment | Docker, 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

Create a Python project QuadraticEquation, add the Solver.py file, and copy and paste 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, let's define a Docker-based remote interpreter.

  1. Ensure that you have downloaded and installed Python on your computer.

  2. Do one of the following:

    • Click the Python Interpreter selector and choose Add New Interpreter.

    • Press Ctrl+Alt+S to open the project Settings/Preferences and go to Project: <project name> | Python Interpreter. Click the Add Interpreter link next to the list of the available interpreters.

  3. Select On Docker from the list of the available interpreter types.

    Adding Docker-based interpreters
  4. In the New Target: Docker dialog, select Pull to pull pre-built images from a Docker registry, and specify python:latest in the Image tag field. Alternatively, you can configure PyCharm to build images locally from a Dockerfile.

    Creating a new Docker target

    Optionally, specify the docker build options.

  5. Wait for PyCharm to connect to the Docker daemon and complete the container introspection.

    Docker container introspection is completed
  6. Next, select an interpreter to use in the Docker container. You can select any virtual environment that is already configured in the container or select a system interpreter.

    Selecting a system interpreter for a Docker target
  7. Click OK to apply changes and close the dialog.

Running your application in a Docker container

In the gutter, next to the main clause, click the Run button, and choose Run 'Solver.py' command. You see that your script runs in the Docker container:

Run context menu

The script is launched in the Run tool window. As you can see, the prefix in the Run tool window shows the container ID.

Running the sample in a Docker container

Switch to the Services tool window to preview the container details. Expand the Containers node and you'll discover the one with the same ID.

Docker container in the Services tool window

You can switch to the Log tab to see the execution results.

Log tab in the Services tool window

Debugging your application in a Docker container

Next, let's debug the application. For that, let's put a breakpoint on the line that calculates d, click Run in the gutter and choose Debug 'Solver'.

Setting a breakpoint and launching the debugger

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

Debugging in a Docker container

You can also discover it in the Services tool window. However, now this container has a different id, and hence - different name. You can switch to the Log tab to see the execution status.

Preview in the Services tool window

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 the following video tutorial for additional information:

Last modified: 19 September 2022