来自多阶段 Dockerfiles 的"npm build"和"COPY --from&q

  • 本文关键字:quot build COPY --from npm Dockerfiles docker dockerfile
  • 更新时间 :
  • 英文 :


我有一个多阶段Dockerfile它将一些部分从第一个图像复制到第二个图像,它总是失败

COPY --from=react-builder /app/build /var/www

有了这个错误

Step 17/22 : COPY --from=react-builder /app/build /var/www
COPY failed: stat /mnt/data/docker/overlay2/901e2feb2323e7fc6d9de0f3e64e84d16d1e935e198bf19f15021af0eb6134d6/merged/app/build: no such file or directory

这是Dockerfile

FROM node:10-slim AS react-builder
ARG REACT_APP_ENV
ENV REACT_APP_ENV $REACT_APP_ENV
WORKDIR app
COPY . .
RUN cat /app/.env | grep = | sort | sed -e 's|REACT_APP_([a-zA-Z_]*)=(.*)|REACT_APP_1=NGINX_REPLACE_1|' > /app/.env.local
RUN npm config set registry=https://npm.company.com 
RUN npm config set "strict-ssl" false
RUN npm install
RUN npm build
FROM nginx:1.15
COPY nginx.conf.template /etc/nginx/conf.d/nginx.conf.template
COPY gcp-deploy/htpasswd /etc/nginx/htpasswd
COPY --from=react-builder /app/.env.local /etc/nginx/conf.d/
RUN NGINX_SUB_FILTER=$(cat /etc/nginx/conf.d/.env.local | grep '=' | sort | sed -e 's/REACT_APP_([a-zA-Z_]*)=(.*)/sub_filter "NGINX_REPLACE_1" "${1}";/') && cat /etc/nginx/conf.d/nginx.conf.template | sed -e "s|LOCATION_SUB_FILTER|$(echo $NGINX_SUB_FILTER)|" | sed -u 's|}"; |}";ntt|g' > /etc/nginx/conf.d/default.conf.template
COPY --from=react-builder /app/build /var/www
# This is a hack around the envsubst nginx config. Because we have `$uri` set
# up, it would replace this as well. Now we just reset it to its original value.
ENV uri $uri
# Default config
ENV REST_URI " https://whatever.wherever.com/"
ENV PORT 80
ENV SERVER_NAME _
CMD ["sh", "-c", "envsubst < /etc/nginx/conf.d/default.conf.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"]

预期行为

运行docker build .只需构建映像,没有问题

实际行为

生成始终失败,使用相同的COPY --from语句。有第二个COPY --from命令总是通过。如果我切换顺序,我仍然会收到相同的错误:

Step 17/22 : COPY --from=react-builder /app/build /var/www
COPY failed: stat /mnt/data/docker/overlay2/901e2feb2323e7fc6d9de0f3e64e84d16d1e935e198bf19f15021af0eb6134d6/merged/app/build: no such file or directory

docker version输出:

Client: Docker Engine - Community
Version:           19.03.5
API version:       1.39 (downgraded from 1.40)
Go version:        go1.12.12
Git commit:        633a0ea
Built:             Wed Nov 13 07:25:41 2019
OS/Arch:           linux/amd64
Experimental:      false
Server: Docker Engine - Community
Engine:
Version:          18.09.0
API version:      1.39 (minimum version 1.12)
Go version:       go1.10.4
Git commit:       4d60db4
Built:            Wed Nov  7 00:19:08 2018
OS/Arch:          linux/amd64
Experimental:     false

docker info输出:


Client:
Debug Mode: false
Server:
Containers: 17
Running: 1
Paused: 0
Stopped: 16
Images: 374
Server Version: 18.09.0
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: b34a5c8af56e510852c35414db4c1f4fa6172339
runc version: 3e425f80a8c931f88e6d94a8c831b9d5aa481657
init version: fec3683
Security Options:
seccomp
Profile: default
Kernel Version: 3.10.0-1062.9.1.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 3.701GiB
Name: devs0211
ID: 25FQ:7G3D:T46W:4DTJ:VV4A:V7JG:DZ53:C2E4:O7JN:WUP6:XWPA:3FX4
Docker Root Dir: /mnt/data/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Registry Mirrors:
https://docker.company.com/
Live Restore Enabled: true
Product License: Community Engine

构建过程的更多详细信息,包括附加ls -ls /app

