当docker容器启动时,将主机/ etc / hosts文件的条目添加到该文件夹中 [英] add entry to host /etc/hosts file when a docker container is started
问题描述
我想能够将主机名添加到我的笔记本电脑/ etc / hosts中,映射到docker容器。由于容器ips不是静态的,所以每个站点我启动/重新启动一个容器,我需要手动更新/ etc / hosts文件,这是非常实用的。
I want to be able to add hostnames to my laptop /etc/hosts that maps to a docker container. Since container ips are not static, each site I start/restart a container I would need to update the /etc/hosts file manually which is not very practical.
寻找一个简单的方法来解决这个问题。
我可以做一些脚本,它监听docker事件,检查容器ip和更新/ etc / hosts,但是我不想重新发明。 (如下所示: https://github.com/discordianfish/docker-spotter )但是我无法真正理解它是如何工作的。
I am looking for a simple way to solve this. I could do some sort of script, which listens to docker events, checks for container ip and updates /etc/hosts but I dont want to reinvent the wheel. (something like this: https://github.com/discordianfish/docker-spotter) but I couldnt really understand how it works.
有人有建议吗?
谢谢。
Anybody has a suggestion?. Thank you.
推荐答案
您可以在您的 docker-compose.yml 使用 extra_hosts
一>。例如:
You can do that using in your docker-compose.yml using extra_hosts
. For instance:
version: '2'
services:
daemon:
build: ./
extra_hosts:
- "example.com:127.0.0.1"
这篇关于当docker容器启动时,将主机/ etc / hosts文件的条目添加到该文件夹中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!