我正在尝试为angular应用创建docker映像,并设法用很少的玩具项目来做到这一点。
我应该提到我在docker和angular上的经验都非常差。
如果我尝试从项目内部的终端运行npm run build,则会成功创建dist文件夹。
据我所知,Angular应用程序的docker文件由两部分组成,第一阶段是构建Angular应用程序,第二阶段是实际创建docker映像。
我将工作目录设置为:
WORKDIR /usr/src/app
设置代理并运行npm后,我和npm运行build。
在我将npm构建生成的所有文件复制到/ usr / src / app / dist路径的最新步骤中,它找不到它,并且构建失败:
COPY --from=builder /usr/src/app/dist/ /usr/share/nginx/html
我认为发生的事情是某个地方无法建立dist文件夹或将其删除。
如果我尝试在没有最后一个cmd(COPY)的情况下运行Dockerfile并运行映像,如果我做了docker exec -it hedgehog-front-end bash
并尝试转到/ usr / src / app /,则为空。
我从命令行运行的docker命令是:
docker build --rm -t hedgehog-fe .
docker run --rm -d --name hedgehog-front-end -p 8888:80/tcp hedgehog-fe
但是很明显它在构建上失败
也尝试在没有--rm标志的情况下运行构建,也失败了。据我所知,--rm标志在容器退出后便将其删除。
完整的Dockerfile是:
FROM node:10-alpine AS builder
WORKDIR /usr/src/app
COPY . .
ENV http_proxy http://192.168.150.242:3128
ENV https_proxy http://192.168.150.242:3128
ENV no_proxy 192.168.151.171,local*,127.0.0.1,172.19.0.3
RUN npm config set proxy http://192.168.150.242:3128
RUN npm config set https-proxy http://192.168.150.242:3128
RUN npm install
RUN npm run build
# also tried with: RUN npm run build --output-path=/usr/src/app/dist but with little success
RUN pwd
FROM nginx:1.15.8-alpine
RUN rm -rf /usr/share/nginx/html/*
COPY --from=builder /usr/src/app/dist/ /usr/share/nginx/html
Docker日志如下:
Sending build context to Docker daemon 2.052MB
Step 1/14 : FROM node:10-alpine AS builder
---> 9dfa73010b19
Step 2/14 : WORKDIR /usr/src/app
---> Using cache
---> 75922f5045ea
Step 3/14 : COPY . .
---> 8605405dd225
Step 4/14 : ENV http_proxy http://192.168.150.242:3128
---> Running in cda1703a159d
Removing intermediate container cda1703a159d
---> c89d7918e24f
Step 5/14 : ENV https_proxy http://192.168.150.242:3128
---> Running in 51597119b85f
Removing intermediate container 51597119b85f
---> 99104b4ceeec
Step 6/14 : ENV no_proxy 192.168.151.171,local*,127.0.0.1,172.19.0.3
---> Running in 92c895a8bbab
Removing intermediate container 92c895a8bbab
---> 1492be836516
Step 7/14 : RUN npm config set proxy http://192.168.150.242:3128
---> Running in a2c0191f05c5
Removing intermediate container a2c0191f05c5
---> 6a26b7e44a3a
Step 8/14 : RUN npm config set https-proxy http://192.168.150.242:3128
---> Running in 514694cb5b9e
Removing intermediate container 514694cb5b9e
---> 65567143581a
Step 9/14 : RUN npm install
---> Running in a7d3375c0265
...
...
...
Removing intermediate container a7d3375c0265
---> 274f7f2fa911
Step 10/14 : RUN npm run build --output-path=/usr/src/app/dist
---> Running in c302f6ffdcbe
> hedgehog@0.0.0 build /usr/src/app
> ng build
Date: 2019-07-25T09:35:46.279Z
Hash: 91334de448ec3f98f32f
Time: 21001ms
chunk {main} main.js, main.js.map (main) 86.7 kB [initial] [rendered]
chunk {polyfills} polyfills.js, polyfills.js.map (polyfills) 240 kB [initial] [rendered]
chunk {runtime} runtime.js, runtime.js.map (runtime) 6.08 kB [entry] [rendered]
chunk {styles} styles.js, styles.js.map (styles) 347 kB [initial] [rendered]
chunk {vendor} vendor.js, vendor.js.map (vendor) 7.38 MB [initial] [rendered]
Removing intermediate container c302f6ffdcbe
---> e686f496c94c
---> e686f496c94c
Step 11/14 : RUN pwd
---> Running in 7fd1c2612469
/usr/src/app
Removing intermediate container 7fd1c2612469
---> 4b5e572e2260
Step 12/14 : FROM nginx:1.15.8-alpine
---> b411e34b4606
Step 13/14 : RUN rm -rf /usr/share/nginx/html/*
---> Using cache
---> 6db439d21fe8
Step 14/14 : COPY --from=builder /usr/src/app/dist/ /usr/share/nginx/html
COPY failed: stat /var/lib/docker/overlay2/d807f3937fc7f50f75a1aad123648730eecfb8c59da3afda634eedf9f0e43c55/merged/usr/src/app/dist: no such file or directory
答案 0 :(得分:0)
好吧,我的.dockerignore文件中有/ dist文件夹
将其从其中删除,现在可以使用