当前位置: 首页 > article >正文

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

1.问题描述

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

  • dockerfile内容
FROM ubuntu
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
CMD ["bash"]
  • dockerfile执行与异常提示
$ docker build -f data/dockerfiles --tag my_test

Get:1 http://mirrors.tools.aliyun.com/ubuntu focal InRelease [265 kB]
Get:2 http://mirrors.tools.aliyun.com/ubuntu focal-updates InRelease [128 kB]
Get:3 http://mirrors.tools.aliyun.com/ubuntu focal-backports InRelease [128 kB]
此处省略......
Ign:33 http://archive.ubuntu.com/ubuntu noble InRelease
Ign:34 http://archive.ubuntu.com/ubuntu noble-updates InRelease
Ign:35 http://archive.ubuntu.com/ubuntu noble-backports 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
Err:34 http://archive.ubuntu.com/ubuntu noble-updates InRelease
  Unable to connect to archive.ubuntu.com:80:
此处省略......
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
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/noble-updates/InRelease  Unable to connect to archive.ubuntu.com:80:
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/noble-backports/InRelease  Unable to connect to archive.ubuntu.com:80:
此处省略......

2.解决方案

本质上,是需要修改基于镜像文件创建的容器中的/etc/apt/sources.list文件,将该文件中的镜像源从官方源换成国内源。这里以Ubuntu基础镜像源为例,有如下两种可选方案实施。

2.1.从dockerfile添加命令修改(自动化、简单)

打开dockerfile,在FROM ...命令后添加镜像源修改命令即可,如下:

FROM ubuntu
# 镜像源修改命令
RUN sed -i "s@http://.*archive.ubuntu.com@http://mirrors.tools.aliyun.com@g" /etc/apt/sources.list
RUN sed -i "s@http://.*security.ubuntu.com@http://mirrors.tools.aliyun.com@g" /etc/apt/sources.list
# 容器软件安装命令
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
CMD ["bash"]

添加镜像源修改命令后,再次执行容器构建命令docker build -f data/dockerfiles --tag my_test,即可快速完成容器构建。

