[英]Doesn't connect to localhost when running within a docker container using nodejs app & anguar
I am using Nodejs with angular app .我正在将 Nodejs 与angular 应用程序一起使用。 I deploy angular to my docker desktop.
我将 angular 部署到我的 docker 桌面。 It is running but when I want access from my pc using localhost from PC browser, it does not connect..
它正在运行,但是当我想使用 PC 浏览器中的 localhost 从我的 PC 访问时,它没有连接..
Here is docker image running这是正在运行的 docker 映像
Here is Dockerfile code这是 Dockerfile 代码
FROM node:16.13.1 as build
WORKDIR /app
COPY package.json /app
COPY . .
RUN npm install
RUN npm run build --prod
RUN ls -la dist
FROM nginx:alpine
COPY --from=build /app/dist /usr/share/nginx/html
EXPOSE 4200
When I want access using http://localhost:4200/ from my pc, it is not connecting.当我想从我的电脑使用http://localhost:4200/访问时,它没有连接。 Please help to find out the problem..
请帮忙找出问题..
The EXPOSE
is only part of the solution. EXPOSE
只是解决方案的一部分。 You also need to publish the port when creating the container.您还需要在创建容器时发布端口。 You haven't provided detail about how you start the container, but the proper way in your case would be:
您尚未提供有关如何启动容器的详细信息,但在您的情况下,正确的方法是:
docker run -d -P your_image_name
the -P
flag publishes all exposed ports. -P
标志发布所有公开的端口。
Another way - to explicitly publish specific port:另一种方式 - 显式发布特定端口:
docker run -d --publish 4200:4200 your_image_name
Read more in Docker documentation在Docker 文档中阅读更多信息
nginx:alpine
expose on port 80. nginx:alpine
在端口 80 上暴露。
port 4200 is used for development when you run npm start
or ng serve
.运行
npm start
或ng serve
时,端口 4200 用于开发。
EXPOSE 4200 does nothing and is in this case misleading. EXPOSE 4200 什么都不做,在这种情况下具有误导性。 You use EXPOSE to document what port the image listens on.
您使用 EXPOSE 记录图像侦听的端口。 As Antoine says, nginx listens on port 80, so if you want an EXPOSE statement, it should be
EXPOSE 80
.正如 Antoine 所说, nginx 监听端口 80,所以如果你想要一个 EXPOSE 语句,它应该是
EXPOSE 80
。
Remove the EXPOSE statement since it's misleading删除 EXPOSE 语句,因为它具有误导性
FROM node:16.13.1 as build
WORKDIR /app
COPY package.json /app
COPY . .
RUN npm install
RUN npm run build --prod
RUN ls -la dist
FROM nginx:alpine
COPY --from=build /app/dist /usr/share/nginx/html
When you run your container, you can then map port 80 in the container to port 4200 on your machine like this当您运行容器时,您可以像这样将容器中的 map 端口 80 连接到机器上的端口 4200
docker run -d -p 4200:80 my_image_name
You'll then be able to load your app by going to http://localhost:4200/然后,您可以通过转到 http://localhost:4200/ 来加载您的应用程序
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.