$ docker build . --no-cache
[sudo] password for sc146082:
Your password will expire in 4 day(s).
Sending build context to Docker daemon  3.561MB
Step 1/23 : FROM node:10-slim as react-builder
---> 92eff963766f
Step 2/23 : ARG REACT_APP_ENV
---> Running in 13e997164e2c
Removing intermediate container 13e997164e2c
---> 8edcfa0931a8
Step 3/23 : ENV REACT_APP_ENV $REACT_APP_ENV
---> Running in 17fa11f08e3e
Removing intermediate container 17fa11f08e3e
---> a906714bef73
Step 4/23 : WORKDIR /app
---> Running in cbc94fc1faa8
Removing intermediate container cbc94fc1faa8
---> 10eaff3f4774
Step 5/23 : COPY . .
---> 567cfa8797d9
Step 6/23 : RUN cat /app/.env | grep = | sort | sed -e 's|REACT_APP_([a-zA-Z_]*)=(.*)|REACT_APP_1=NGINX_REPLACE_1|' > /app/.env.local
---> Running in c28d9688f19e
Removing intermediate container c28d9688f19e
---> 65dd918e5162
Step 7/23 : RUN npm config set registry=https://npm.company.com 
---> Running in f83b8b73b4e8
Removing intermediate container f83b8b73b4e8
---> d36d51e3cabb
Step 8/23 : RUN npm config set "strict-ssl" false
---> Running in 1f65e71e350b
Removing intermediate container 1f65e71e350b
---> d297ff074f44
Step 9/23 : RUN npm install
---> Running in 09a445362f73
....
added 1696 packages from 978 contributors in 42.078s
55 packages are looking for funding
run `npm fund` for details
Removing intermediate container 09a445362f73
---> c30a48bda924
Step 10/23 : RUN npm build
---> Running in be3f6a0a64ea
npm WARN build `npm build` called with no arguments. Did you mean to `npm run-script build`?
Removing intermediate container be3f6a0a64ea
---> 14a5a88aeeb1
Step 11/23 : RUN pwd
---> Running in 1b7603116844
/app
Removing intermediate container 1b7603116844
---> 0579c1f467a8
Step 12/23 : RUN ls -ls /app
---> Running in fd4479923203
total 1240
4 -rw-r--r--.    1 root root   1406 Feb  5 11:37 Dockerfile
4 -rw-r--r--.    1 root root    504 Feb  4 14:10 Jenkinsfile
4 -rw-r--r--.    1 root root   1057 Feb  4 14:09 README.md
0 drwxr-xr-x.    2 root root     83 Feb  4 14:09 gcp-deploy
4 -rw-r--r--.    1 root root    827 Feb  4 14:09 nginx.conf.template
60 drwxr-xr-x. 1150 root root  36864 Feb  5 11:37 node_modules
628 -rw-r--r--.    1 root root 639799 Feb  5 11:37 package-lock.json
4 -rw-r--r--.    1 root root   2821 Feb  4 14:09 package.json
12 -rw-r--r--.    1 root root   9106 Feb  4 14:18 pom.xml
0 drwxr-xr-x.    2 root root     64 Feb  4 14:09 public
4 -rw-r--r--.    1 root root    591 Feb  4 14:09 sonar-project.properties
4 drwxr-xr-x.   15 root root   4096 Feb  4 14:09 src
0 drwxr-xr-x.    2 root root     28 Feb  4 14:10 target
4 -rw-r--r--.    1 root root    486 Feb  4 14:09 tsconfig.json
508 -rw-r--r--.    1 root root 517508 Feb  4 14:09 yarn.lock
Removing intermediate container fd4479923203
---> e3329a5200cd
Step 13/23 : FROM nginx:1.15
---> 53f3fd8007f7
Step 14/23 : COPY nginx.conf.template /etc/nginx/conf.d/nginx.conf.template
---> c6ddc33a47e7
Step 15/23 : COPY gcp-deploy/htpasswd /etc/nginx/htpasswd
---> db3d7c99cea0
Step 16/23 : COPY --from=react-builder /app/.env.local /etc/nginx/conf.d/
---> 973f6987ab00
Step 17/23 : RUN NGINX_SUB_FILTER=$(cat /etc/nginx/conf.d/.env.local | grep '=' | sort | sed -e 's/REACT_APP_([a-zA-Z_]*)=(.*)/sub_filter "NGINX_REPLACE_1" "${1}";/') && cat /etc/nginx/conf.d/nginx.conf.template | sed -e "s|LOCATION_SUB_FILTER|$(echo $NGINX_SUB_FILTER)|" | sed -u 's|}"; |}";ntt|g' > /etc/nginx/conf.d/default.conf.template
---> Running in a1eb5fdc7a02
Removing intermediate container a1eb5fdc7a02
---> ee3041f221df
Step 18/23 : COPY --from=react-builder /app/build /var/www
COPY failed: stat /mnt/data/docker/overlay2/7a85db99ff921b8588b566624634cc64c864f0e7db46c6540d09517662f7335a/merged/app/build: no such file or directory

我可以确认/mnt/data/docker/overlay2/7a85db99ff921b8588b566624634cc64c864f0e7db46c6540d09517662f7335a不存在

错误很明显,路径/app/buildreact-builder图层中不存在。

要解决此问题,您可以做的是将WORKDIR app更新为如下所示的WORKDIR /app

FROM node:10-slim AS react-builder
ARG REACT_APP_ENV
ENV REACT_APP_ENV $REACT_APP_ENV
WORKDIR /app
COPY . .

问题是WORKDIR appWORKDIR /app不同,因此当您执行RUN npm build时,生成的build文件夹将不会保存在预期的/app/build中,从而导致错误。

显然,当运行RUN npm build时,它不会发生预期的情况 - 没有编译? - 因此没有/app/build目录。但是,如果RUN npm run-script build运行,则编译工作正常

Step 10/24 : RUN npm run-script build
---> Running in b61a1d5020d9
> opas-frontend@0.13.0 build /app
> react-scripts build
Creating an optimized production build...
Compiled successfully.

因此,COPY --from=react-builder /app/build /var/www也可以正常工作。

最新更新