插件窝 干货文章 Docker中apt-get update失败:如何正确配置阿里云镜像源?

Docker中apt-get update失败:如何正确配置阿里云镜像源?

Ubuntu multiverse restricted universe 251    来源:    2025-03-12

在Docker容器中使用apt-get update时,如果遇到更新失败的问题,通常是由于默认的软件源服务器在国外,访问速度较慢或不可靠。为了提高更新速度,可以将软件源配置为国内的镜像源,例如阿里云的镜像源。

以下是配置阿里云镜像源的步骤:

1. 修改/etc/apt/sources.list文件

在Docker容器中,首先需要编辑/etc/apt/sources.list文件,将默认的软件源替换为阿里云的镜像源。

步骤:

  1. 进入Docker容器:

    docker exec -it <container_id> /bin/bash
    

    其中<container_id>是你要操作的容器的ID。

  2. 备份原有的/etc/apt/sources.list文件:

    cp /etc/apt/sources.list /etc/apt/sources.list.bak
    
  3. 编辑/etc/apt/sources.list文件:

    vi /etc/apt/sources.list
    

    或者使用nano等其他文本编辑器。

  4. 将文件内容替换为阿里云的镜像源。以下是一些常见的Ubuntu版本的阿里云镜像源配置:

  • Ubuntu 20.04 (Focal Fossa): ```bash deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse

    deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse

    deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse

    deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse

    deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse ```

  • Ubuntu 18.04 (Bionic Beaver): ```bash deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse

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

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

    deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse

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

    根据你使用的Ubuntu版本选择合适的镜像源配置。

  1. 保存并退出编辑器。

2. 更新软件包列表

完成镜像源配置后,运行以下命令更新软件包列表:

apt-get update

3. 验证配置

如果配置正确,apt-get update应该能够成功执行,并且速度会明显提升。

4. 在Dockerfile中配置镜像源(可选)

如果你希望在构建Docker镜像时就使用阿里云的镜像源,可以在Dockerfile中添加以下内容:

RUN sed -i 's|http://archive.ubuntu.com/ubuntu/|http://mirrors.aliyun.com/ubuntu/|g' /etc/apt/sources.list && \
    apt-get update

这样在构建镜像时就会使用阿里云的镜像源。

总结

通过将Docker容器中的软件源配置为阿里云的镜像源,可以显著提高apt-get update的速度和成功率。你可以直接在容器中修改/etc/apt/sources.list文件,或者在Dockerfile中预先配置好镜像源。