下面几种方式都支持8.x到11.x和12.x最新版本,即写入镜像源时自动获取系统版本,是版本自适应写法。
阿里云Debian镜像源地址:https://developer.aliyun.com/mirror/debian
shell中版本自适应写法(支持所有版本)
先备份再创建新的sources.list文件
mv /etc/apt/sources.list{,.backup`date +%Y%m%d%H%M%S`} \
&& cat > /etc/apt/sources.list <<EOF
deb https://mirrors.aliyun.com/debian/ $(cat /etc/os-release | grep ‘VERSION_CODENAME’ | awk -F ‘=’ ‘{print $2}’) main non-free contrib
deb-src https://mirrors.aliyun.com/debian/ $(cat /etc/os-release | grep ‘VERSION_CODENAME’ | awk -F ‘=’ ‘{print $2}’) main non-free contrib
deb https://mirrors.aliyun.com/debian-security/ $(cat /etc/os-release | grep ‘VERSION_CODENAME’ | awk -F ‘=’ ‘{print $2}’)-security main
deb-src https://mirrors.aliyun.com/debian-security/ $(cat /etc/os-release | grep ‘VERSION_CODENAME’ | awk -F ‘=’ ‘{print $2}’)-security main
deb https://mirrors.aliyun.com/debian/ $(cat /etc/os-release | grep ‘VERSION_CODENAME’ | awk -F ‘=’ ‘{print $2}’)-updates main non-free contrib
deb-src https://mirrors.aliyun.com/debian/ $(cat /etc/os-release | grep ‘VERSION_CODENAME’ | awk -F ‘=’ ‘{print $2}’)-updates main non-free contrib
deb https://mirrors.aliyun.com/debian/ $(cat /etc/os-release | grep ‘VERSION_CODENAME’ | awk -F ‘=’ ‘{print $2}’)-backports main non-free contrib
deb-src https://mirrors.aliyun.com/debian/ $(cat /etc/os-release | grep ‘VERSION_CODENAME’ | awk -F ‘=’ ‘{print $2}’)-backports main non-free contrib
EOF
以Debian为基础镜像的docker构建加速
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
# 为apt配置镜像源 加速构建
RUN cp /etc/apt/sources.list.d/debian.sources /etc/apt/sources.list.d/debian.sources.bak \
&& sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list.d/debian.sources
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
clang zlib1g-dev
更多Dockerfile中用法示例
以下是RUN指令写法示例
RUN echo “deb https://mirrors.aliyun.com/debian/ $(cat /etc/os-release | grep ‘VERSION_CODENAME’ | awk -F ‘=’ ‘{print $2}’) main non-free contrib \
\ndeb-src https://mirrors.aliyun.com/debian/ $(cat /etc/os-release | grep ‘VERSION_CODENAME’ | awk -F ‘=’ ‘{print $2}’) main non-free contrib \
\ndeb https://mirrors.aliyun.com/debian-security/ $(cat /etc/os-release | grep ‘VERSION_CODENAME’ | awk -F ‘=’ ‘{print $2}’)-security main \
\ndeb-src https://mirrors.aliyun.com/debian-security/ $(cat /etc/os-release | grep ‘VERSION_CODENAME’ | awk -F ‘=’ ‘{print $2}’)-security main \
\ndeb https://mirrors.aliyun.com/debian/ $(cat /etc/os-release | grep ‘VERSION_CODENAME’ | awk -F ‘=’ ‘{print $2}’)-updates main non-free contrib \
\ndeb-src https://mirrors.aliyun.com/debian/ $(cat /etc/os-release | grep ‘VERSION_CODENAME’ | awk -F ‘=’ ‘{print $2}’)-updates main non-free contrib \
\ndeb https://mirrors.aliyun.com/debian/ $(cat /etc/os-release | grep ‘VERSION_CODENAME’ | awk -F ‘=’ ‘{print $2}’)-backports main non-free contrib \
\ndeb-src https://mirrors.aliyun.com/debian/ $(cat /etc/os-release | grep ‘VERSION_CODENAME’ | awk -F ‘=’ ‘{print $2}’)-backports main non-free contrib” \
> /etc/apt/sources.list
一键生成dockerfile脚本示例
使用cat命令一键生成Dockerfile文件
cat > Dockerfile <<EOF
FROM debian:latest
USER root
RUN cat /etc/os-release | grep VERSION_CODENAME | awk -F = ‘{print \$2}’
RUN echo “deb http://mirrors.aliyun.com/debian/ \$(cat /etc/os-release | grep VERSION_CODENAME | awk -F = ‘{print \$2}’) main non-free contrib”
RUN echo “deb https://mirrors.aliyun.com/debian/ \$(cat /etc/os-release | grep ‘VERSION_CODENAME’ | awk -F ‘=’ ‘{print \$2}’) main non-free contrib \
\ndeb-src https://mirrors.aliyun.com/debian/ \$(cat /etc/os-release | grep ‘VERSION_CODENAME’ | awk -F ‘=’ ‘{print \$2}’) main non-free contrib \
\ndeb https://mirrors.aliyun.com/debian-security/ \$(cat /etc/os-release | grep ‘VERSION_CODENAME’ | awk -F ‘=’ ‘{print \$2}’)-security main \
\ndeb-src https://mirrors.aliyun.com/debian-security/ \$(cat /etc/os-release | grep ‘VERSION_CODENAME’ | awk -F ‘=’ ‘{print \$2}’)-security main \
\ndeb https://mirrors.aliyun.com/debian/ \$(cat /etc/os-release | grep ‘VERSION_CODENAME’ | awk -F ‘=’ ‘{print \$2}’)-updates main non-free contrib \
\ndeb-src https://mirrors.aliyun.com/debian/ \$(cat /etc/os-release | grep ‘VERSION_CODENAME’ | awk -F ‘=’ ‘{print \$2}’)-updates main non-free contrib \
\ndeb https://mirrors.aliyun.com/debian/ \$(cat /etc/os-release | grep ‘VERSION_CODENAME’ | awk -F ‘=’ ‘{print \$2}’)-backports main non-free contrib \
\ndeb-src https://mirrors.aliyun.com/debian/ \$(cat /etc/os-release | grep ‘VERSION_CODENAME’ | awk -F ‘=’ ‘{print \$2}’)-backports main non-free contrib” \
> /etc/apt/sources.list
RUN cat /etc/apt/sources.list
# 安装curl
RUN apt-get update \
&& apt-get install -y curl \
&& rm -rf /var/lib/apt/lists/*
USER rootless
EOF
发表回复