我无法做到的事情:
可以从Ubuntu容器ping默认的docker网络的网关ip,但不能从主机ping容器,我试过看here但这是另一回事。我想从我的主机访问我的容器。
我尝试了什么
所以我用这个创建了一个自定义网络:
docker network create --driver=bridge --subnet=192.168.0.1/24 --ip-range=192.168.0.130/24 --gateway=192.168.0.1 br0
这是新添加的网络
$ docker network ls
NETWORK ID NAME DRIVER
38bcca85e37a bridge bridge
202ace7d3e0c none null
512b3a6dc77f host host
d5f4b00e3281 br0 bridge
并通过inspect命令验证:
$ docker network inspect br0
[
{
"Name": "br0",
"Id": "d5f4b00e3281c27e6d5879ac37463b6d8af728944dc7d3fbc23c25092020c7c7",
"Scope": "local",
"Driver": "bridge",
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "192.168.0.1/24",
"IPRange": "192.168.0.130/24",
"Gateway": "192.168.0.1"
}
]
},
"Containers": {},
"Options": {}
}
]
然后使用以下命令运行我的容器:
docker run -i -d --name=rc --net br0 --ip 192.168.0.150 -v /var/log/restcomm/:/var/log/restcomm/ -e STATIC_ADDRESS="192.168.0.150" -e ENVCONFURL="https://raw.githubusercontent.com/RestComm/Restcomm-Docker/master/scripts/restcomm_env_locally.sh" -p 80:80 -p 443:443 -p 9990:9990 -p 5060:5060 -p 5061:5061 -p 5062:5062 -p 5063:5063 -p 5060:5060/udp -p 65000-65050:65000-65050/udp restcomm/restcomm:latest
然后我登录到正在运行的容器并运行ifconfig命令
$ docker exec -it a09a4c120999 bash
root@8412c266634d:/# ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:c0:a8:00:96
inet addr:192.168.0.150 Bcast:0.0.0.0 Mask:255.255.255.0
inet6 addr: fe80::42:c0ff:fea8:96/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:6 errors:0 dropped:0 overruns:0 frame:0
TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:508 (508.0 B) TX bytes:508 (508.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:2 errors:0 dropped:0 overruns:0 frame:0
TX packets:2 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:142 (142.0 B) TX bytes:142 (142.0 B)
root@a09a4c120999:/# exit
此处的问题仍然是即使此容器的IP设置为 192.168.0.150 ,但它仍然无法ping其IP为192.168.0.115的主机。另外上面的命令我必须快速运行,因为你可以看到容器自行退出,所以我一次又一次重启容器尝试运行一个命令,如果我尝试再次登录而不重新启动,它给出以下错误:
$ docker exec -it e0783d69b96f bash
Error response from daemon: Container e0783d69b96f8e7ff4a17f76d0066b78c4b54a6817de7526a514f42a6af4640e is not running: Exited (1) 1 seconds ago
我也通过列出所有容器来验证这一点
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e0783d69b96f restcomm/restcomm:latest "/sbin/my_init" 5 minutes ago Exited (1) 4 minutes ago rc
我在Windows 10上运行docker并按照here
中的说明操作编辑: 添加此自定义网络后,容器会自行退出,因此尝试添加网络可能有问题,但我在here的官方教程中找到了此命令
所以有2个问题
编辑:
感谢@VonC我通过添加推荐的开关修复了第二个问题,是的,这两个是无关的