Table of contents

  1. PyCharm remote debug in a docker container
  2. How to remote debug python code in a Docker Container with VS Code
  3. Opening remote project in PyCharm

PyCharm remote debug in a docker container

Debugging a Python application running inside a Docker container using PyCharm involves setting up a remote debugger connection. Here's a step-by-step guide on how to achieve this:

  1. Install Necessary Packages: Make sure you have the necessary packages installed in your Docker image to enable debugging. You'll need the pydevd-pycharm package for remote debugging. You can include it in your Dockerfile with:

    RUN pip install pydevd-pycharm
  2. Configure Your Docker Image: To enable remote debugging, you need to start your Python application with the PyCharm debugger server. Modify your Docker entry point or command to include the necessary debugging options:

    ENTRYPOINT ["python", "-m", "pydevd", "--host", "", "--port", "5678", "--wait-for-client", ""]

    Replace with the entry point script of your application.

  3. Expose Debugging Port: Make sure you expose the debugging port (default is 5678) when running your container. You can use the -p flag when running your container:

    docker run -p 5678:5678 your_image_name
  4. Configure Remote Debugging in PyCharm: Open your project in PyCharm and follow these steps:

    • Go to "Run" > "Edit Configurations".
    • Click the "+" button and select "Python Remote Debug".
    • Set the "Name" as you like.
    • Set the "Local Host" as your local machine's IP address or "localhost".
    • Set the "Port" to the port you exposed in the Docker container (5678 by default).
    • Make sure the "Path mappings" section is configured correctly. This tells PyCharm how to map paths between your local system and the Docker container.
  5. Start the Debugger Server in PyCharm: Click on the green "Debug" icon or use the keyboard shortcut (usually Shift + F9) to start the debugger server in PyCharm.

  6. Run the Docker Container: Run your Docker container with the debugging options and port mapping as mentioned earlier.

  7. Trigger a Breakpoint: Place a breakpoint in your Python code in PyCharm where you want to start debugging.

  8. Attach to Docker Container: Once the PyCharm debugger server is running, it will listen for incoming connections. Run your Python script inside the Docker container to trigger the breakpoint. PyCharm should detect the connection and pause at the breakpoint.

Remember that the details might vary depending on your specific environment, settings, and preferences. Always refer to the official documentation for both PyCharm and Docker for any updates or additional information specific to your versions.

How to remote debug python code in a Docker Container with VS Code

Remote debugging Python code running in a Docker container using Visual Studio Code (VS Code) involves setting up a debugging configuration, attaching VS Code's debugger to the Docker container, and ensuring the necessary tools are installed. Here's a step-by-step guide:

  1. Install Required Extensions: Install the "Docker" and "Python" extensions in your VS Code instance if you haven't already.

  2. Install ptvsd in the Docker Container: You'll need to install the ptvsd package (Python Tools for Visual Studio Debugging) in the Docker container to enable debugging. Add it to your requirements.txt or install it manually:

    pip install ptvsd
  3. Update the Python Code: Import and configure ptvsd in your Python code. Add the following lines at the beginning of your script:

    import ptvsd
    # Allow other computers to attach to ptvsd at this IP address and port
    ptvsd.enable_attach(address=('', 5678))
  4. Start the Docker Container: When running the Docker container, you need to expose the debugging port (e.g., 5678) by adding the -p flag:

    docker run -p 5678:5678 -it your_image_name
  5. Create a Debug Configuration in VS Code: Click on the "Run and Debug" icon on the sidebar and create a new configuration. Choose the "Python: Remote Attach" configuration. In the launch.json configuration file that opens, update it to look like this:

        "version": "0.2.0",
        "configurations": [
                "name": "Python: Remote Attach",
                "type": "python",
                "request": "attach",
                "port": 5678,
                "host": "localhost",
                "pathMappings": [
                        "localRoot": "${workspaceFolder}",
                        "remoteRoot": "/path/to/your/code/in/container"

    Replace /path/to/your/code/in/container with the path to your code in the Docker container.

  6. Attach Debugger: Start the debugger by selecting the "Python: Remote Attach" configuration in the dropdown and clicking the green play button or pressing F5.

  7. Set Breakpoints and Debug: Add breakpoints to your code in VS Code, and when you trigger the code to run in the Docker container, the debugger should pause at the breakpoints, allowing you to inspect variables, step through code, etc.

Remember that the ptvsd debugging approach works best with single-threaded or single-process applications. If you're working with a multi-process application or other complexities, you might need to adapt the debugging setup accordingly.

Opening remote project in PyCharm

To open a remote project in PyCharm, you can use the built-in functionality called "Remote Development." This feature allows you to work on your project files located on a remote server, VM, or container, directly from your local PyCharm IDE. Here's how you can set it up:

  1. Install PyCharm Professional: Remote Development is a feature available in PyCharm Professional Edition, so ensure that you have the Professional version installed.

  2. Install SSH and Python on the Remote Server: Make sure your remote server has SSH access enabled and Python installed.

  3. Open a New Project in PyCharm: Open PyCharm and go to File > New Project.

  4. Select "SSH/SFTP" as Project Location: In the "Location" field of the "New Project" dialog, select "SSH/SFTP" and fill in the required details:

    • Host: The hostname or IP address of the remote server.
    • Port: The SSH port (default is 22).
    • Username: Your SSH username on the remote server.
    • Auth type: Select "Password" or "Key pair" based on your remote server's authentication method.
  5. Authenticate and Connect: If you selected "Password," enter your SSH password. If you selected "Key pair," browse and select your private key file. Then, click the "Test Connection" button to ensure the connection is successful.

  6. Choose Project Interpreter: Once connected, you'll be prompted to choose a remote interpreter for the project. You can use the Python interpreter on the remote server or set up a remote interpreter through SSH.

  7. Configure Deployment: In the "New Project" dialog, you can configure the deployment settings to automatically synchronize files between your local machine and the remote server. This allows you to edit files locally and have them automatically updated on the remote server.

  8. Finish and Open Project: Once you've configured the necessary settings, click the "Create" button to create the project. PyCharm will synchronize with the remote server and open the project.

  9. Working with the Project: You can now work on your remote project just like you would with a local project. Edits you make to the files will be reflected on the remote server, and you can run, debug, and test your code using the remote interpreter.

Remember that remote development might introduce some latency due to the network connection, so be patient when interacting with files and running code on the remote server.

Keep in mind that the exact steps might vary depending on your specific setup, but this general process should help you get started with opening a remote project in PyCharm.

More Python Questions

More C# Questions