当前位置:   article > 正文

ERROR [stage-1 2/4] RUN apk add --no-cache ca-certificates_dockerfile执行run apk add --no-cache wkhtmltopdf报错er

dockerfile执行run apk add --no-cache wkhtmltopdf报错error: unable to select
  1. 背景

该环境是在一个VM虚拟中进行的docker测试

DOCKERFILE文档

  1. FROM golang:1.9 as builder
  2. RUN mkdir -p /go/src/test
  3. WORKDIR /go/src/test
  4. COPY main.go .
  5. RUN CGO_ENABLED=0 GOOS=linux go build -o app .
  6. FROM alpine:latest
  7. RUN apk add --no-cache ca-certificates
  8. WORKDIR /root/
  9. COPY --from=builder /go/src/test/app .
  10. CMD ["./app"]

创建命令

docker build -t lqliw/test02:V1 .

运行报错如下

  1. [+] Building 12.3s (12/14)
  2. => [internal] load build definition from Dockerfile 0.0s
  3. => => transferring dockerfile: 302B 0.0s
  4. => [internal] load .dockerignore 0.0s
  5. => => transferring context: 2B 0.0s
  6. => [internal] load metadata for docker.io/library/golang:1.9 1.9s
  7. => [internal] load metadata for docker.io/library/alpine:latest 0.0s
  8. => CACHED [builder 1/5] FROM docker.io/library/golang:1.9@sha256:8b5968585131604a92af02f5690713efadf029cc8dad53f79280b87a80eb1354 0.0s
  9. => [internal] load build context 0.0s
  10. => => transferring context: 28B 0.0s
  11. => CACHED [stage-1 1/4] FROM docker.io/library/alpine:latest 0.0s
  12. => ERROR [stage-1 2/4] RUN apk add --no-cache ca-certificates 10.3s
  13. => [builder 2/5] RUN mkdir -p /go/src/test 0.4s
  14. => [builder 3/5] WORKDIR /go/src/test 0.0s
  15. => [builder 4/5] COPY main.go . 0.0s
  16. => [builder 5/5] RUN CGO_ENABLED=0 GOOS=linux go build -o app . 0.5s
  17. ------
  18. > [stage-1 2/4] RUN apk add --no-cache ca-certificates:
  19. #0 0.291 fetch https://dl-cdn.alpinelinux.org/alpine/v3.17/main/x86_64/APKINDEX.tar.gz
  20. #0 5.296 WARNING: Ignoring https://dl-cdn.alpinelinux.org/alpine/v3.17/main: temporary error (try again later)
  21. #0 5.296 fetch https://dl-cdn.alpinelinux.org/alpine/v3.17/community/x86_64/APKINDEX.tar.gz
  22. #0 10.30 WARNING: Ignoring https://dl-cdn.alpinelinux.org/alpine/v3.17/community: temporary error (try again later)
  23. #0 10.30 ERROR: unable to select packages:
  24. #0 10.30 ca-certificates (no such package):
  25. #0 10.30 required by: world[ca-certificates]
  26. ------
  27. Dockerfile:8
  28. --------------------
  29. 6 |
  30. 7 | FROM alpine:latest
  31. 8 | >>> RUN apk add --no-cache ca-certificates
  32. 9 | WORKDIR /root/
  33. 10 | COPY --from=builder /go/src/test/app .
  34. --------------------
  35. ERROR: failed to solve: process "/bin/sh -c apk add --no-cache ca-certificates" did not complete successfully: exit code: 1

2、解决过程

  1. 刚开始想的是ca-certificates证书安装失败,原因是读取不到文件,想了一下应该是网上下载的,所以修改为国内APT阿里源和清华源,结果还是报错

  1. #更新Alpine的软件源为阿里云
  2. RUN echo http://mirrors.aliyun.com/alpine/v3.10/main/ > /etc/apk/repositories && \
  3. echo http://mirrors.aliyun.com/alpine/v3.10/community/ >> /etc/apk/repositories
  4. RUN apk update && upgrade
  1. 使用alpine直接建一个容器,进入直接使用命令操作。一样的报错

  1. [root@localhost dockertest2]# docker run -itd alpine
  2. / # apk add --no-cache ca-certificates
  3. fetch https://dl-cdn.alpinelinux.org/alpine/v3.17/main/x86_64/APKINDEX.tar.gz
  4. WARNING: Ignoring https://dl-cdn.alpinelinux.org/alpine/v3.17/main: temporary error (try again later)
  5. fetch https://dl-cdn.alpinelinux.org/alpine/v3.17/community/x86_64/APKINDEX.tar.gz
  6. WARNING: Ignoring https://dl-cdn.alpinelinux.org/alpine/v3.17/community: temporary error (try again later)
  7. ERROR: unable to select packages:
  8. ca-certificates (no such package):
  9. required by: world[ca-certificates]
  1. 更换为阿里源、清华源也一样

  1. / # echo http://mirrors.aliyun.com/alpine/v3.10/main/ > /etc/apk/repositories && \
  2. > echo http://mirrors.aliyun.com/alpine/v3.10/community/ >> /etc/apk/repositories
  3. / # apk update && apk upgrade
  4. fetch http://mirrors.aliyun.com/alpine/v3.10/main/x86_64/APKINDEX.tar.gz
  5. ERROR: http://mirrors.aliyun.com/alpine/v3.10/main/: temporary error (try again later)
  6. WARNING: Ignoring http://mirrors.aliyun.com/alpine/v3.10/main/: No such file or directory
  7. fetch http://mirrors.aliyun.com/alpine/v3.10/community/x86_64/APKINDEX.tar.gz
  8. ERROR: http://mirrors.aliyun.com/alpine/v3.10/community/: temporary error (try again later)
  9. WARNING: Ignoring http://mirrors.aliyun.com/alpine/v3.10/community/: No such file or directory
  10. 2 errors; 15 distinct packages available
  1. 后来发现网络不通!?

  1. / # ping g.cn
  2. ^C
  3. / # ping 61.139.2.69
  4. PING 61.139.2.69 (61.139.2.69): 56 data bytes
  5. ^C
  6. --- 61.139.2.69 ping statistics ---
  7. 5 packets transmitted, 0 packets received, 100% packet loss
  1. 问题找到了,来源:https://www.ngui.cc/el/3157056.html

原因:docker run默认的容器网络运行模式是桥接模式即bridge模式,该模式下应该也可以访问外网,但是能力不够没有研究出来应该怎样配置,不管怎么配置仍然无法连接外网。在host模式下,docker容器与宿主机共享网络,即可以访问外网和使用apt-get等操作。

运行容器时更改docker网络连接模式来解决这个问题,即在docker run 指令后加参数 --network=host,host模式的含义是使docker的网络连接模式与宿主机的网络连接模式一致。命令如下所示:

docker run --network=host -i -t ubuntu /bin/bash

同样如果想使用Dockerfile和docker bulid来构建docker镜像,如果Dockerfile中有apt-get等网络下载指令并且由于不能连接网络导致构建失败,同样也可以使用–network=host参数。代码如下所示:

docker build --network=host  -t="test/static_web:first" .

3、解决办法

在build时增加--network=host参数,如下

docker build --network=host -t lqliw/test01:V1 .

最后:写这么细的原因是,看到的朋友可以避坑。花了1小时才解决。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小舞很执着/article/detail/880066
推荐阅读
相关标签
  

闽ICP备14008679号