我在 docker 容器内运行一个 rails 应用程序,但尝试从浏览器访问它时,浏览器显示ERR_CONNECTION_REFUSED
。运行时docker ps
我得到
a086ed5a6c4a rails "bash" 3 days ago Up About an hour 0.0.0.0:3000->3000/tcp rails
然后运行时docker inspect rails
我看到网关是"Gateway": "172.17.0.1"
当我尝试时ping 172.17.0.1
它有效但如果我尝试ping 172.17.0.1:3000
它则无效。
当我尝试时nmap -p 3000 172.17.0.1
我得到
Starting Nmap 6.40 ( http://nmap.org ) at 2015-12-12 15:27 PST
Nmap scan report for 172.17.0.1
Host is up (0.000047s latency).
PORT STATE SERVICE
3000/tcp closed ppp
运行sudo netstat -tulpn
我得到以下内容。
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:5940 0.0.0.0:* LISTEN 1900/teamviewerd
tcp 0 0 127.0.1.1:53 0.0.0.0:* LISTEN 5965/dnsmasq
tcp 0 0 127.0.0.1:53 0.0.0.0:* LISTEN 1195/dnsmasq
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 2910/cupsd
tcp6 0 0 :::5858 :::* LISTEN 4693/docker-proxy
tcp6 0 0 :::4200 :::* LISTEN 4629/docker-proxy
tcp6 0 0 :::6379 :::* LISTEN 4423/docker-proxy
tcp6 0 0 :::8080 :::* LISTEN 4665/docker-proxy
tcp6 0 0 ::1:53 :::* LISTEN 1195/dnsmasq
tcp6 0 0 ::1:631 :::* LISTEN 2910/cupsd
tcp6 0 0 :::5432 :::* LISTEN 4540/docker-proxy
tcp6 0 0 :::3000 :::* LISTEN 3099/docker-proxy
tcp6 0 0 :::3001 :::* LISTEN 4700/docker-proxy
tcp6 0 0 :::3002 :::* LISTEN 6521/docker-proxy
tcp6 0 0 :::7357 :::* LISTEN 4610/docker-proxy
udp 0 0 0.0.0.0:27824 0.0.0.0:* 6091/dhclient
udp 0 0 0.0.0.0:53717 0.0.0.0:* 569/avahi-daemon: r
udp 0 0 0.0.0.0:5353 0.0.0.0:* 3206/chrome
udp 0 0 0.0.0.0:5353 0.0.0.0:* 569/avahi-daemon: r
udp 0 0 0.0.0.0:5699 0.0.0.0:* 5961/dhclient
udp 0 0 127.0.1.1:53 0.0.0.0:* 5965/dnsmasq
udp 0 0 127.0.0.1:53 0.0.0.0:* 1195/dnsmasq
udp 0 0 0.0.0.0:68 0.0.0.0:* 6091/dhclient
udp 0 0 0.0.0.0:68 0.0.0.0:* 5961/dhclient
udp 0 0 0.0.0.0:631 0.0.0.0:* 1336/cups-browsed
udp6 0 0 :::53862 :::* 5961/dhclient
udp6 0 0 :::5353 :::* 569/avahi-daemon: r
udp6 0 0 ::1:53 :::* 1195/dnsmasq
udp6 0 0 :::59093 :::* 569/avahi-daemon: r
udp6 0 0 :::26992 :::* 6091/dhclient
答案1
运行时我必须绑定到 0.0.0.0 。因此我按照此处答案中的说明rails server
使用。rails server -b 0.0.0.0
https://stackoverflow.com/questions/34246254/cant-access-docker-container
答案2
最可能的解释是 Rails 实际上并未在容器中运行。具体来说, 的输出docker ps
表明容器中运行的命令是bash
。这意味着,除非您在容器内手动启动它,否则 Rails 不会运行。