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

Docker镜像中的源替换为国内源

Docker镜像中的源替换为国内源

  • 介绍
    • 示例

介绍

使用Dockerfile构建Containers,通常国内网络更新安装包会有网络问题。本文以python:3.11.7-slim-bookworm镜像为例,实现替换镜像源。

示例

要将基于 python:3.11.7-slim-bookworm 的 Docker 镜像的源替换为国内源,可以在 Dockerfile 中添加一些步骤来修改源配置。以下是一个示例 Dockerfile,展示了如何将源替换为国内的阿里云源:

FROM python:3.11.7-slim-bookworm

# 替换为国内源
RUN sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list && \
    sed -i 's|security.debian.org/debian-security|mirrors.aliyun.com/debian-security|g' /etc/apt/sources.list

# 更新包列表并安装必要的包
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
    # 安装你需要的其他包
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# 设置 Python 的 pip 源为国内源
RUN pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/

# 其他 Dockerfile 指令
# ...

在这个示例中,我们做了以下几件事:

  1. 使用 sed 命令将 /etc/apt/sources.list 文件中的默认源地址替换为阿里云的源地址。
  2. 更新包列表并安装必要的包。
  3. 设置 Python 的 pip 源为阿里云的源。

也可以根据需要选择其他国内源,例如清华大学的源:

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

RUN pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple/

源文件不存在情况:
在某些情况下,/etc/apt/sources.list 文件可能不存在或路径不正确。为了确保能够正确替换源,可以先检查文件是否存在,或者直接创建一个新的 sources.list 文件。以下是一个 示例:

FROM python:3.11.7-slim-bookworm

# 检查 /etc/apt/sources.list 是否存在,如果不存在则创建一个新的
RUN test -e /etc/apt/sources.list || echo "deb http://mirrors.aliyun.com/debian bookworm main" > /etc/apt/sources.list && \
    echo "deb http://mirrors.aliyun.com/debian-security bookworm-security main" >> /etc/apt/sources.list && \
    echo "deb http://mirrors.aliyun.com/debian bookworm-updates main" >> /etc/apt/sources.list

# 更新包列表并安装必要的包
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
    # 安装你需要的其他包
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# 设置 Python 的 pip 源为国内源
RUN pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/

# 其他 Dockerfile 指令
# ...

在这个示例中,我们首先检查 /etc/apt/sources.list 文件是否存在,如果不存在,则创建一个新的文件并添加阿里云的源。这样可以确保无论 /etc/apt/sources.list 文件是否存在,都能够正确配置源。

如果你希望使用其他国内源,例如清华大学的源,可以相应地修改 sources.list 文件的内容:

RUN test -e /etc/apt/sources.list || echo "deb http://mirrors.tuna.tsinghua.edu.cn/debian bookworm main" > /etc/apt/sources.list && \
    echo "deb http://mirrors.tuna.tsinghua.edu.cn/debian-security bookworm-security main" >> /etc/apt/sources.list && \
    echo "deb http://mirrors.tuna.tsinghua.edu.cn/debian bookworm-updates main" >> /etc/apt/sources.list

RUN pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple/

http://www.kler.cn/news/290482.html

相关文章:

  • C/C++:函数指针
  • SpringBoot3集成Spring Authorization Server实现SSO单点登录
  • uniapp小程序实现横屏手写签名
  • 宠物智能家居监测器的融合
  • LeetCode 606.根据二叉树创建字符串
  • 开源vscode AI插件
  • 什么是基于云的 SIEM
  • MuseTalk模型构建指南
  • css改变鼠标样式
  • 论文速读|BiGym:一款基于演示的移动双手操作机器人基准
  • IO进程练习:请在linux 利用c语言编程实现两个线程按照顺序依次输出”ABABABAB......“
  • pdf转cad软件,5款快速上手转换软件分享
  • vue el-tree主键id重复 添加自增id 以及原相同节点同步勾选 同步操作
  • 在Ubuntu上使用apt工具安装RabbitMQ
  • 单链表的问题(2)
  • NCBI-get-GCFIDs_fast.py
  • 基于贝叶斯优化CNN-LSTM网络的数据分类识别算法matlab仿真
  • 戴尔科技领涨市场,AI服务器需求成关键驱动力
  • Error when attempting to add data source to Azure OpenAI api
  • uniapp css样式盒子上面的箭头样式
  • ElasticSearch学习笔记(四)分页、高亮、RestClient查询文档
  • 20240904日报
  • C#如何查看/写入日志到Windows事件查看器
  • c++学习笔记(5)
  • .gitignore 修改问题
  • 力扣151.反转字符串中的单词
  • 输电线路分布式故障诊断系统:分布式智慧网络的构建
  • c#笔记5 详解事件的内置类型EventHandler、windows事件在winform中的运用
  • 【IEEE出版 | 往届会后3个月EI检索】第三届IEEE云计算、大数据应用与软件工程国际学术会议 (IEEE-CBASE 2024,10月18-20)
  • 6 - Shell编程之sed与awk编辑器