无法从Windows主机访问linux容器内的角度Web服务器

问题描述 投票:1回答:1

我正在一个托管在Windows 10上的ubuntu容器中部署一个角度应用程序。这是我的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主机浏览器访问我的角度应用程序?如果您想了解更多信息,请在评论中提出要求。

docker
1个回答
1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.