我试图将两个端口从docker容器绑定到其中一个主机接口上当前未使用的IP地址。
我尝试启动容器(基于centos / systemd图像),如下所示:
sudo docker run --privileged -d -p 172.19.5.1:22:22 -p 172.19.5.1:5432:5432 --name test --hostname test -v /sys/fs/cgroup:/sys/fs/cgroup:ro --net cdnet image_name
它失败并显示消息:Error starting userland proxy: listen tcp 172.19.5.1:22: bind: address already in use.
您可以从网络设置中看到此地址应完全有效:
[
{
"Name": "cdnet",
"Id": "c7f58c7f7765b7ee85ffd7638d98c1f73d0abf2393635d26d9634ac94e1c05e9",
"Created": "2018-04-27T20:40:44.542040471Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.19.0.0/16",
"Gateway": "172.19.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"Containers": {},
"Options": {},
"Labels": {}
}
]
并且为了更好的衡量,我在我的机器的主要网络接口上创建了IP:
6: bond0: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 9000 qdisc noqueue state UP qlen 1000
link/ether 0c:c4:7a:8e:f4:f2 brd ff:ff:ff:ff:ff:ff
inet 69.241.118.34/30 brd 69.241.118.35 scope global bond0
valid_lft forever preferred_lft forever
inet 69.241.118.0/30 scope global bond0
valid_lft forever preferred_lft forever
inet 69.241.118.4/30 scope global bond0
valid_lft forever preferred_lft forever
inet 172.19.5.1/25 scope global bond0
valid_lft forever preferred_lft forever
inet 69.241.118.1/30 scope global secondary bond0
valid_lft forever preferred_lft forever
inet 69.241.118.2/30 scope global secondary bond0
valid_lft forever preferred_lft forever
inet 69.241.118.3/30 scope global secondary bond0
valid_lft forever preferred_lft forever
inet6 2001:558:fee8:17a:ec4:7aff:fe8e:f4f2/64 scope global deprecated mngtmpaddr dynamic
valid_lft 1974996sec preferred_lft 0sec
inet6 2001:558:fee8:17a::2/64 scope global
valid_lft forever preferred_lft forever
inet6 fe80::ec4:7aff:fe8e:f4f2/64 scope link
valid_lft forever preferred_lft forever
我没有其他容器正在运行(sudo docker ps -aq
没有返回任何内容)我确定没有其他进程正在使用该地址 - 这里是sudo netstat -anlp
的输出(没有Unix域)套接字部分):
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:199 0.0.0.0:* LISTEN 1406/snmpd
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 1763/dnsmasq
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1397/sshd
tcp 0 0 0.0.0.0:1691 0.0.0.0:* LISTEN 31215/perl
tcp 0 0 0.0.0.0:5662 0.0.0.0:* LISTEN 31215/perl
tcp6 0 0 :::111 :::* LISTEN 1/systemd
tcp6 0 0 :::22 :::* LISTEN 1397/sshd
tcp6 0 920 <IP REDACTED>:22 <IP REDACTED> ESTABLISHED 22357/sshd: <USERNAME REDACTED>
tcp6 0 0 <IP REDACTED>:22 <IP REDACTED> ESTABLISHED 22099/sshd: <USERNAME REDACTED>
udp 0 0 0.0.0.0:53763 0.0.0.0:* 1406/snmpd
udp 0 0 0.0.0.0:57647 0.0.0.0:* 1406/snmpd
udp 0 0 192.168.122.1:53 0.0.0.0:* 1763/dnsmasq
udp 0 0 0.0.0.0:67 0.0.0.0:* 1763/dnsmasq
udp 0 0 0.0.0.0:111 0.0.0.0:* 17684/rpcbind
udp 0 0 0.0.0.0:161 0.0.0.0:* 1406/snmpd
udp 0 0 0.0.0.0:162 0.0.0.0:* 2589/snmptrapd
udp 0 0 0.0.0.0:892 0.0.0.0:* 17684/rpcbind
udp 0 0 0.0.0.0:35179 0.0.0.0:* 1406/snmpd
udp6 0 0 :::111 :::* 17684/rpcbind
udp6 0 0 :::892 :::* 17684/rpcbind
(不用说,编辑的IP不是我试图绑定的那个;它们是我和机器的真实外部IPv6地址。)
我在搜索中找到的发布docker容器端口的所有结果只是试图将端口绑定到localhost,我真的无法弄清楚为什么会破坏它。该系统是CentOS Linux版本7.4.1708(核心版),Docker版本17.05.0-ce,版本89658be
注意:我在Dockerfile中尝试使用EXPOSE 22 5432
和不使用ip addr
,我尝试了不同的IP地址(您可以在上面的-DADHOC
输出中看到)。< / p>
答案 0 :(得分:1)
0.0.0.0表示“所有ipv4接口”,所以是的,它已经在你输出的这一行中使用了:
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1397/sshd