有没有办法使用docker-compose将Docker主机添加到容器的/ etc / hosts?
TL; DR是否有一个简单的方法将docker主机地址分配给docker-compose容器中的主机名?
我正在使用selenium集线器进行testing,在Docker容器中运行。 在我的开发机器上,我可以在http://localhost:8080
看到我的web应用程序,但是在selenium集线器的上下文中, localhost
不可用。 我想要做的是写我的testing,以便我可以使用像http://dockerhost:8080/
的URL来testing应用程序。
起皱的是,这个代码的开发发生在多台机器上,其中一些机器位于不同的networking,不同的地点等等。所以我想这样做,不需要修改docker-compose.yml
文件的每个主机可能运行。
我在我的应用程序中有一个单独的docker-compose.yml文件,里面有selenium集线器和浏览器图像,看起来像这样:
hub: image: selenium/hub ports: - 4444:4444 extra_hosts: dockerhost: 1.2.3.4 firefox: image: selenium/node-firefox links: - hub extra_hosts: dockerhost: 1.2.3.4 chrome: image: selenium/node-chrome links: - hub extra_hosts: dockerhost: 1.2.3.4
这可以工作,但需要docker-compose.yml
文件在每个主机上更新IP地址。 鉴于docker-compose.yml
文件被提交到项目的git
存储库,这是不可接受的。
我想要做的是用docker-compose up
时使用正确的IP地址的东西(一个环境variables?一些特殊的docker-compose
值?)replace硬编码的IP地址。 我在这里看到了许多不同的问题,提出类似的问题,但不完全相同,我尝试了许多不同的方法。
我试过用一个环境variablesDOCKER_HOSTreplace1.2.3.4
,像这样运行它:
# in the docker-compose.yml file extra_hosts: dockerhost: $DOCKER_HOST # run docker-compose up on the command line DOCKER_HOST=1.2.3.4 docker-compose up
它的工作。 但是在启动容器时还有一件事要记住 – 我已经把它抛弃了一次。 我也尝试用bash命令replacedocker-compose.yml
文件中的环境variables来提取IP地址,如下所示:
extra_hosts: dockerhost: $(ip -4 addr show eth0 | grep -Po 'inet \K[\d.]+')
但是,这也许毫不奇怪,是行不通的。
任何其他的想法?