Node.js 访问Docker容器中的应用程序

Node.js 访问Docker容器中的应用程序,node.js,angular,docker,localhost,dockerfile,Node.js,Angular,Docker,Localhost,Dockerfile,我试图在docker容器中运行Angular应用程序,但在转到时无法访问它。当我运行应用程序时,我可以看到通常的角度日志,但我无法从浏览器访问它 我试图公开端口4200,在运行应用程序“docker run-p 4200:4200 angular example”时指定端口,并在ng serve命令中指定主机和端口,但这些操作都不起作用 我的Dockerfile # base image FROM node:12.2.0 # install chrome for protractor test

我试图在docker容器中运行Angular应用程序,但在转到时无法访问它。当我运行应用程序时,我可以看到通常的角度日志,但我无法从浏览器访问它

我试图公开端口4200,在运行应用程序“docker run-p 4200:4200 angular example”时指定端口,并在ng serve命令中指定主机和端口,但这些操作都不起作用

我的Dockerfile

# base image
FROM node:12.2.0

# install chrome for protractor tests
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'
RUN apt-get update && apt-get install -yq google-chrome-stable

# set working directory
WORKDIR /app

# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH

# install and cache app dependencies
COPY package.json /app/package.json
RUN npm install node-sass
RUN npm install
RUN npm install -g @angular/cli@7.3.9

# add app
COPY . /app

EXPOSE 4200

# start app
CMD ng serve --host 0.0.0.0 --port 4200
我用来运行映像的命令

docker run -p 4200:4200  angular-example
以及运行应用程序时获取的日志

WARNING: This is a simple server for use in testing or debugging Angular applications
locally. It hasn't been reviewed for security issues.

Binding this server to an open connection can result in compromising your application or
computer. Using a different host than the one passed to the "--host" flag might result in
websocket connection issues. You might need to use "--disableHostCheck" if that's the
case.
** Angular Live Development Server is listening on 0.0.0.0:4200, open your browser on http://localhost:4200/ **

Date: 2019-10-13T04:08:51.354Z
Hash: 518bf687971012e084e7
Time: 89920ms
chunk {main} main.js, main.js.map (main) 304 kB [initial] [rendered]
chunk {polyfills} polyfills.js, polyfills.js.map (polyfills) 237 kB [initial] [rendered]
chunk {runtime} runtime.js, runtime.js.map (runtime) 6.08 kB [entry] [rendered]
chunk {styles} styles.js, styles.js.map (styles) 178 kB [initial] [rendered]
chunk {vendor} vendor.js, vendor.js.map (vendor) 7.82 MB [initial] [rendered]
ℹ 「wdm」: Compiled successfully.

当我启动应用程序而不是到达实际应用程序时,我得到了一个错误连接被拒绝。

我最近在一个Angular项目上工作,该项目将其部署在Docker容器上。这是Dockerfile的一个示例

Docker命令:

docker build -t your-app-name .
docker run -itd -p 4200:80 --name=your-app-name your-app-name
在Dockerfile中:

### STAGE 1: Build ###

FROM node:10-alpine as builder

COPY package.json package-lock.json ./

RUN npm set progress=false && npm config set depth 0 && npm cache clean --force

## Storing node modules on a separate layer will prevent unnecessary npm installs at each build
RUN npm i && mkdir /ng-app && cp -R ./node_modules ./ng-app

WORKDIR /ng-app

COPY . .

## Build the angular app in production mode and store the artifacts in dist folder
RUN $(npm bin)/ng build --prod --build-optimizer

### STAGE 2: Setup ###

FROM nginx:1.17.0-alpine

## Copy our default nginx config
COPY nginx/default.conf /etc/nginx/conf.d/

## Remove default nginx website
RUN rm -rf /usr/share/nginx/html/*

## From 'builder' stage copy over the artifacts in dist folder to default nginx public folder
COPY --from=builder /ng-app/dist/your-app-name /usr/share/nginx/html

CMD ["nginx", "-g", "daemon off;"]

我发现了为什么我不能使用localhost访问我的容器。原因是我在使用Docker Quickstart,它将我的容器映射到192.168.99.100。然后我就可以拿到我的申请表了


感谢您的帮助

将建议将cmd格式化为
cmd[“ng”,“service”,“--host”,“0.0.0.0”,“--port”,“4200”,“disableHostCheck”]
也执行
docker exec-it bash
然后
curl localhost:4200
检查它是否在localhost上响应您是否正在运行docker工具箱(通常在Windows 7上)?或者您是否有某种防火墙设置可能会阻塞端口?
docker run-p
命令与服务器的启动日志相匹配,并且它正在0.0.0.0上侦听,这两个版本都很好。@DavidMaze我正在windows 10上使用docker Quickstart。我不记得在我的电脑上安装了防火墙machine@Adiii当我发出curl请求时,我可以看到响应,但我无法使用浏览器访问它,然后查看@DavidMaze评论