Docker容器创建时,无法访问镜像源:Could not connect to archive.ubuntu. ...

打印 上一主题 下一主题

主题 844|帖子 844|积分 2532

1.问题描述

当基于dockerfile创建容器时,遇到Could not connect to ...、Failed to fetch ...等异常时,大概缘故原由是没有设置好容器创建所需的镜像源。这里以Ubuntu基础镜像源为例。


  • dockerfile内容
  1. FROM ubuntu
  2. RUN apt update && apt install python3 -y && apt install python3-pip -y && apt install git -y && apt install vim -y && apt install curl -y && apt install wget -y && apt install unzip -y && apt install zip -y && apt install tree -y && apt install npm -y
  3. CMD ["bash"]
复制代码


  • dockerfile实行与异常提示
  1. $ docker build -f data/dockerfiles --tag my_test
  2. Get:1 http://mirrors.tools.aliyun.com/ubuntu focal InRelease [265 kB]
  3. Get:2 http://mirrors.tools.aliyun.com/ubuntu focal-updates InRelease [128 kB]
  4. Get:3 http://mirrors.tools.aliyun.com/ubuntu focal-backports InRelease [128 kB]
  5. 此处省略......
  6. Ign:33 http://archive.ubuntu.com/ubuntu noble InRelease
  7. Ign:34 http://archive.ubuntu.com/ubuntu noble-updates InRelease
  8. Ign:35 http://archive.ubuntu.com/ubuntu noble-backports InRelease
  9. 此处省略......
  10.   Could not connect to archive.ubuntu.com:80 (185.125.190.81), connection timed out Could not connect to archive.ubuntu.com:80 (91.189.91.82), connection timed out Could not connect to archive.ubuntu.com:80 (91.189.91.81), connection timed out Could not connect to archive.ubuntu.com:80 (185.125.190.82), connection timed out Could not connect to archive.ubuntu.com:80 (185.125.190.83), connection timed out
  11. Err:34 http://archive.ubuntu.com/ubuntu noble-updates InRelease
  12.   Unable to connect to archive.ubuntu.com:80:
  13. 此处省略......
  14. W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/noble/InRelease  Could not connect to archive.ubuntu.com:80 (185.125.190.81), connection timed out Could not connect to archive.ubuntu.com:80 (91.189.91.82), connection timed out Could not connect to archive.ubuntu.com:80 (91.189.91.81), connection timed out Could not connect to archive.ubuntu.com:80 (185.125.190.82), connection timed out Could not connect to archive.ubuntu.com:80 (185.125.190.83), connection timed out
  15. W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/noble-updates/InRelease  Unable to connect to archive.ubuntu.com:80:
  16. W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/noble-backports/InRelease  Unable to connect to archive.ubuntu.com:80:
  17. 此处省略......
复制代码
2.办理方案

本质上,是必要修改基于镜像文件创建的容器中的/etc/apt/sources.list文件,将该文件中的镜像源从官方源换成国内源。这里以Ubuntu基础镜像源为例,有如下两种可选方案实施。
2.1.从dockerfile添加命令修改(主动化、简单)

打开dockerfile,在FROM ...命令后添加镜像源修改命令即可,如下:
  1. FROM ubuntu
  2. # 镜像源修改命令
  3. RUN sed -i "s@http://.*archive.ubuntu.com@http://mirrors.tools.aliyun.com@g" /etc/apt/sources.list
  4. RUN sed -i "s@http://.*security.ubuntu.com@http://mirrors.tools.aliyun.com@g" /etc/apt/sources.list
  5. # 容器软件安装命令
  6. RUN apt update && apt install python3 -y && apt install python3-pip -y && apt install git -y && apt install vim -y && apt install curl -y && apt install wget -y && apt install unzip -y && apt install zip -y && apt install tree -y && apt install npm -y
  7. CMD ["bash"]
