【问题标题】:Not able to access angular webserver inside linux contianer from windows host无法从 Windows 主机访问 linux 容器内的 Angular Web 服务器
【发布时间】:2019-02-14 16:32:20
【问题描述】:

我正在 Windows 10 上托管的 ubuntu 容器内部署一个 Angular 应用程序。这是我的 dockerfile:

FROM ubuntu:latest
COPY app /app
RUN apt-get update
RUN apt-get install -y curl software-properties-common python-pip
RUN curl -sL https://deb.nodesource.com/setup_8.x | bash -
RUN apt-get install -y nodejs
RUN npm install -g @angular/cli
RUN pip install -r /app/requirements.txt
WORKDIR /app/ng
RUN npm install
RUN npm rebuild node-sass
EXPOSE 4200
WORKDIR /
RUN touch start.sh
RUN echo "python /app/server.py &" >> start.sh
RUN echo "cd /app/ng" >> start.sh
RUN echo "ng serve" >> start.sh
RUN chmod +x start.sh
ENTRYPOINT ["/bin/bash"]
CMD ["start.sh"]

我使用

运行图像
docker run -p 4200:4200 --name=test app

现在,问题是,我在 windows 10 上运行这个容器,从 docker 的网络角度来看,我对它不是很熟悉。如果我一直在运行 Linux,那么我可以通过访问 http://localhost:4200 通过任何浏览器轻松访问该应用程序,但在 Windows 10 上似乎并非如此。当我尝试通过 chrome 访问我的应用程序时,我得到了

This site can’t be reached
localhost refused to connect.
ERR_CONNECTION_REFUSED

我尝试在 docker 论坛上搜索并找到 similar issue。在那里接受建议,我尝试通过我的 IPv4 地址访问容器,但失败了。我也尝试使用 docker NAT IP 10.0.75.1 但没有结果。我通过docker inspect test 获得了容器IP,并使用了容器IP 172.17.0.2,但这也没有用。

从主机输出 curl:

E:\app>curl localhost:4200
curl: (7) Failed to connect to localhost port 4200: Connection refused

E:\app>curl 0.0.0.0:4200
curl: (7) Failed to connect to 0.0.0.0 port 4200: Address not available

如果我在容器内卷曲,它会按预期工作

root@97cd2c1e6784:/# curl localhost:4200
<!doctype html>
<html lang="en">
<body>
        <p>Test app</p>
</body>
</html>

如何从 Windows 主机浏览器访问我的 Angular 应用程序?如果您想了解更多信息,请在 cmets 中询问。

【问题讨论】:

  • 你能不能执行这个命令并添加结果,让我们更多地了解 Docker 在 Windows 中是如何运行的? docker-machine ls

标签: docker


【解决方案1】:

经过更多搜索,我得到了答案here。我只需要使用ng serve --host 0.0.0.0 而不是ng serve 来启动角度服务器,这样应用程序就可以在所有网络接口上运行,而不仅仅是循环接口。

【讨论】:

    【解决方案2】:

    已解决:我只需要重启机器 :)。 我有一个类似的问题(但我没有使用 Docker)。当我运行命令“ng serve --host 0.0.0.0”时,我可以将它卷曲在 ubuntu WSL2 实例中(附截图),它工作正常!!,但是我无法使用浏览器(在 Windows 10 build 19043 上)它总是返回“ERR_CONNECTION_REFUSED”。有什么想法吗?

    【讨论】:

      猜你喜欢
      • 2016-07-18
      • 2019-09-08
      • 2019-01-13
      • 1970-01-01
      • 1970-01-01
      • 2010-11-21
      • 2014-11-02
      • 1970-01-01
      • 2021-07-02
      相关资源
      最近更新 更多