使用 --add-host 或 extra_hosts 和 docker-compose
Using --add-host or extra_hosts with docker-compose
我正在使用 docker-compose
到 运行 一个测试环境,它由大约 5 个不同的容器组成。容器间 links 和共享卷 (volumes-from) 工作得很好。我还向主机公开了一些端口,效果很好。
我缺少的是 link 我的一些真实服务器进入这个环境的方法,没有硬编码 ip 地址。使用 docker run
,您可以使用 --add-host
在 /etc/hosts
文件中添加另一行。有什么方法可以用 docker-compose 做类似的事情吗?
我有好消息:这将在 Compose 1.3 中!
我在当前的 RC (RC1) 中使用它是这样的:
rng:
build: rng
extra_hosts:
seed: 1.2.3.4
tree: 4.3.2.1
https://github.com/compose-spec/compose-spec/blob/master/spec.md#extra_hosts
extra_hosts - Add hostname mappings.
Uses the same values as the docker client --add-host parameter.
extra_hosts:
- "somehost:162.242.195.82"
- "otherhost:50.31.209.229"
An entry with the ip address and hostname will be created in /etc/hosts > inside containers for this service, e.g:
162.242.195.82 somehost
50.31.209.229 otherhost
基本 docker-compose.yml
与 extra hosts:
version: '3'
services:
api:
build: .
ports:
- "5003:5003"
extra_hosts:
- "your-host.name.com:162.242.195.82" #host and ip
- "your-host--1.name.com your-host--2.name.com:50.31.209.229" #multiple hostnames with same ip
创建的容器中/etc/hosts
文件中的内容:
162.242.195.82 your-host.name.com
50.31.209.229 your-host--1.name.com your-host--2.name.com
您可以使用以下命令检查 /etc/hosts
文件:
$ docker-compose -f path/to/file/docker-compose.yml run api bash # 'api' is service name
#then inside container bash
root@f7c436910676:/app# cat /etc/hosts
我正在使用 docker-compose
到 运行 一个测试环境,它由大约 5 个不同的容器组成。容器间 links 和共享卷 (volumes-from) 工作得很好。我还向主机公开了一些端口,效果很好。
我缺少的是 link 我的一些真实服务器进入这个环境的方法,没有硬编码 ip 地址。使用 docker run
,您可以使用 --add-host
在 /etc/hosts
文件中添加另一行。有什么方法可以用 docker-compose 做类似的事情吗?
我有好消息:这将在 Compose 1.3 中!
我在当前的 RC (RC1) 中使用它是这样的:
rng:
build: rng
extra_hosts:
seed: 1.2.3.4
tree: 4.3.2.1
https://github.com/compose-spec/compose-spec/blob/master/spec.md#extra_hosts
extra_hosts - Add hostname mappings. Uses the same values as the docker client --add-host parameter.
extra_hosts:
- "somehost:162.242.195.82"
- "otherhost:50.31.209.229"
An entry with the ip address and hostname will be created in /etc/hosts > inside containers for this service, e.g:
162.242.195.82 somehost
50.31.209.229 otherhost
基本 docker-compose.yml
与 extra hosts:
version: '3'
services:
api:
build: .
ports:
- "5003:5003"
extra_hosts:
- "your-host.name.com:162.242.195.82" #host and ip
- "your-host--1.name.com your-host--2.name.com:50.31.209.229" #multiple hostnames with same ip
创建的容器中/etc/hosts
文件中的内容:
162.242.195.82 your-host.name.com
50.31.209.229 your-host--1.name.com your-host--2.name.com
您可以使用以下命令检查 /etc/hosts
文件:
$ docker-compose -f path/to/file/docker-compose.yml run api bash # 'api' is service name
#then inside container bash
root@f7c436910676:/app# cat /etc/hosts