node.js - 在Docker容器中访问Angular应用程序

标签 node.js angular docker localhost dockerfile

我正在尝试在Docker容器中运行Angular应用程序,但是当我转到http://localhost:4200时我无法访问它。当我运行该应用程序时,我可以看到通常的 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.

启动应用程序而不是到达实际应用程序时,我得到一个ERR_CONNECTION_REFUSED。

最佳答案

我最近从事一个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;"]

关于node.js - 在Docker容器中访问Angular应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58360885/

相关文章:

node.js - 我可以将 nodejs 与 teradata 连接吗(为此可以使用任何 nodejs npm)?

javascript - Angular 2 下拉指令

docker - 如何将变量从.env文件传递给Docker?

docker - 无法与gulp-webserver一起使用docker-compose

node.js - 如何在sqlite nodejs select查询中获取整个结果集?

node.js - 我应该需要 mongoose 模型还是直接从 mongoose 获取

javascript - ESLint - 识别外部(CDN)库的方法

angular - 反转 Angular 2 *ngFor

javascript - Angular2 在组件内部调用外部 JS 文件函数

R 的 pipe() 函数和 Ubuntu 控制台给出不同的结果