运行 Angular 应用作为 docker 图片使用 Node js
Running Angular app as docker image using Node js
正在尝试在 docker 和 运行 中构建 angular 应用程序作为我本地使用 Node js 的容器。
我在 Dockerfile 下使用了构建映像,但我不确定 运行ning 时我缺少什么。有人可以指出我吗?
Docker 文件:
FROM node:10.15.3
ENV HOME=/home
WORKDIR $HOME
RUN npm config set strict-ssl false \
&& npm config set proxy http://proxy.xxxxxx.com:8080
COPY package.json .
RUN npm install
使用以下命令成功创建图像
docker build -t example .
我正在尝试使用以下命令 运行 图像,但没有帮助
docker run -p 4201:4200 example
您的 Dockerfile 没有 run/serve 您的应用程序,为此您必须:
- 安装angular/cli
- 复制应用程序
- run/serve 应用
FROM node:10.15.3
RUN npm config set strict-ssl false \
&& npm config set proxy http://proxy.xxxxxx.com:8080
# get the app
WORKDIR /src
COPY . .
# install packages
RUN npm ci
RUN npm install -g @angular/cli
# start app
CMD ng serve --host 0.0.0.0
希望这对您有所帮助。
容器需要一个前台进程运行,然后它不会退出。如果没有,容器会直接退出。
对于您的情况,您需要在 docker build
时 COPY
将您的 nodejs 项目添加到容器中,并在 CMD
中启动项目,例如 CMD [ "npm", "start" ]
。由于web服务器没有退出,那么你的容器也不会退出。
一篇很好的文章 here,供您参考如何对 Node.js 网络应用进行 docker 化。
只需更新您的 Dockerfile 即可实现您的目标,了解更多选项请参阅 here:
# base image
FROM node:12.2.0
RUN npm config set strict-ssl false \
&& npm config set proxy http://proxy.xxxxxx.com:8080
# 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
RUN npm install -g @angular/cli@7.3.9
# add app
COPY . /app
# start app
CMD ng serve --host 0.0.0.0
也为下面的 Dockerfile 试一试!
FROM node:alpine
# get the app
WORKDIR /src
# install packages
RUN npm ci
RUN npm install -g @angular/cli
COPY package.json .
RUN npm install
COPY . .
# start app
CMD ["ng", "serve", "-o"]
正在尝试在 docker 和 运行 中构建 angular 应用程序作为我本地使用 Node js 的容器。
我在 Dockerfile 下使用了构建映像,但我不确定 运行ning 时我缺少什么。有人可以指出我吗?
Docker 文件:
FROM node:10.15.3
ENV HOME=/home
WORKDIR $HOME
RUN npm config set strict-ssl false \
&& npm config set proxy http://proxy.xxxxxx.com:8080
COPY package.json .
RUN npm install
使用以下命令成功创建图像
docker build -t example .
我正在尝试使用以下命令 运行 图像,但没有帮助
docker run -p 4201:4200 example
您的 Dockerfile 没有 run/serve 您的应用程序,为此您必须:
- 安装angular/cli
- 复制应用程序
- run/serve 应用
FROM node:10.15.3
RUN npm config set strict-ssl false \
&& npm config set proxy http://proxy.xxxxxx.com:8080
# get the app
WORKDIR /src
COPY . .
# install packages
RUN npm ci
RUN npm install -g @angular/cli
# start app
CMD ng serve --host 0.0.0.0
希望这对您有所帮助。
容器需要一个前台进程运行,然后它不会退出。如果没有,容器会直接退出。
对于您的情况,您需要在 docker build
时 COPY
将您的 nodejs 项目添加到容器中,并在 CMD
中启动项目,例如 CMD [ "npm", "start" ]
。由于web服务器没有退出,那么你的容器也不会退出。
一篇很好的文章 here,供您参考如何对 Node.js 网络应用进行 docker 化。
只需更新您的 Dockerfile 即可实现您的目标,了解更多选项请参阅 here:
# base image
FROM node:12.2.0
RUN npm config set strict-ssl false \
&& npm config set proxy http://proxy.xxxxxx.com:8080
# 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
RUN npm install -g @angular/cli@7.3.9
# add app
COPY . /app
# start app
CMD ng serve --host 0.0.0.0
也为下面的 Dockerfile 试一试!
FROM node:alpine
# get the app
WORKDIR /src
# install packages
RUN npm ci
RUN npm install -g @angular/cli
COPY package.json .
RUN npm install
COPY . .
# start app
CMD ["ng", "serve", "-o"]