[英]Can't access docker containers from host
I have a simple image for a Rails service with the following Dockerfile: 我有一个使用以下Dockerfile的Rails服务的简单映像:
FROM ruby:2.4.4
MAINTAINER sadzid.suljic@gmail.com
RUN apt-get update && apt-get install -y \
build-essential \
nodejs
RUN mkdir /app
WORKDIR /app
COPY Gemfile Gemfile.lock ./
RUN gem install bundler && bundle install
COPY . ./
EXPOSE 3000
CMD ["rails", "s", "-p", "3000"]
I built the image and ran the container with these commands: 我构建了图像,并使用以下命令运行了容器:
docker build -t chat/users .
docker run -P --name users_service chat/users
I have this output on the host: 我在主机上有此输出:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
23716591e656 chat/users "rails s -p 3000" 6 minutes ago Up 5 minutes 0.0.0.0:32774->3000/tcp users_service
$ lsof -n -i :32774 | grep LISTEN
com.docke 32891 ssuljic 18u IPv4 0x41c034d5d4627f5f 0t0 TCP *:filenet-re (LISTEN)
com.docke 32891 ssuljic 19u IPv6 0x41c034d5d3beb9b7 0t0 TCP [::1]:filenet-re (LISTEN)
$ curl localhost:32774
curl: (52) Empty reply from server
When I run curl localhost:3000
inside the container I get the proper response from my API. 当我在容器内运行
curl localhost:3000
,我会从API中得到正确的响应。
Does anyone know why I can't access the container from my host? 有谁知道为什么我无法从主机访问容器?
I'm using Docker for Mac with this version: 我在此版本的Mac上使用Docker:
$ docker -v
Docker version 18.03.1-ce, build 9ee9f40
Some versions of Rails bind to localhost by default, which explains why you can access from within the container but not from the host (it's viewed as a different host). 默认情况下,某些版本的Rails绑定到localhost,这说明了为什么您可以从容器内部而不是主机(它被视为另一台主机)进行访问。
Adding -b 0.0.0.0
to the CMD
instruction should solve the problem. 在
CMD
指令中添加-b 0.0.0.0
应该可以解决该问题。
Try: 尝试:
docker run -p 3000:3000 --name users_service chat/users
This will map 3000 to 3000 这将映射3000到3000
http://localhost:3000
Looking into this more I think this was either and chrome issue or network issue as I was having the same issue: 进一步研究这个问题,我认为这可能是Chrome问题,也可能是网络问题,因为我遇到的问题是:
Here is how I resolved it: 这是我的解决方法:
Make sure your /etc/hosts
file has 127.0.0.1 localhost
(more than likely it's already there) 确保您的
/etc/hosts
文件具有127.0.0.1 localhost
(很有可能已经存在)
Cleared Cookies and Cached files 清除的Cookie和缓存的文件
Cleared host cache 清除主机缓存
Go to: chrome://net-internals/#dns
click Clear Host Cache 转到:
chrome://net-internals/#dns
点击清除主机缓存
Restarted chrome 重新启动Chrome
Reset Network Adapter Note: This was unintentional so not sure if it was part of the fix or not, but wanted to include it in case. 重置网络适配器注: 这不是故意的,因此不确定是否是此修复程序的一部分,但想以防万一。
Unfortunately I'm not sure which step fixed the problem 不幸的是,我不确定哪一步可以解决问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.