赞
踩
该环境是在一个VM虚拟中进行的docker测试
DOCKERFILE文档
- FROM golang:1.9 as builder
- RUN mkdir -p /go/src/test
- WORKDIR /go/src/test
- COPY main.go .
- RUN CGO_ENABLED=0 GOOS=linux go build -o app .
-
- FROM alpine:latest
- RUN apk add --no-cache ca-certificates
- WORKDIR /root/
- COPY --from=builder /go/src/test/app .
- CMD ["./app"]
创建命令
docker build -t lqliw/test02:V1 .
运行报错如下
[+] Building 12.3s (12/14) => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 302B 0.0s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [internal] load metadata for docker.io/library/golang:1.9 1.9s => [internal] load metadata for docker.io/library/alpine:latest 0.0s => CACHED [builder 1/5] FROM docker.io/library/golang:1.9@sha256:8b5968585131604a92af02f5690713efadf029cc8dad53f79280b87a80eb1354 0.0s => [internal] load build context 0.0s => => transferring context: 28B 0.0s => CACHED [stage-1 1/4] FROM docker.io/library/alpine:latest 0.0s => ERROR [stage-1 2/4] RUN apk add --no-cache ca-certificates 10.3s => [builder 2/5] RUN mkdir -p /go/src/test 0.4s => [builder 3/5] WORKDIR /go/src/test 0.0s => [builder 4/5] COPY main.go . 0.0s => [builder 5/5] RUN CGO_ENABLED=0 GOOS=linux go build -o app . 0.5s ------ > [stage-1 2/4] RUN apk add --no-cache ca-certificates: #0 0.291 fetch https://dl-cdn.alpinelinux.org/alpine/v3.17/main/x86_64/APKINDEX.tar.gz #0 5.296 WARNING: Ignoring https://dl-cdn.alpinelinux.org/alpine/v3.17/main: temporary error (try again later) #0 5.296 fetch https://dl-cdn.alpinelinux.org/alpine/v3.17/community/x86_64/APKINDEX.tar.gz #0 10.30 WARNING: Ignoring https://dl-cdn.alpinelinux.org/alpine/v3.17/community: temporary error (try again later) #0 10.30 ERROR: unable to select packages: #0 10.30 ca-certificates (no such package): #0 10.30 required by: world[ca-certificates] ------ Dockerfile:8 -------------------- 6 | 7 | FROM alpine:latest 8 | >>> RUN apk add --no-cache ca-certificates 9 | WORKDIR /root/ 10 | COPY --from=builder /go/src/test/app . -------------------- ERROR: failed to solve: process "/bin/sh -c apk add --no-cache ca-certificates" did not complete successfully: exit code: 1
刚开始想的是ca-certificates证书安装失败,原因是读取不到文件,想了一下应该是网上下载的,所以修改为国内APT阿里源和清华源,结果还是报错
- #更新Alpine的软件源为阿里云
- RUN echo http://mirrors.aliyun.com/alpine/v3.10/main/ > /etc/apk/repositories && \
- echo http://mirrors.aliyun.com/alpine/v3.10/community/ >> /etc/apk/repositories
- RUN apk update && upgrade
使用alpine直接建一个容器,进入直接使用命令操作。一样的报错
- [root@localhost dockertest2]# docker run -itd alpine
- / # apk add --no-cache ca-certificates
- fetch https://dl-cdn.alpinelinux.org/alpine/v3.17/main/x86_64/APKINDEX.tar.gz
- WARNING: Ignoring https://dl-cdn.alpinelinux.org/alpine/v3.17/main: temporary error (try again later)
- fetch https://dl-cdn.alpinelinux.org/alpine/v3.17/community/x86_64/APKINDEX.tar.gz
- WARNING: Ignoring https://dl-cdn.alpinelinux.org/alpine/v3.17/community: temporary error (try again later)
- ERROR: unable to select packages:
- ca-certificates (no such package):
- required by: world[ca-certificates]
更换为阿里源、清华源也一样
- / # echo http://mirrors.aliyun.com/alpine/v3.10/main/ > /etc/apk/repositories && \
- > echo http://mirrors.aliyun.com/alpine/v3.10/community/ >> /etc/apk/repositories
- / # apk update && apk upgrade
- fetch http://mirrors.aliyun.com/alpine/v3.10/main/x86_64/APKINDEX.tar.gz
- ERROR: http://mirrors.aliyun.com/alpine/v3.10/main/: temporary error (try again later)
- WARNING: Ignoring http://mirrors.aliyun.com/alpine/v3.10/main/: No such file or directory
- fetch http://mirrors.aliyun.com/alpine/v3.10/community/x86_64/APKINDEX.tar.gz
- ERROR: http://mirrors.aliyun.com/alpine/v3.10/community/: temporary error (try again later)
- WARNING: Ignoring http://mirrors.aliyun.com/alpine/v3.10/community/: No such file or directory
- 2 errors; 15 distinct packages available
后来发现网络不通!?
- / # ping g.cn
- ^C
- / # ping 61.139.2.69
- PING 61.139.2.69 (61.139.2.69): 56 data bytes
- ^C
- --- 61.139.2.69 ping statistics ---
- 5 packets transmitted, 0 packets received, 100% packet loss
问题找到了,来源: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" .
在build时增加--network=host参数,如下
docker build --network=host -t lqliw/test01:V1 .
最后:写这么细的原因是,看到的朋友可以避坑。花了1小时才解决。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。