当docker容器启动时,将主机/ etc / hosts文件的条目添加到该文件夹​​中 [英] add entry to host /etc/hosts file when a docker container is started

查看:1433
本文介绍了当docker容器启动时,将主机/ etc / hosts文件的条目添加到该文件夹​​中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想能够将主机名添加到我的笔记本电脑/ 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