英文:
Docker : failed to compute cache key
问题
我正在尝试为我的示例Go应用程序构建一个Docker镜像。
我正在从sample-app文件夹本身运行,并使用goland编辑器的终端。但是构建失败了,并给出了一些错误。
我的Docker文件如下所示:
FROM alpine:latest
RUN mkdir -p /src/build
WORKDIR /src/build
RUN apk add --no-cache tzdata ca-certificates
COPY ./configs /configs
COPY main /main
EXPOSE 8000
CMD ["/main"]
构建命令:
docker build --no-cache --progress=plain - < Dockerfile
错误和日志:
#1 [internal] load build definition from Dockerfile
#1 sha256:8bb9ee83603259cf748d90ce42602f12527fa720d7417da22799b2ad4e503497
#1 transferring dockerfile: 222B done
#1 DONE 0.0s
#2 [internal] load .dockerignore
#2 sha256:f93d938488588cd0e0a94d9d343fe69dcfd28d0cb1da95ad7aab00aac50235c3
#2 transferring context: 2B done
#2 DONE 0.0s
#3 [internal] load metadata for docker.io/library/alpine:latest
#3 sha256:13549c58a76bcb5dac9d52bc368a8fb6b5cf7659f94e3fa6294917b85546978d
#3 DONE 0.0s
#10 [1/6] FROM docker.io/library/alpine:latest
#10 sha256:d20daa00e252bfb345a1b4f53b6bb332aafe702d8de5e583a76fcd09ba7ea1c1
#10 CACHED
#7 [internal] load build context
#7 sha256:0f7a8a6082a837c139acc2855e1b745bba9f28cc96709d45cd0b7be42442c0e8
#7 transferring context: 2B done
#7 DONE 0.0s
#4 [2/6] RUN mkdir -p /src/build
#4 sha256:b9fa3007a44471d47414dd29b3ff07ead6af28ede820a2b4bae0ce84cf2c5a83
#4 CACHED
#5 [3/6] WORKDIR /src/build
#5 sha256:b2ec58a365fdd74c4f9030b0caff2e2225eea33617da306678ad037fce675388
#5 CACHED
#6 [4/6] RUN apk add --no-cache tzdata ca-certificates
#6 sha256:0966097abf956d5781bc2330d49cf715cd52c3807e8fedfff07dec50907ff03b
#6 CACHED
#9 [6/6] COPY main /main
#9 sha256:f4b81960427c014a020361bea0903728f289e1d796892fe0adc6409434f3ca76
#9 ERROR: "/main" not found: not found
#8 [5/6] COPY ./configs /configs
#8 sha256:630f272dd60dd307f40dbbdaef277ee0dfc24b71fa11e10a3b8efd64d3c05086
#8 ERROR: "/configs" not found: not found
#4 [2/6] RUN mkdir -p /src/build
#4 sha256:b9fa3007a44471d47414dd29b3ff07ead6af28ede820a2b4bae0ce84cf2c5a83
#4 DONE 0.2s
------
> [5/6] COPY ./configs /configs:
------
------
> [6/6] COPY main /main:
------
failed to compute cache key: "/main" not found: not found
PS:我找不到问题出在哪里?请帮忙。
英文:
I am trying to build a docker image for my sample-go app.
I am running it from the sample-app folder itself and using the goland editor's terminal. But the build is failing and giving me certain errors.
My docker file looks like this:
FROM alpine:latest
RUN mkdir -p /src/build
WORKDIR /src/build
RUN apk add --no-cache tzdata ca-certificates
COPY ./configs /configs
COPY main /main
EXPOSE 8000
CMD ["/main"]
command for building:
docker build --no-cache --progress=plain - < Dockerfile
Error And Logs:
#1 [internal] load build definition from Dockerfile
#1 sha256:8bb9ee83603259cf748d90ce42602f12527fa720d7417da22799b2ad4e503497
#1 transferring dockerfile: 222B done
#1 DONE 0.0s
#2 [internal] load .dockerignore
#2 sha256:f93d938488588cd0e0a94d9d343fe69dcfd28d0cb1da95ad7aab00aac50235c3
#2 transferring context: 2B done
#2 DONE 0.0s
#3 [internal] load metadata for docker.io/library/alpine:latest
#3 sha256:13549c58a76bcb5dac9d52bc368a8fb6b5cf7659f94e3fa6294917b85546978d
#3 DONE 0.0s
#10 [1/6] FROM docker.io/library/alpine:latest
#10 sha256:d20daa00e252bfb345a1b4f53b6bb332aafe702d8de5e583a76fcd09ba7ea1c1
#10 CACHED
#7 [internal] load build context
#7 sha256:0f7a8a6082a837c139acc2855e1b745bba9f28cc96709d45cd0b7be42442c0e8
#7 transferring context: 2B done
#7 DONE 0.0s
#4 [2/6] RUN mkdir -p /src/build
#4 sha256:b9fa3007a44471d47414dd29b3ff07ead6af28ede820a2b4bae0ce84cf2c5a83
#4 CACHED
#5 [3/6] WORKDIR /src/build
#5 sha256:b2ec58a365fdd74c4f9030b0caff2e2225eea33617da306678ad037fce675388
#5 CACHED
#6 [4/6] RUN apk add --no-cache tzdata ca-certificates
#6 sha256:0966097abf956d5781bc2330d49cf715cd52c3807e8fedfff07dec50907ff03b
#6 CACHED
#9 [6/6] COPY main /main
#9 sha256:f4b81960427c014a020361bea0903728f289e1d796892fe0adc6409434f3ca76
#9 ERROR: "/main" not found: not found
#8 [5/6] COPY ./configs /configs
#8 sha256:630f272dd60dd307f40dbbdaef277ee0dfc24b71fa11e10a3b8efd64d3c05086
#8 ERROR: "/configs" not found: not found
#4 [2/6] RUN mkdir -p /src/build
#4 sha256:b9fa3007a44471d47414dd29b3ff07ead6af28ede820a2b4bae0ce84cf2c5a83
#4 DONE 0.2s
------
> [5/6] COPY ./configs /configs:
------
------
> [6/6] COPY main /main:
------
failed to compute cache key: "/main" not found: not found
PS: I am not able to find where is the problem? Help Please
答案1
得分: 3
这是一个基本的错误。
COPY ./configs /configs
:将主机上的configs
文件夹复制到Docker镜像中。COPY main /main
:将可执行文件main
从主机复制到Docker镜像中。
问题是:
- 基础Docker镜像中没有这些文件夹
/configs
和/main
。你必须手动创建它们(Docker会按照你的命令理解)。 - 但是我有一些建议:
- 为两个目的创建两个Docker镜像:构建和生产。
- 将源代码复制到用于构建应用程序的Docker构建器镜像中。
- 将必要的输出文件从Docker构建器镜像复制到Docker生产镜像中。
英文:
This is a basic mistake.
COPY ./configs /configs
: copy the folderconfigs
from the host to the Docker image.COPY main /main
: copy the executable filemain
from the host to the Docker image.
The problems are:
- The base Docker images do not have these folders
/configs
,/main
. You must create them manually (Docker understood your command this way). - But I have some advice:
- Create 2 Docker images for 2 purposes: build, production.
- Copy the source code into Docker builder image which is use for building your app.
- Copy necessary output files from the Docker builder image into the Docker production image.
答案2
得分: 2
在我的情况下,我在.dockerignore文件中错过了文件夹条目。做一些像这样的事情。
**/*
!docker-images
!configs
!main
英文:
In my case I missed the folder entry in .dockerignore file. Do something like that.
**/*
!docker-images
!configs
!main
答案3
得分: 1
两个文件夹/main
和/configs
不存在。
COPY
命令无法复制到这些文件夹中。
1. 解决方案
在构建过程中创建这些文件夹
RUN mkdir -p /main
RUN mkdir -p /configs
然后使用COPY
命令
2. 解决方案
-
尝试在没有
COPY
和CMD
的情况下进行构建 -
然后使用新镜像运行
-
使用
bash
或sh
进入正在运行的容器 -
创建文件夹
-
退出
exec
容器 -
使用docker run
commit
创建正在运行的容器的新镜像 -
停止容器并删除它
-
使用新镜像重新构建,并包含
COPY
和CMD
命令
英文:
The two folders /main
and /configs
does not exist.
The COPY
command can't copy into this folders.
1. Solution
Create the folders on build
RUN mkdir -p /main
RUN mkdir -p /configs
And than use COPY
2. Solution
-
Try to build without
COPY
andCMD
-
Than
run
the the new image -
exec
into running container withbash
orsh
-
Create the folders
-
Exit
exec
container -
Create a new image of the running container with docker run
commit
-
Stop the container and delete it
-
Build again with your new image and include
COPY
andCMD
答案4
得分: 0
在我的情况下,问题是我的计算机连接了 VPN/代理网络。
在我断开 VPN/代理网络后,问题得到解决。
英文:
In my case, the issue was the connected vpn/proxy network from my machine.
It worked after I disconnecting the vpn/proxy network.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论