通过容器IP访问Docker不起作用,但可以通过Localhost访问

发布于 2025-01-31 13:33:11 字数 673 浏览 1 评论 0原文

我一直在学习Docker,我了解到的一件事是,如果端口在容器中公开并且应用程序在该端口正在运行,那么您可以使用http:// container_ip:port port 访问它。已经进行了测试,并且总是有效的。

但是今天,在使用Angular应用程序执行此操作的同时,我发现我无法访问URL,并且不会返回任何内容。 神奇地我想到击中http:// localhost:port and and ant ant ant and ant and ant and and other,这是怎么可能的,为什么它甚至首先发生?

dockerfile

# Stage 1

FROM node as build-step

RUN mkdir -p /app

WORKDIR /app

COPY package.*json /app

RUN npm install

COPY . /app

# RUN npm install -g ts-node

RUN npm run build

# Stage 2

FROM nginx

EXPOSE 80

COPY --from=build-step /app/dist/test-app /usr/share/nginx/html

任何帮助都将受到赞赏。

I have been learning docker and one thing I learned is that if a port is exposed in a container and an application is running at that port then you can access it using http://container_IP:port which I have tested and it always works.

But today while doing the same with an Angular app, I found that I can't access the URL and it does not return anything.
Magically I thought of hitting http://localhost:port and it worked, how is this possible and why did it even happen in the first place?

Dockerfile

# Stage 1

FROM node as build-step

RUN mkdir -p /app

WORKDIR /app

COPY package.*json /app

RUN npm install

COPY . /app

# RUN npm install -g ts-node

RUN npm run build

# Stage 2

FROM nginx

EXPOSE 80

COPY --from=build-step /app/dist/test-app /usr/share/nginx/html

Any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

女皇必胜 2025-02-07 13:33:11

您不应该使用容器IP地址
这就是为什么您要公开端口,因为要映射container端口到localhost port

You shouldn't use container IP address
that's why you are exposing the port because you want to map container port to localhost port

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文