docker - 来自多阶段Dockerfile的`npm build`和`COPY --from`失败,并且`COPY失败:stat/mnt/data/docker/overlay2/xxxxx/merged/app/build

标签 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|}";\ |}";\n\t\t|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|}";\ |}";\n\t\t|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/build层中不存在路径react-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中,因此会出现错误。

关于docker - 来自多阶段Dockerfile的`npm build`和`COPY --from`失败,并且`COPY失败:stat/mnt/data/docker/overlay2/xxxxx/merged/app/build,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60072107/

相关文章:

docker - 在 ASP.NET Core 的 Linux 容器中安装字体

mysql - docker-compose.yml问题nodejs和mysql

docker - 将根文件夹复制到 docker 根路径?

mongodb - 在 dockerized mongo 上初始化数据

python - 在 AWS 实例上的 Docker 容器中使用 PyCharm 进行开发

docker - 新代码更改存在于实时容器中,但未反射(reflect)在浏览器中

python - Docker SDK for python中的auto_remove和remove有什么区别

docker - Kubernetes-如何将数据从一个pod发送到kubernetes中的另一个pod

bash - 是否可以从 psql 内部导入 OSM 数据?

多阶段构建中的 Docker-Compose Args(变量扩展)