在用Docker build一个基于openjdk:8-jdk-alpine的镜像时,遇到了以下错误:
WARNING: IPv4 forwarding is disabled. Networking will not work.
fetch http://dl-cdn.alpinelinux.org/alpine/v3.8/main/x86_64/APKINDEX.tar.gz
ERROR: http://dl-cdn.alpinelinux.org/alpine/v3.8/main: temporary error (try again later)
WARNING: Ignoring APKINDEX.adfa7ceb.tar.gz: No such file or directory
fetch http://dl-cdn.alpinelinux.org/alpine/v3.8/community/x86_64/APKINDEX.tar.gz
ERROR: http://dl-cdn.alpinelinux.org/alpine/v3.8/community: temporary error (try again later)
WARNING: Ignoring APKINDEX.efaa1f73.tar.gz: No such file or directory
2 errors; 53 distinct packages available
Dockerfile (部分)如下:
FROM openjdk:8-jdk-alpine
RUN apk update && apk upgrade && apk add netcat-openbsd
...
刚开始以为是http://dl-cdn.alpinelinux.org/alpine/v3.8/main 链接打不开,结果浏览器里可以正常打开。
搜索了网上的一些说法,都不能解决问题。
后来分析了一下,准备先来测试一下alpine这个容器能不能访问外网:
docker run -it openjdk:8-jdk-alpine ping www.baidu.com
果然不能ping通,错误信息如下:
WARNING: IPv4 forwarding is disabled. Networking will not work.
ping: bad address 'www.baidu.com'
为了排除是DNS问题还是网络问题,接着直接ping著名的南京电信DNS服务器的IP地址:
docker run -it openjdk:8-jdk-alpine ping 114.114.114.114
还是ping不通。
WARNING: IPv4 forwarding is disabled. Networking will not work.
PING 114.114.114.114 (114.114.114.114): 56 data bytes
这时才惊觉一直出现的“WARNING: IPv4 forwarding is disabled. Networking will not work.” 已经告诉我们网络不能正常工作了!真是灯下黑啊。
搜索这个错误信息后,发现了需要设置允许IP forward:
sysctl -w net.ipv4.ip_forward=1
再次ping百度,成功Ping通。
docker run -it openjdk:8-jdk-alpine ping www.baidu.com
运行apk update
,成功。
docker run -it openjdk:8-jdk-alpine apk update
再次运行docker build构建镜像,成功!
参考文档: