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

ops/2024/9/24 13:25:43/

1.问题描述

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

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_testGet: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 InReleaseUnable 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基础镜像源为例,有如下两种可选方案实施。

dockerfile_34">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.ppmy.cn/ops/110717.html

相关文章

将 Parallels Desktop(PD虚拟机)安装在移动硬盘上,有影响吗?

当我们谈论在移动硬盘上安装 Parallels Desktop(简称PD虚拟机)及其对性能的影响时,特别是在运行如Unigraphics这样的资源密集型软件时,用户需要在便携性与性能之间找到最佳平衡。本文将深入探讨PD虚拟机装在移动硬盘有影响吗&…

web基础之信息泄露

1、目录遍历漏洞 (1)原理:本质是没有过滤用户输入的 ../ 相关的目录跳转符,使得攻击者通过目录跳转符来遍历服务器中的任意文件。 (2)题解: eg:根据提示遍历网页目录信息,会在某一个…

使用vuex模仿el-table

1、vuex 在main.js引入 import Vue from vue; import Vuex from vuex;Vue.use(Vuex);const store new Vuex.Store({state: {// 定义要传递的数据datas: []},mutations: {// 定义修改数据的 mutationSET_DATAS(state, newDatas) {state.datas newDatas;}},actions: {// 定义…

# 键盘字母上有下标数字,输入时怎么一键去掉,关闭键盘上的下标数字。‌

键盘字母上有下标数字,输入时怎么一键去掉,关闭键盘上的下标数字。‌ 一、问题描述: 如下图,有的笔记本电脑键盘上,没有数字小键盘,数字小键盘会和字母混和在一起,这样打字时,不容…

python中如何打印日志信息推荐logaid库(强大的日志库)

一、安装 pip install logaid二、使用 只打印,不保存 from logaid import loglog.info(hello world) log.error(hello world) log.warning(hello world) log.fatal(hello world,123,{},[],False)开启超级print from logaid import log log.init(print_proTrue)prin…

ASP.NET Core 入门教学二十三 模型绑定和验证

System.ComponentModel.DataAnnotations 命名空间提供了用于在 .NET 应用程序中进行数据验证和绑定的属性。在 ASP.NET Core 中,这些属性可以与模型绑定和模型验证一起使用,以确保用户输入的数据有效且符合预期的格式。 以下是如何使用 System.Componen…

Springboot工程配置https访问

背景 因为前端工程使用nginx配置了https访问,在https直接请求我们Springboot后端的http接口会报错。那么我们就需要配置使得我们后端的springboot服务支持https访问。 证书生成 在配置springboot工程https之前,我们需要生成自签名证书以及Spring Boot…

汇编:嵌入式软件架构学习资源

成为嵌入式软件架构设计师需要掌握多方面的知识,包括嵌入式系统、实时操作系统、硬件接口、软件设计模式等。 以下是一些推荐的博客和网站,可以帮助你深入学习嵌入式软件架构设计: ### 1. **Embedded.com** - **网址**: [Embedded.com](htt…