Docker: 计算缓存键失败

huangapple go评论81阅读模式
英文:

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 [&quot;/main&quot;]

command for building:

docker build --no-cache --progress=plain  - &lt; 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: &quot;/main&quot; not found: not found

#8 [5/6] COPY ./configs /configs
#8 sha256:630f272dd60dd307f40dbbdaef277ee0dfc24b71fa11e10a3b8efd64d3c05086
#8 ERROR: &quot;/configs&quot; not found: not found

#4 [2/6] RUN mkdir -p /src/build
#4 sha256:b9fa3007a44471d47414dd29b3ff07ead6af28ede820a2b4bae0ce84cf2c5a83
#4 DONE 0.2s
------
 &gt; [5/6] COPY ./configs /configs:
------
------
 &gt; [6/6] COPY main /main:
------
failed to compute cache key: &quot;/main&quot; 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 folder configs from the host to the Docker image.
  • COPY main /main: copy the executable file main 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. 解决方案

  1. 尝试在没有COPYCMD的情况下进行构建

  2. 然后使用新镜像运行

  3. 使用bashsh进入正在运行的容器

  4. 创建文件夹

  5. 退出exec容器

  6. 使用docker run commit创建正在运行的容器的新镜像

  7. 停止容器并删除它

  8. 使用新镜像重新构建,并包含COPYCMD命令

英文:

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

  1. Try to build without COPY and CMD

  2. Than run the the new image

  3. exec into running container with bash or sh

  4. Create the folders

  5. Exit exec container

  6. Create a new image of the running container with docker run commit

  7. Stop the container and delete it

  8. Build again with your new image and include COPY and CMD

答案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.

huangapple
  • 本文由 发表于 2022年4月12日 14:56:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/71838329.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定