在docker – 撰写如何创build一个别名/链接到本地主机?
在我的docker-compose文件中,需要多个容器来知道特定容器的主机名,包括这个特定的容器。
链接将无法工作,因为一个容器不能链接到自己。
基本上,我正在寻找的是在docker-compose中别名本地主机的方法。
你应该避免使用链接。 相反,同一个Dockernetworking上的服务可以通过使用服务名称作为DNS名称来查找对方。 使用它来引用您描述的特定容器,包括引用自己的时间。
例如,在下面组成的Docker Compose文件中,如果某个服务是在端口80上服务的Web服务器,则anotherservice
服务服务将能够通过http://someservice/
连接到它,因为它们位于公共networkingthe_net
。
version: '3' services: someservice: image: someserviceimage networks: - the_net anotherservice: image: anotherserviceimage networks: - the_net networks: the_net:
http://somservice/
也可以通过http://somservice/
访问自己。
extra_hosts为我做了诡计。
extra_hosts: – “主机名:127.0.0.1”
从docker撰写文件:
extra_hosts添加主机名映射。 使用与docker客户端–add-host参数相同的值。
extra_hosts: – “somehost:162.242.195.82” – “otherhost:50.31.209.229”具有ip地址和主机名的条目将在此服务的容器内的/ etc / hosts中创build,例如:
其他主机162.242.195.82某些主机50.31.209.229