How to connect PyCharm to a python interpreter located inside a Docker container?

前端 未结 10 2171
再見小時候
再見小時候 2021-01-30 03:40

I\'m starting with Docker, but I don\'t know how to configure PyCharm to use a python interpreter located in a container.

It was easy to setup with Vagrant, but there\'s

10条回答
  •  梦毁少年i
    2021-01-30 04:04

    Steps specific to PyCharm Professional Edition 2017.2(however they may work with PyCharm CE)

    Here are a couple steps I took to get my setup working

    Step 1: Environment

    A few assumptions of the structure of your (or anyone who might be reading this) project:

    bleh
    ├── README.md
    ├── api
    │   ├── Dockerfile  <---- this is the one we want to debug
    │   ├── config.example.ini
    │   └── src
    │       ├── __init__.py    <---- this is a pycharm project
    │       ├── __main__.py    <---- this is a pycharm project
    │       └── ...
    ├── proxy
    │   ├── Dockerfile
    │   ├── config.example.ini
    │   └── src
    │       ├── ...
    │       └── ...
    ├── webserver
    │   ├── Dockerfile
    │   ├── config.example.ini
    │   └── src
    │       ├── ...
    │       └── ...
    ├── frontend
    │   ├── Dockerfile
    │   ├── config.example.ini
    │   └── src
    │       ├── ...
    │       └── ...
    ├── db
    │   ├── Dockerfile
    │   ├── ...
    │   └── migrations
    │       ├── ...
    │       └── ...
    └── docker-compose.yml
    
    • Note I'm using bleh as a my project name only as an example.
    • Note We're also going to assume that this project has the absolute location of /Users/myfunkyusername/Projects/bleh.
    • Note Obviously this is all random as far as naming and location is concerned, please make adjustments specific to your system/project
    • Note We're also going to assume that you wish to live debug the api service as shown later in the docker-compose.yml file
    • Note We're also going to assume a content of your api's one and only Dockerfile is as such

      FROM python
      ADD config.example.ini /etc/bleh/config.ini
      RUN chmod +x /usr/bin/bleh
      COPY ./src /usr/bin/bleh
      WORKDIR /usr/bin/bleh
      RUN pip install -r requirements.txt
      CMD ["sh", "-c", "python -m bleh --cfg=/etc/bleh/config.ini"]
      
    • Note We're assuming your one and only docker-compose.yml has these contents

      version: '2'
      services:
      
        api:
          build:
            context: ./api
          depends_on:
            - db
          expose:
            - "8080"
          networks:
            - default
      
        frontend:
          build:
            context: ./frontend
          ports:
              - "80:7000"
          networks:
            - default
      
        webserver:
          build:
            context: ./webserver
          depends_on:
            - frontend
          networks:
            - default
      
        proxy:
          build:
            context: ./proxy
          ports:
            - "80:80"
            - "443:443"
          depends_on:
            - webserver
            - api
          networks:
            - default
      
        db:
          build:
            context: ./db
          expose:
            - "3306"
          networks:
            - default
      
      networks:
        default:
          driver: bridge
      

    Step 2: Create Docker-Machine

    Create docker-machine specifically for the bleh project

    docker-machine create bleh
    

    Step 3: connect remote interpreter

    • From PyCharm / Preferences / Build, Execution, Deployment / Docker click +
    • Select the Docker machine radio button and highlight bleh's docker machine in the pull down
    • Select Apply
    • From PyCharm / Preferences / Project:bleh / Project Interpreter
    • Click the gear icon on the far right of the Project Interpreter field and select Add Remote
    • Select Docker radio button
    • With Server field, select previously created docker machine for this project
    • Select the docker image that holds your desired python interpreter for this project (e.g bleh_api)
    • No change to the Python interpreter path needed
    • Click OK

    Step 4: configure remote debugger

    • From Run / Edit Configurations select + to add a configuration
    • Select Python
    • With Script field, use location of script file on the docker container that will be run (in this example it's /usr/bin/bleh/__main__.py as we're giving the absolute location of our target script)
    • With Script parameters field, supply CLI parameters, if any (mimics the Dockerfile's last CMD command, which is --cfg=/etc/bleh/config.ini)
    • With Python Interpreter field, select your previously established remote python interpreter
    • With Working directory field, select the directory where Script is located within the Docker container (e.g /usr/bin/bleh)
    • With Path mappings field, click the ... and select local (e.g /Users/myfunkyusername/Projects/bleh/api/src) and remote (e.g /usr/bin/bleh) as above
    • With Docker container settings field, click ...
      • ensure you have the correct docker container selected (e.g. bleh_api:latest)
      • Add port binding container/host that mimics what you have the in Dockerfile (e.g 8080/8080 and expose to 0.0.0.0 using the tcp protocol, now I haven't shown what your app structure is, but let's assume that you were sane and within your app are also specifying 8080 as the port where your'e serving your data.
      • Add volume bindings container/host /usr/bin/bleh / /Users/myfunkyusername/Projects/bleh/api/src
      • ensure Network mode (thanks Piotr) is set to _ (e.g bleh_default, you can confirm with docker network ls from within the correct docker-machine)

    Step 5: Bask in the Sun or Bash your head some more

    These are the steps that got me to a working docker and PyCharm setup.

    I don't pretend to be correct in each of these steps. I will gladly update any errors/improvements you find.

提交回复
热议问题