Configure PyCharm interpreter with docker-compose

2019-05-10 06:56发布

I have a basic vagrant box, with docker and docker-compose running in it. The docker-compose.yaml has a web service like this:

web:
  restart: always
  build: .
  ports: 
    - "5000:5000"
  expose:
    - "5000"
  links:
    - postgres:postgres
  volumes:
    - .:/usr/src/app/
  env_file: .env
  command: python manage.py runserver
#below the postgres service is defined

Vagrantfile:

Vagrant.configure(2) do |config|

  config.vm.box = "phusion/ubuntu-14.04-amd64"

  config.vm.network "private_network", ip: "192.168.33.69"

  config.vm.synced_folder ".", "/vagrant_data"
  # provisioning

The web service uses a Dockerfile with content: FROM python:3.5.1-onbuild

I installed the PyCharm 5.1 Professional Edition Beta 2 (build 145.256.43, March 11, 2016). I want to configure the interpreter of pycharm as the same which runs the web service.
When I try to do so, in the "Configure remote python interpreter" dialog window, I select Docker Compose, then I add a new Docker server. When trying to add the docker server, when I put the ip of the vagrant machine + port 2376(this was the default in the input field) I get an exception:
screenshot

Are there any gotchas I am missing?

1条回答
Viruses.
2楼-- · 2019-05-10 07:53

Okay I finally got it to work. Here's what I did:

  1. Went to the VM, and in /etc/default, I opened the docker file. Uncommented the DOCKER_OPTS line and changed it to:
    DOCKER_OPTS="-H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock"
  2. In my Vagrantfile (the one which defines the VM on which the docker daemon is running, I changed the synced folders to

    config.vm.synced_folder ".", "/vagrant", disabled: true # make sure you add this line
    config.vm.synced_folder ".", "/home/georgi/Projects/ipad", # /home/georgi.... is the full path of the project on the host machine. This lines makes sure that the path of the project on the host and on the vm are the same.
        owner: 'vagrant',
        group: 'vagrant',
        mount_options: ["dmode=777", "fmode=777"]
    config.vm.synced_folder "~/.PyCharm2016.1/system/tmp", "/home/georgi/.PyCharm2016.1/system/tmp", 
        owner: 'vagrant',
        group: 'vagrant',
        mount_options: ["dmode=777", "fmode=777"]  
    

    Restart the VM at this point.

  3. In PyCharm, I opened the project, went to File->Settings->Project->Project Interpreter->Add Remote. Selected Docker-Compose.
  4. In the Server section, press New. Enter the API as follows: tcp://192.168.33.69:2375 (the ip is the one defined in the Vagrantfile for the vm. The port is the one defined in the DOCKER_OPTS.) Leave the rest as it is. And press Ok.
  5. In the Configuration - select the docker-compose.yaml - the key part is here - the path of this file should be the same at the host and the vm.
  6. Service name - in my case - web
  7. Voila

EDIT: I forgot to mention - I installed PyCharm 2016.1

EDIT 2017: Check out this or this. Newer versions of Docker seem to not accept the trick in the original answer.

查看更多
登录 后发表回答