复制代码
添加镜像源修改命令后,再次实行容器构建命令docker build -f data/dockerfiles --tag my_test,即可快速完成容器构建。
2.2.从创建启动的基础镜像容器修改(略微繁琐)



  • (1)创建启动基础镜像的容器
    1. $ docker run -it ubuntu /bin/bash
    复制代码
  • (2)进入容器命令行,然后手动修改sources.list文件
    1. $ vim /etc/apt/sources.list
    2. # 官方sources.list文件内容
    3. deb http://archive.ubuntu.com/ubuntu/ bionic main restricted
    4. deb-src http://archive.ubuntu.com/ubuntu/ bionic main restricted
    5. deb http://archive.ubuntu.com/ubuntu/ bionic-updates main restricted
    6. deb-src http://archive.ubuntu.com/ubuntu/ bionic-updates main restricted
    7. deb http://archive.ubuntu.com/ubuntu/ bionic universe
    8. deb-src http://archive.ubuntu.com/ubuntu/ bionic universe
    9. deb http://archive.ubuntu.com/ubuntu/ bionic-updates universe
    10. deb-src http://archive.ubuntu.com/ubuntu/ bionic-updates universe
    11. deb http://archive.ubuntu.com/ubuntu/ bionic multiverse
    12. deb-src http://archive.ubuntu.com/ubuntu/ bionic multiverse
    13. deb http://archive.ubuntu.com/ubuntu/ bionic-updates multiverse
    14. deb-src http://archive.ubuntu.com/ubuntu/ bionic-updates multiverse
    15. deb http://archive.ubuntu.com/ubuntu/ bionic-backports main restricted universe multiverse
    16. deb-src http://archive.ubuntu.com/ubuntu/ bionic-backports main restricted universe multiverse
    17. deb http://security.ubuntu.com/ubuntu/ bionic-security main restricted
    18. deb-src http://security.ubuntu.com/ubuntu/ bionic-security main restricted
    19. deb http://security.ubuntu.com/ubuntu/ bionic-security universe
    20. deb-src http://security.ubuntu.com/ubuntu/ bionic-security universe
    21. deb http://security.ubuntu.com/ubuntu/ bionic-security multiverse
    22. deb-src http://security.ubuntu.com/ubuntu/ bionic-security multiverse
    复制代码
    将上述官方sources.list文件内容修改为如下:
    1. deb http://mirrors.tools.aliyun.com/ubuntu bionic main restricted
    2. deb-src http://mirrors.tools.aliyun.com/ubuntu bionic main restricted
    3. deb http://mirrors.tools.aliyun.com/ubuntu bionic-updates main restricted
    4. deb-src http://mirrors.tools.aliyun.com/ubuntu bionic-updates main restricted
    5. deb http://mirrors.tools.aliyun.com/ubuntu bionic universe
    6. deb-src http://mirrors.tools.aliyun.com/ubuntu bionic universe
    7. deb http://mirrors.tools.aliyun.com/ubuntu bionic-updates universe
    8. deb-src http://mirrors.tools.aliyun.com/ubuntu bionic-updates universe
    9. deb http://mirrors.tools.aliyun.com/ubuntu bionic multiverse
    10. deb-src http://mirrors.tools.aliyun.com/ubuntu bionic multiverse
    11. deb http://mirrors.tools.aliyun.com/ubuntu bionic-updates multiverse
    12. deb-src http://mirrors.tools.aliyun.com/ubuntu bionic-updates multiverse
    13. deb http://mirrors.tools.aliyun.com/ubuntu bionic-backports main restricted universe multiverse
    14. deb-src http://mirrors.tools.aliyun.com/ubuntu bionic-backports main restricted universe multiverse
    15. deb http://mirrors.tools.aliyun.com/ubuntu bionic-security main restricted
    16. deb-src http://mirrors.tools.aliyun.com/ubuntu bionic-security main restricted
    17. deb http://mirrors.tools.aliyun.com/ubuntu bionic-security universe
    18. deb-src http://mirrors.tools.aliyun.com/ubuntu bionic-security universe
    19. deb http://mirrors.tools.aliyun.com/ubuntu bionic-security multiverse
    20. deb-src http://mirrors.tools.aliyun.com/ubuntu bionic-security multiverse
    复制代码
    注意,重要事变说3遍:
    该步调肯定是在启动的基础镜像容器内部实行!!!
    该步调肯定是在启动的基础镜像容器内部实行!!!
    该步调肯定是在启动的基础镜像容器内部实行!!!

  • (3)在当前容器中,手动安装dockerfile中列出的其他软件包
    1. apt update && apt install python3 -y && apt install python3-pip -y && apt install git -y && apt install vim -y && apt install curl -y && apt install wget -y && apt install unzip -y && apt install zip -y && apt install tree -y && apt install npm -y
    复制代码

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

卖不甜枣

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表