docker · Docker
报错原文
COPY failed: file not found in build context or excluded by .dockerignore
这是什么意思
COPY 只能从构建上下文里取文件,而那个文件不在其中。上下文是 docker build 的最后一个参数,所以通常是你用 ../x 指向了上一层目录,或者 .dockerignore 把那个文件滤掉了——忽略了 node_modules 或 *.env,却又要 COPY 其中的文件,是最典型的。这条消息同时点出两种原因,所以先 cat .dockerignore,再按上下文根目录重写路径。靠扩大上下文来解决,意味着整个目录都要传给守护进程,每次构建都会变慢。
怎么修
cat .dockerignore- 来自
- docker
- Docker
- 12
docker 的错误要先归到某一层才读得懂——客户端连不上守护进程、镜像仓库拒绝你、构建过程中某条 RUN 失败、容器一起来就死掉,是四种不同的问题——而在构建和运行这两层,那行字本身并不是原因:原因在里面那条命令留下的输出里,各层修复的代价也不一样。
怎么读报错
- 从第一行往下读。越往下越是工具内部的事,起因通常写在最上面。
- 有文件名和行号就从那里查——不是栈顶那一帧,而是最上面那条提到你自己写的文件的行。
- 把报错原文照样去搜,但先去掉你自己的路径和变量名,正是那些让搜索匹配不上。
- 同一种情况在不同版本里措辞不同。结果不对头,就把版本号一起加进查询。
- 粘贴修法之前,先确认它会丢掉什么。这里面有些是不能撤回的。
常见问题
Q. “COPY failed: file not found in build context or excluded by .dockerignore” 是什么意思?
COPY 只能从构建上下文里取文件,而那个文件不在其中。上下文是 docker build 的最后一个参数,所以通常是你用 ../x 指向了上一层目录,或者 .dockerignore 把那个文件滤掉了——忽略了 node_modules 或 *.env,却又要 COPY 其中的文件,是最典型的。这条消息同时点出两种原因,所以先 cat .dockerignore,再按上下文根目录重写路径。靠扩大上下文来解决,意味着整个目录都要传给守护进程,每次构建都会变慢。
Q. 怎么修?
cat .dockerignore —— 执行前先看上面的说明,确认这条命令会丢掉什么。
Q. 这是哪个工具报的?
docker。它属于Docker,报错原文有 12 个词。
相关报错
failed to solve: process "/bin/sh -c npm ci" did not complete successfully: exit code: 1buildkitstandard_init_linux.go: exec user process caused: no such file or directoryruncexec /usr/local/bin/entrypoint.sh: exec format errordockerOCI runtime create failed: exec: "bash": executable file not found in $PATH: unknownruncCannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?dockerBind for 0.0.0.0:8080 failed: port is already allocateddockerConflict. The container name "/api" is already in use by containerdockerfailed to register layer: Error processing tar file(exit status 1): no space left on devicedocker