问题
docker build时报错ERROR: failed to solve: failed to compute cache key,找不到对应文件目录,但实际上目录文件存在
root@ubuntu:~/crfsdi-performance-front# docker build --no-cache -t yu .
[+] Building 15.2s (7/8) docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 168B 0.0s
=> [internal] load metadata for docker.io/library/nginx:1.19 15.2s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 279B 0.0s
=> [1/4] FROM docker.io/library/nginx:1.19@sha256:df13abe416e37eb3db4722840dd479b00ba193ac6606e7902331dcea50f4f1f2 0.0s
=> => resolve docker.io/library/nginx:1.19@sha256:df13abe416e37eb3db4722840dd479b00ba193ac6606e7902331dcea50f4f1f2 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 32B 0.0s
=> CACHED [2/4] RUN pwd 0.0s
=> ERROR [3/4] COPY dist /usr/share/nginx/html/ 0.0s
------
> [3/4] COPY dist /usr/share/nginx/html/:
------
Dockerfile:5
--------------------
3 | RUN pwd
4 |
5 | >>> COPY dist /usr/share/nginx/html/
6 | COPY nginx.conf /etc/nginx/conf.d/nginx.conf
7 | EXPOSE 80
--------------------
ERROR: failed to solve: failed to compute cache key: failed to calculate checksum of ref 6f513923-284c-49e9-bbe9-046524d0bd21::wkdlaual0x08detpvfwizdrqt: "/dist": not found
root@ubuntu:~/crfsdi-performance-front# ls -a
. commitlint.config.js Dockerfile.bak .env.development .git Jenkinsfile-dev mock .nvmrc .prettierrc.js src tsconfig.json
.. deploy .dockerignore .env.production .gitignore LICENSE nginx.conf package.json public stylelint.config.js types
.browserslistrc dist .editorconfig .env.staging .husky .lintstagedrc node_modules pnpm-lock.yaml README.en-US.md .stylelintignore vite.config.ts
build Dockerfile .env eslint.config.js index.html .markdownlint.json .npmrc postcss.config.js README.md tailwind.config.ts .vscode
root@ubuntu:~/crfsdi-performance-front# ls .dockerignore
原因
dockerignore文件配置了忽略
root@ubuntu:~/crfsdi-performance-front# cat .dockerignore
node_modules
.DS_Store
dist
dist-ssr
*.local
.eslintcache
report.html
yarn.lock
npm-debug.log*
.pnpm-error.log*
.pnpm-debug.log
tests/**/coverage/
# Editor directories and files
.idea
*.suo
*.ntvs*
*.njsproj
*.sln
tsconfig.tsbuildinfo
dockerignore作用
.dockerignore 文件的作用类似于 git 工程中的 .gitignore 。不同的是 .dockerignore 应用于 docker 镜像的构建,它存在于 docker 构建上下文的根目录,用来排除不需要上传到 docker 服务端的文件或目录。
docker 在构建镜像时首先从构建上下文找有没有 .dockerignore 文件,如果有的话则在上传上下文到 docker 服务端时忽略掉 .dockerignore 里面的文件列表。
构建镜像时能避免不需要的大文件上传到服务端,从而拖慢构建的速度、网络带宽的消耗;
可以避免构建镜像时将一些敏感文件及其他不需要的文件打包到镜像中,从而提高镜像的安全性;
.dockerignore 文件编写方法
.dockerignore 文件的写法和 .gitignore 类似,支持正则和通配符,具体规则如下:
1、每行为一个条目;
2、空行被忽略;
3、构建上下文路径为所有文件的根路径;
文件匹配规则具体语法如下:
符号 | 作用 |
---|---|
# | 注释 |
* | 匹配0或多个非/的字符 |
? | 匹配1个非/的字符 |
** | 0个或多个目录 |
! | 除…外,需结合上下文语义 |
举例
例子 | 解释 |
---|---|
*/temp* | 匹配根路径下一级目录下以temp开头的文件或目录 |
*/*/temp* | 匹配根路径下两级目录下以temp开头的文件或目录 |
temp? | 匹配以temp开头,任意一个字符结尾的目录或文件 |
**/*.go | 匹配所有以.go结尾的文件或目录,即递归搜索所有路径 |
*.md !README.md | 以.md结尾的文件或目录,并且README.md除外 |
感悟
再一次说明基础知识很重要,这个问题卡壳了好长时间,回头看看就是一个基础知识盲点