2.2.从创建启动的基础镜像容器修改(略微繁琐)

  • (1)创建启动基础镜像的容器

    $ docker run -it ubuntu /bin/bash
    
  • (2)进入容器命令行,然后手动修改sources.list文件

    $ vim /etc/apt/sources.list
    
    # 官方sources.list文件内容
    deb http://archive.ubuntu.com/ubuntu/ bionic main restricted
    deb-src http://archive.ubuntu.com/ubuntu/ bionic main restricted
    deb http://archive.ubuntu.com/ubuntu/ bionic-updates main restricted
    deb-src http://archive.ubuntu.com/ubuntu/ bionic-updates main restricted
    deb http://archive.ubuntu.com/ubuntu/ bionic universe
    deb-src http://archive.ubuntu.com/ubuntu/ bionic universe
    deb http://archive.ubuntu.com/ubuntu/ bionic-updates universe
    deb-src http://archive.ubuntu.com/ubuntu/ bionic-updates universe
    deb http://archive.ubuntu.com/ubuntu/ bionic multiverse
    deb-src http://archive.ubuntu.com/ubuntu/ bionic multiverse
    deb http://archive.ubuntu.com/ubuntu/ bionic-updates multiverse
    deb-src http://archive.ubuntu.com/ubuntu/ bionic-updates multiverse
    deb http://archive.ubuntu.com/ubuntu/ bionic-backports main restricted universe multiverse
    deb-src http://archive.ubuntu.com/ubuntu/ bionic-backports main restricted universe multiverse
    deb http://security.ubuntu.com/ubuntu/ bionic-security main restricted
    deb-src http://security.ubuntu.com/ubuntu/ bionic-security main restricted
    deb http://security.ubuntu.com/ubuntu/ bionic-security universe
    deb-src http://security.ubuntu.com/ubuntu/ bionic-security universe
    deb http://security.ubuntu.com/ubuntu/ bionic-security multiverse
    deb-src http://security.ubuntu.com/ubuntu/ bionic-security multiverse
    

    将上述官方sources.list文件内容修改为如下:

    deb http://mirrors.tools.aliyun.com/ubuntu bionic main restricted
    deb-src http://mirrors.tools.aliyun.com/ubuntu bionic main restricted
    deb http://mirrors.tools.aliyun.com/ubuntu bionic-updates main restricted
    deb-src http://mirrors.tools.aliyun.com/ubuntu bionic-updates main restricted
    deb http://mirrors.tools.aliyun.com/ubuntu bionic universe
    deb-src http://mirrors.tools.aliyun.com/ubuntu bionic universe
    deb http://mirrors.tools.aliyun.com/ubuntu bionic-updates universe
    deb-src http://mirrors.tools.aliyun.com/ubuntu bionic-updates universe
    deb http://mirrors.tools.aliyun.com/ubuntu bionic multiverse
    deb-src http://mirrors.tools.aliyun.com/ubuntu bionic multiverse
    deb http://mirrors.tools.aliyun.com/ubuntu bionic-updates multiverse
    deb-src http://mirrors.tools.aliyun.com/ubuntu bionic-updates multiverse
    deb http://mirrors.tools.aliyun.com/ubuntu bionic-backports main restricted universe multiverse
    deb-src http://mirrors.tools.aliyun.com/ubuntu bionic-backports main restricted universe multiverse
    deb http://mirrors.tools.aliyun.com/ubuntu bionic-security main restricted
    deb-src http://mirrors.tools.aliyun.com/ubuntu bionic-security main restricted
    deb http://mirrors.tools.aliyun.com/ubuntu bionic-security universe
    deb-src http://mirrors.tools.aliyun.com/ubuntu bionic-security universe
    deb http://mirrors.tools.aliyun.com/ubuntu bionic-security multiverse
    deb-src http://mirrors.tools.aliyun.com/ubuntu bionic-security multiverse
    

    注意,重要事情说3遍:
    该步骤一定是在启动的基础镜像容器内部执行!!!
    该步骤一定是在启动的基础镜像容器内部执行!!!
    该步骤一定是在启动的基础镜像容器内部执行!!!

  • (3)在当前容器中,手动安装dockerfile中列出的其他软件包

    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
    

http://www.kler.cn/a/294566.html

相关文章:

  • Java基础-组件及事件处理(下)
  • 信捷 PLC C语言 POU 指示灯交替灭0.5秒亮0.5秒(保持型定时器)
  • 后端:Aop 面向切面编程
  • 大语言模型在序列推荐中的应用
  • JMeter基础篇
  • 第七部分:2. STM32之ADC实验--AD多通道(AD采集三路传感器模块实验:光敏传感器、热敏传感器、反射式传感器附赠温湿度传感器教程)
  • C语言操作符详解1(含进制转换,原反补码)
  • 消息队列 think-queue tp5.0
  • 使用Docker启动Redis容器并映射端口
  • ChatGPT3.5/4.0新手使用手册,国内中文版使用教程
  • 记录 vue-router 跳转到第一个有权限的菜单的实现方式
  • 【大模型开发】传统向量模型 vs 重排序模型:原理、实现与应用
  • 欺诈文本分类检测(十二):模型导出与部署
  • 八大排序原来也不过如此
  • 【软件逆向】第27课,软件逆向安全工程师之(二)寄存器寻址,每天5分钟学习逆向吧!
  • 【MIT 6.5840/6.824】In Search of an Understandable Consensus Algorithm 学习笔记
  • 如何使用useMemo来优化React组件的性能?
  • 7、关于LoFTR
  • 三维布尔运算对不规范几何数据的兼容处理
  • Linux 中常用的 Vim 命令大全
  • [OpenCV] 数字图像处理 C++ 学习——13Canny边缘检测 附完整代码
  • 828华为云征文 | Flexus X 实例服务器网络性能深度评测
  • 使用PowerShell导出Exchange邮箱大小
  • docker-network
  • GatewayWorker框架的详解和应用
  • [建模已更新]2024数学建模国赛高教社杯A题:“板凳龙” 闹元宵 思路代码文章助攻手把手保姆级