最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

在Docker容器中访问Angular应用程序

运维笔记admin9浏览0评论

在Docker容器中访问Angular应用程序

在Docker容器中访问Angular应用程序

我正在尝试在Docker容器中运行Angular应用程序,但是当我转到http://localhost:4200时我无法访问它。运行该应用程序时,我可以看到通常的角度记录,但是无法从浏览器访问它。

我试图公开端口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 - .pub | apt-key add -
RUN sh -c 'echo "deb [arch=amd64] / 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/[email protected]

# 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;"]
发布评论

评论列表(0)

  1. 暂无评论