Ubuntu 22.04+ 使用 deb822 格式源文件,路径默认是 /etc/apt/sources.list.d/ubuntu.sources,不再是旧版 sources.list。
一、Ubuntu 22.04 Jammy 阿里云源 Dockerfile
dockerfile
FROM ubuntu:22.04
# 清空原有源,写入阿里云deb822格式源
RUN echo > /etc/apt/sources.list.d/ubuntu.sources && \
cat > /etc/apt/sources.list.d/ubuntu.sources <<EOF
Types: deb
URIs: https://mirrors.aliyun.com/ubuntu
Suites: jammy jammy-security jammy-updates jammy-backports
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
EOF
# 更新软件源
RUN apt update && apt upgrade -y
二、Ubuntu 24.04 Noble 阿里云源 Dockerfile
dockerfile
FROM ubuntu:24.04
RUN echo > /etc/apt/sources.list.d/ubuntu.sources && \
cat > /etc/apt/sources.list.d/ubuntu.sources <<EOF
Types: deb
URIs: https://mirrors.aliyun.com/ubuntu
Suites: noble noble-security noble-updates noble-backports
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
EOF
RUN apt update
三、国内内网镜像(可选,阿里内网环境)
将 https://mirrors.aliyun.com/ubuntu 替换为
https://mirrors.aliyuncs.com/ubuntu
四、如果需要同时注释掉旧版 sources.list(兼容写法)
dockerfile
RUN sed -i 's/^/#/' /etc/apt/sources.list && \
echo > /etc/apt/sources.list.d/ubuntu.sources && \
cat > /etc/apt/sources.list.d/ubuntu.sources <<EOF
Types: deb
URIs: https://mirrors.aliyun.com/ubuntu
Suites: jammy jammy-security jammy-updates jammy-backports
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
EOF
五、极简一行版(直接 COPY 文件)
- 本地新建
ubuntu.sources文件粘贴对应版本源 - Dockerfile:
dockerfile
COPY ubuntu.sources /etc/apt/sources.list.d/ubuntu.sources
RUN apt update
正文完
可以使用微信扫码关注公众号(ID:xzluomor)