阿里雲ECS啓動Docker容器沒法訪問外網

初次在ECS使用docker,如文章所述有誤,勞煩看官留言指正,謝過!linux

日期:2017-07-19
平臺: 阿里雲 ECS
操做系統: Centos 7
註明日期,只是輔助看官參考,隨時間過去,文中所述不必定適用您遇到的問題。golang

言歸正傳docker

本文重點講,ECS中docker build 不能訪問外網的問題,
因此,如何安裝或部署golang服務,請參考
使用alpinelinux 構建 golang http ubuntu

建立鏡像的Dockerfile以下:centos

FROM  alpine:latest
MAINTAINER demo <juest a demo>

RUN echo -e "https://mirror.tuna.tsinghua.edu.cn/alpine/latest-stable/main\n\
https://mirrors.aliyun.com/alpine/v3.6/main" >> /etc/apk/repositories

RUN apk add --update curl bash && \
    rm -rf /var/cache/apk/*

RUN mkdir -p /data/go
COPY http /data/go

EXPOSE 8080

ENTRYPOINT ["/data/go/http"]

提示以下錯誤:bash

...
fetch https://mirror.tuna.tsinghua.edu.cn/alpine/latest-stable/main/x86_64/APKINDEX.tar.gz
ERROR: https://mirror.tuna.tsinghua.edu.cn/alpine/latest-stable/main: temporary error (try again later)
WARNING: Ignoring APKINDEX.951f4ce6.tar.gz: No such file or directory
fetch https://mirror.tuna.tsinghua.edu.cn/alpine/latest-stable/community/x86_64/APKINDEX.tar.gz
ERROR: https://mirror.tuna.tsinghua.edu.cn/alpine/latest-stable/community: temporary error (try again later)
WARNING: Ignoring APKINDEX.d4f262b4.tar.gz: No such file or directory
ERROR: unsatisfiable constraints:
  bash (missing):
    required by: world[bash]
  curl (missing):
    required by: world[curl]
The command '/bin/sh -c apk add --update curl bash &&     rm -rf /var/cache/apk/*' returned a non-zero code: 2

由於初次使用docker,開始也不知道具體發生了什麼問題,由於上面的地址和連接在本地都是能夠訪問的,因此猜想是docker容器不能訪問外網,可是一樣的Dockerfile在筆者本地環境是沒有問題的,在ECS上又試了ubuntu的最新鏡像啓動的容器,一樣是沒法訪問外網。
查閱一些資料後,得知若是是啓動容器沒法訪問外網,docker run 的時候能夠添加 --net=host 可使容器使用宿主機的網絡訪問外網,但如今是要生成鏡像docker build。
通過一番google,最後總結以下解決方案(已解決筆者遇到的問題)網絡

在宿主機中,進行以下操做:dom

1、 查看宿主機的nameservercurl

> cat /etc/resolv.conf
domain mycompany
search mycompany
nameserver xxx.xxx.xxx.xxx

2、 建立或修改 /etc/default/docker,在其中添加或編輯以下選項fetch

DOCKER_OPTS="--dns xxx.xxx.xxx.xxx"

3、 重啓 docker

systemctl restart docker

而後從新執行 docker build 所有經過

參考1
參考2

相關文章
相關標籤/搜索