简体   繁体   English

使用 nodejs app 和 anguar 在 docker 容器中运行时不连接到本地主机

[英]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 documentationDocker 文档中阅读更多信息

nginx:alpine expose on port 80. nginx:alpine在端口 80 上暴露。
port 4200 is used for development when you run npm start or ng serve .运行npm startng 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.

相关问题 docker nodejs容器无法从本地主机连接到mysql容器 - docker nodejs container cant connect to mysql container from localhost docker 容器中的 nodejs 应用程序尝试连接到不同 docker 容器中的 mongodb 时出错 - Error when nodejs app in a docker container tries to connect to mongodb in a different docker container MongoDB + Node.js不与本地主机连接 - Mongodb + nodejs doesn't connect with localhost 使用Node.js的Docker无法启动容器 - Docker with nodejs doesn't start container Docker 容器中的 NodeJS MongoDB API 无法连接到在主机上运行的数据库 - NodeJS MongoDB API in Docker Container can't connect to Database running on Host 将一个Docker容器中的nodejs应用程序连接到Docker Swarm中另一个容器中的mongodb - Connect nodejs App in one docker container to mongodb in another on Docker Swarm 在docker swarm中运行的docker容器中的firebase身份验证错误nodejs应用程序 - firebase authentication error nodejs app in docker container running in docker swarm Docker 容器“连接 ECONNREFUSED localhost” - Docker container 'Connect ECONNREFUSED localhost' 无法从运行localhost的nodejs app连接到mongodb(mongolab) - Failing to connect to mongodb (mongolab) from nodejs app running localhost 使用 NodeJS 连接到 Docker 容器内的 MongoDB 不起作用 - Connect to MongoDB inside a Docker container with NodeJS don't work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM