在 Vagrant 中使用 docker-compose 配置 PyCharm 解释器

Configure PyCharm interpreter with docker-compose inside Vagrant

我有一个基本的 vagrant box,里面有 docker 和 docker-compose 运行。 docker-compose.yaml 有这样的 Web 服务:

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

流浪文件:

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

web 服务使用 Docker 文件,内容为:FROM python:3.5.1-onbuild

我安装了 PyCharm 5.1 专业版 Beta 2(内部版本 145.256.43,2016 年 3 月 11 日)。我想将 pycharm 的解释器配置为运行 web 服务的解释器。
当我尝试这样做时,在 "Configure remote python interpreter" 对话框 window 中,我 select Docker 撰写,然后添加一个新的 Docker 服务器。尝试添加 docker 服务器时,当我输入 vagrant 机器的 ip + 端口 2376(这是输入字段中的默认值)时,出现异常:
screenshot

有没有我遗漏的陷阱?

好的,我终于让它工作了。 这是我所做的:

  1. 进入虚拟机,在/etc/default中,我打开了docker文件。取消注释 DOCKER_OPTS 行并将其更改为:
    DOCKER_OPTS="-H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock"
  2. 在我的 Vagrantfile(定义 docker 守护程序所在的 VM 的文件中 运行,我将同步文件夹更改为

    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"]  
    

    此时重启虚拟机。

  3. 在PyCharm中,我打开了项目,转到文件->设置->项目->项目解释器->添加远程。已选择 Docker-撰写。
  4. 在服务器部分,按新建。输入 API 如下: tcp://192.168.33.69:2375(ip是vm在Vagrantfile中定义的,port是DOCKER_OPTS中定义的)其他保持原样。然后按确定。
  5. 在配置中 - select docker-compose.yaml - 关键部分在这里 - 该文件的路径在主机和虚拟机上应该相同。
  6. 服务名称 - 在我的例子中 - web

编辑:我忘了说 - 我安装了 PyCharm 2016.1

编辑 2017:查看 this or this。 Docker 的较新版本似乎不接受原始答案中的技巧。