简体   繁体   English

无法从主机访问Docker容器

[英]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: 这是我的解决方法:

  1. 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 (很有可能已经存在)

  2. Cleared Cookies and Cached files 清除的Cookie和缓存的文件

  3. Cleared host cache 清除主机缓存

    Go to: chrome://net-internals/#dns click Clear Host Cache 转到: chrome://net-internals/#dns点击清除主机缓存

  4. Restarted chrome 重新启动Chrome

  5. 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.

相关问题 使 rails 和 sidekiq 从不同的 Docker 容器一起工作(但不能使用 docker-compose) - Make rails and sidekiq work together from different Docker containers (but can't use docker-compose) 无法访问docker容器 - can't access docker container Ruby on Rails 无法连接到 Docker 容器中的数据库主机 - Ruby on Rails can't connect to database host in Docker container 无法从VMware Fusion Guest访问Mac主机上的Rails应用 - Can’t access rails app on Mac host from VMware Fusion Guest 在同一主机/不同端口上运行不同的Docker容器 - Running different docker containers on same host/different port 配置Docker卷以在主机和容器之间共享数据 - Configure docker volumes to share data across host and containers 容器需要重建并重新启动。 无法从浏览器重新加载 - Containers need to be rebuilt and restarted. Can't reload from browser Docker 容器无法停止或移除 - 权限被拒绝错误 - Docker Containers can not be stopped or removed - permission denied Error 尝试从另一个容器访问mysql Docker容器时出现“不允许主机'192.XXX.XXX.X'连接到该MySQL服务器”错误 - “Host '192.XXX.XXX.X' is not allowed to connect to this MySQL server” error when attempting to access mysql Docker container from another container Docker Compose:允许图像访问主机资源(如postgresql) - Docker Compose: Allow images to access host resources (like postgresql)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM