在docker中ubuntu的apt-get慢的优化办法

1. 选择一个更快的镜像源

Debian(和 Ubuntu)的官方镜像源可能因地理位置和网络条件而速度不同。选择一个地理位置靠近你的镜像源通常能显著提高下载速度。

  1. 查找最近的镜像源:你可以访问 Debian 的镜像列表来找到一个离你较近的镜像源。

  2. 修改 sources.list 文件:编辑 /etc/apt/sources.list 文件,将现有的源替换为选择的镜像源。例如,如果你选择了中国的清华大学镜像源,可以这样配置:

    deb http://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free
    deb-src http://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free
    
    deb http://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free
    deb-src http://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free
    
    deb http://mirrors.tuna.tsinghua.edu.cn/debian-security bookworm-security main contrib non-free
    deb-src http://mirrors.tuna.tsinghua.edu.cn/debian-security bookworm-security main contrib non-free
    

    修改 Dockerfile 来自定义源:

    FROM debian:bookworm
    
    RUN sed -i 's/deb.debian.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list && \
        sed -i 's/security.debian.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list && \
        apt-get update && apt-get install -y YOUR_PACKAGE
    

    替换 YOUR_PACKAGE 为你需要安装的包。

2.使用 apt-getAcquire::http 选项

通过使用 Acquire::http 选项,可以调整 apt-get 的一些网络参数,例如重试次数或超时时间,来尝试解决网络缓慢的问题。

在 Dockerfile 中使用这种方式:

FROM debian:bookworm

RUN apt-get -o Acquire::http::Timeout=5 -o Acquire::Retries=3 update && \
    apt-get install -y YOUR_PACKAGE

3. 其他通用优化

  • 并行下载apt-get 支持并行下载,可以在 /etc/apt/apt.conf.d 下创建一个配置文件来启用这一特性。

    echo 'Acquire::http {No-Cache=True;}; Acquire::Retries=3; Acquire::https::Timeout=5; Acquire::http::Timeout=5;' > /etc/apt/apt.conf.d/99parallel
    
  • 清理缓存:运行 apt-get update 后,清理不必要的缓存,可以减少 Docker 图像的大小。

    RUN apt-get update && apt-get install -y YOUR_PACKAGE && \
        rm -rf /var/lib/apt/lists/*
    

如果在你的 Debian 系统中没有 /etc/apt/sources.list 文件,但有 /etc/apt/sources.list.d/debian.source 文件,这表明你的软件源配置被分散在了 sources.list.d 目录下的多个文件中。这是一种组织 APT 源的方式,使得管理多个源更加方便和清晰。

4.修改 /etc/apt/sources.list.d/debian.source

  1. 编辑 debian.source 文件:直接编辑这个文件,将现有的源替换为更快的镜像源。使用 nano 或你喜欢的任何文本编辑器来编辑这个文件。例如:

    sudo nano /etc/apt/sources.list.d/debian.source
    
  2. debian.source 中替换内容:找到文件中的现有 Debian 源,并将它们替换为你选择的镜像源。以清华大学的镜像源为例,你可能会把内容改为:

    deb http://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free
    deb-src http://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free
    
    deb http://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free
    deb-src http://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free
    
    deb http://mirrors.tuna.tsinghua.edu.cn/debian-security bookworm-security main contrib non-free
    deb-src http://mirrors.tuna.tsinghua.edu.cn/debian-security bookworm-security main contrib non-free
    

    保存并退出编辑器。

5.如果 /etc/apt/sources.list.d/ 中有多个文件

有时 /etc/apt/sources.list.d/ 目录下会有多个文件,你应该查看这些文件来确定所有的 APT 源配置。

  1. 列出所有源配置文件

    ls /etc/apt/sources.list.d/
    
  2. 逐个检查和修改

    对于目录下的每一个 .list.source 文件,重复上述的编辑过程。

    sudo nano /etc/apt/sources.list.d/your-other-sources.list
    

6.在 Docker 中修改源(debian修改办法)

如果你在 Dockerfile 中设置 Debian 环境,没有 sources.list 而是 /etc/apt/sources.list.d/debian.source,你可以按以下方式修改 Dockerfile:

FROM debian:bookworm

# 替换源
RUN sed -i 's|deb.debian.org|mirrors.tuna.tsinghua.edu.cn|g' /etc/apt/sources.list.d/debian.source && \
    sed -i 's|security.debian.org|mirrors.tuna.tsinghua.edu.cn|g' /etc/apt/sources.list.d/debian.source

# 安装软件
RUN apt-get update && apt-get install -y YOUR_PACKAGE

# 清理缓存
RUN rm -rf /var/lib/apt/lists/*
  • 30
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值