官方文档: Add entries to container hosts file (–add-host)
目录
Add entries to container hosts file (–add-host)
添加hosts到容器/etc/hosts文件
官方文档原文
You can add other hosts into a container’s /etc/hosts file by using one or more --add-host flags. This example adds a static address for a host named my-hostname:
你可以添加一条或多条额外的hosts记录到容器的 /etc/hosts 文件中,每条记录需要使用一个 --add-host 标识。以下例子展示了添加一条“my-hostname“对应静态地址的记录:
添加hostname记录
Docker run --add-host的用法
官方文档:docker run (docker container run) options
添加IPv4的hostname记录
注意:docker run --add-host 用法与上述例子中IPv4用法不一致。
- 运行一个容器,添加两条IPv4的host记录
// 运行一个容器,添加两条hostname记录
root@ubuntu22-25:~# docker run -it --rm --add-host www.baidu.com:110.242.68.4 --add-host www.sina.com:123.126.45.205 ubuntu22:latest /bin/bash
- 能够解析IPv4 hostname记录
// 测试hostname记录解析
root@0c848b35a01d:/# ping www.baidu.com -c2
PING www.baidu.com (110.242.68.4): 56 data bytes
64 bytes from 110.242.68.4: icmp_seq=0 ttl=127 time=16.260 ms
64 bytes from 110.242.68.4: icmp_seq=1 ttl=127 time=15.960 ms
--- www.baidu.com ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max/stddev = 15.960/16.110/16.260/0.150 ms
root@0c848b35a01d:/# ping www.sina.com -c2
PING www.sina.com (123.126.45.205): 56 data bytes
64 bytes from 123.126.45.205: icmp_seq=0 ttl=127 time=7.756 ms
64 bytes from 123.126.45.205: icmp_seq=1 ttl=127 time=8.489 ms
--- www.sina.com ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max/stddev = 7.756/8.123/8.489/0.367 ms
添加IPv6地址的用法
官方文档原文
You can wrap an IPv6 address in square brackets:
你可以添加IPv6的地址记录,它需要放置在方括号中:
添加IPv6的hostname记录
注意:docker run --add-host 用法与上述例子中IPv6用法不一致。
启用IPv6的官方文档详见:Enable IPv6 support
- 运行一个容器,添加一条IPv6的hostname记录
root@ubuntu22-25:~# docker run -it --rm --add-host my-hostname:"fdb2:2c26:f4e4:0:21c:42ff:fe1c:d10f" ubuntu22:latest /bin/bash
- 能够解析IPv6 hostname记录
root@36e8404e5b98:/# ping6 my-hostname -c3
PING my-hostname (fdb2:2c26:f4e4:0:21c:42ff:fe1c:d10f): 56 data bytes
64 bytes from my-hostname: icmp_seq=0 ttl=64 time=0.176 ms
64 bytes from my-hostname: icmp_seq=1 ttl=64 time=0.113 ms
64 bytes from my-hostname: icmp_seq=2 ttl=64 time=0.117 ms
--- my-hostname ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max/stddev = 0.113/0.135/0.176/0.029 ms
专用标识 host.docker.internal: host-gateway
官方文档原文
The --add-host flag supports a special host-gateway value that resolves to the internal IP address of the host. This is useful when you want containers to connect to services running on the host machine.
–add-host 标识提供一个专用 主机网关(host-gateway)变量,它可以解析为主机的内部地址。当你想让容器连接主机上运行的服务时,这会很有用。
It’s conventional to use host.docker.internal as the hostname referring to host-gateway. Docker Desktop automatically resolves this hostname, see Explore networking features.
通常,使用host.docker.internal作为主机网关(host-gateway)的主机名称。Docker自动解析这个主机名称,详见Explore networking features
The following example shows how the special host-gateway value works. The example runs an HTTP server that serves a file from host to container over the host.docker.internal hostname, which resolves to the host’s internal IP.
接下的例子展示主机网关(host-gateway)这个值是如何使用的。这个例子在主机上运行一个HTTP 服务,通过host.docker.internal 主机名称向容器提供一个主机文件,host.docker.internal 解析为主机的内部地址。
使用 host.docker.internal:host-gateway
- 主机运行一个HTTP服务,端口8080
root@ubuntu22-25:~# echo "hello from my host!" > ./hello
root@ubuntu22-25:~# python3 -m http.server 8080&
[1] 8454
root@ubuntu22-25:~# Serving HTTP on 0.0.0.0 port 8080 (http://0.0.0.0:8080/) ...
- 运行一个容器,–add-host 使用 host.docker.internal:host-gateway
root@ubuntu22-25:~# docker run -it --rm --add-host host.docker.internal:host-gateway ubuntu22:latest /bin/bash
- 容器内 Ping host.docker.internal 解析为172.17.0.1(主机内部地址/docker0)
root@a6c9738709ae:/# ping host.docker.internal
PING host.docker.internal (172.17.0.1): 56 data bytes
64 bytes from 172.17.0.1: icmp_seq=0 ttl=64 time=0.156 ms
64 bytes from 172.17.0.1: icmp_seq=1 ttl=64 time=0.103 ms
64 bytes from 172.17.0.1: icmp_seq=2 ttl=64 time=0.099 ms
^C--- host.docker.internal ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max/stddev = 0.099/0.119/0.156/0.026 ms
- 容器内eth0的地址是172.17.0.3
root@a6c9738709ae:/# ip a show eth0
32: eth0@if33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.3/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 2001:db8:1::242:ac11:3/64 scope global nodad
valid_lft forever preferred_lft forever
inet6 fe80::42:acff:fe11:3/64 scope link
valid_lft forever preferred_lft forever
root@a6c9738709ae:/# ip route
default via 172.17.0.1 dev eth0
172.17.0.0/16 dev eth0 proto kernel scope link src 172.17.0.3
- 主机内docker0的地址是172.17.0.1
root@ubuntu22-25:~# ip a show docker0
3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:54:0d:28:91 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
valid_lft forever preferred_lft forever
inet6 2001:db8:1::1/64 scope global
valid_lft forever preferred_lft forever
inet6 fe80::1/64 scope link
valid_lft forever preferred_lft forever
inet6 fe80::42:54ff:fe0d:2891/64 scope link
valid_lft forever preferred_lft forever
- 容器内安装curl命令
- curl host.docker.internal的HTTP服务,自动解析为主机内部地址(docker0地址)172.17.0.1
// 安装curl命令
root@a6c9738709ae:/# apt install -y curl
// 请求主机上的http服务
root@a6c9738709ae:/# curl -s host.docker.internal:8080/hello
hello from my host!
- 容器请求主机HTTP服务的连接:172.16.0.3(容器)–>172.17.0.1(主机网关/docker0)
root@ubuntu22-25:~# netstat -na | grep tcp | grep 80
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN
tcp 0 0 172.17.0.1:8080 172.17.0.3:45494 TIME_WAIT
–add-host使用:分隔方式
官方文档原文
The --add-host flag also accepts a : separator, for example:
–add-host 也接受 :分隔方式,如下例。
其他问题-语法错误
注意:docker run 在使用–add-host 过程中,只有:分隔方式可以使用。官方样例中的= 和 IPv6的 [ ],均报语法错误。
- 错误提示信息
root@ubuntu22-25:~# docker run -it --rm --add-host host.docker.internal=host-gateway ubuntu22:latest /bin/bash
invalid argument "host.docker.internal=host-gateway" for "--add-host" flag: bad format for add-host: "host.docker.internal=host-gateway"
See 'docker run --help'.
root@ubuntu22-25:~# docker run --add-host=my-hostname=8.8.8.8 --rm -it alpine
invalid argument "my-hostname=8.8.8.8" for "--add-host" flag: bad format for add-host: "my-hostname=8.8.8.8"
See 'docker run --help'.
- 使用docker的版本信息
root@ubuntu22-25:~# docker run --add-host my-hostname=[2001:db8::33] --rm -it alpine
invalid argument "my-hostname=[2001:db8::33]" for "--add-host" flag: invalid IP address in add-host: "db8::33]"
See 'docker run --help'.
root@ubuntu22-25:~# docker version
Client: Docker Engine - Community
Version: 24.0.7
API version: 1.43
Go version: go1.20.10
Git commit: afdd53b
Built: Thu Oct 26 09:07:41 2023
OS/Arch: linux/amd64
Context: default
Server:
Engine:
Version: 24.0.5
API version: 1.43 (minimum version 1.12)
Go version: go1.20.8
Git commit: a61e2b4
Built: Sat Oct 7 00:14:30 2023
OS/Arch: linux/amd64
Experimental: true
containerd:
Version: v1.6.21
GitCommit: 3dce8eb055cbb6872793272b4f20ed16117344f8
runc:
Version: 1.1.7
GitCommit:
docker-init:
Version: 0.19.0
GitCommit: de40ad0