在Windows上通过Vagrant运行时,按主机名访问docker容器

时间:2016-07-30 08:52:52

标签: windows networking docker vagrant

我正在Windows 7上的Vagrant VM(VirtualBox)中的Ubuntu系统上运行docker。一切正常,我通过docker-compose成功设置了几个容器。容器可以通过映射来自docker-compose文件的服务名称的逻辑主机名来实现自己。

有没有办法能够通过这些逻辑主机名来访问容器,不仅来自容器本身,还来自:

  • 主机运行docker守护程序
  • Vagrant的VM主机(我的Windows运行所有这些)

通过这种方式,我不必为直接从顶级Windows主机和目标容器整合配置执行的应用程序维护单独的配置。

简而言之:'ping mongo'仅在从docker容器内部运行时才到达主机。我很想设置它,以便它可以从ubuntu主机和顶级Windows主机...

我的Vagrantfile:

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure(2) do |config|
  config.vm.box = "ubuntu/trusty64"
  if (/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM) != nil
    config.vm.synced_folder ".", "/vagrant", mount_options: ["dmode=700,fmode=600"]
    # find out a way to move this to environment variables...
    # this is to speed up maven builds on vagrant box by reusing host maven repository
    config.vm.synced_folder "c:\\Users\\woci\\.m2\\repository ", "/home/vagrant/.m2/repository", mount_options: ["dmode=700,fmode=600"]
    # to provide access to configuration
    config.vm.synced_folder "d:\\data\\dev\\pro\\pro-lab\\pro-lab-config", "/pro_config", mount_options: ["dmode=700,fmode=600"]
  end   
#  else
#    config.vm.synced_folder ".", "/vagrant"
#    config.vm.synced_folder "c:\\Users\\woci\\.m2\\repository ", "/extrepo"
#  end
  config.vm.provider "virtualbox" do |v|
    v.memory = 4096
  end
  config.vm.define :dev do |dev|
    dev.vm.network "private_network", ip: "10.100.198.200"
    dev.vm.provision :shell, path: "bootstrap.sh"
    dev.vm.provision :shell,
      inline: 'PYTHONUNBUFFERED=1 ansible-playbook \
        /vagrant/ansible/dev.yml -c local'
  end
  if Vagrant.has_plugin?("vagrant-cachier")
    config.cache.scope = :box
  end
end

搬运工-compose.yml

services:

  configserver:
    build: ./config-server
    image: pro-lab/config-server
    volumes:
      - /pro_config:/config
    ports: 
      - "8888:8888"    

  attachment-manager:
    build: ./attachment-manager
    image: pro-lab/attachment-manager
    ports: 
      - "8080:8080"

  mongo:
    image: mongo
    ports:
      - "27017:27017"

  redis:
    image: redis
    ports:
      - "6379:6379"

  datadog:
    image: datadog/docker-dd-agent:latest
    environment:
      API_KEY: ...  
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - /proc/:/host/proc/:ro 
      - /sys/fs/cgroup/:/host/sys/fs/cgroup:ro
      - /vagrant/docker/dd-agent/conf/:/conf.d/:ro 
      - /vagrant/:/vagrant/ 

0 个答案:

没有答案