常用镜像源或代理

Kevin2li大约 3 分钟Box

Ubuntu

阿里云镜像:https://developer.aliyun.com/mirror/ubuntuopen in new window

清华源镜像:https://mirror.tuna.tsinghua.edu.cn/help/ubuntu/open in new window

方法:

  • 修改配置文件

配置文件路径:/etc/apt/sources.list

# 备份
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak

# 替换内容
sudo tee /etc/apt/sources.list << EOF

Ubuntu22.04示例:

# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse

# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
# # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse

deb http://security.ubuntu.com/ubuntu/ jammy-security main restricted universe multiverse
# deb-src http://security.ubuntu.com/ubuntu/ jammy-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
# # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse

Debian

清华源:https://mirror.tuna.tsinghua.edu.cn/help/debian/open in new window

Debian11示例:

# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free

deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free

deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-backports main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-backports main contrib non-free

# deb https://mirrors.tuna.tsinghua.edu.cn/debian-security bullseye-security main contrib non-free
# # deb-src https://mirrors.tuna.tsinghua.edu.cn/debian-security bullseye-security main contrib non-free

deb https://security.debian.org/debian-security bullseye-security main contrib non-free
# deb-src https://security.debian.org/debian-security bullseye-security main contrib non-free

pip

阿里云:https://developer.aliyun.com/mirror/pypi?spm=a2c6h.13651102.0.0.59f71b112m674Eopen in new window
清华源: https://mirror.tuna.tsinghua.edu.cn/help/pypi/open in new window

  • 终端临时使用
# 阿里云
pip install <package> -i https://mirrors.aliyun.com/pypi/simple/

# 清华源
pip install <package> -i https://pypi.tuna.tsinghua.edu.cn/simple
  • 写入配置文件

配置文件路径:~/.pip/pip.conf

内容:

[global]
index-url = https://mirrors.aliyun.com/pypi/simple/

[install]
trusted-host=mirrors.aliyun.com

conda

清华源:https://mirror.tuna.tsinghua.edu.cn/help/anaconda/\open in new window

  • 写入配置文件
    配置文件路径:~/.condarc
channels:
  - defaults
show_channel_urls: true
default_channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
  conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  pytorch-lts: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

docker

参考: 知乎 | Docker网络问题经验谈open in new window

镜像加速站

阿里云:https://help.aliyun.com/document_detail/60750.htmlopen in new window

  • 写入配置文件

配置文件路径:/etc/docker/daemon.json (没有则新建)

添加下面条目:

{
    "registry-mirrors": ["<镜像加速器地址>"]
}    

登录容器镜像服务控制台open in new window,在左侧导航栏选择镜像工具 > 镜像加速器,在镜像加速器页面获取镜像加速地址。

重启服务:

sudo systemctl daemon-reload
sudo systemctl restart docker

验证:

docker info

能找到刚才自己设的Registry Mirrors,说明设置成功。

编译时使用代理

遇到一些git/pip操作,仍然无法连接,首先将宿主机配置好代理,编译时:

docker build -t image_name . --network host \
        --build-arg http_proxy=${http_proxy}\
        --build-arg https_proxy=${https_proxy}

运行时使用代理

首先将宿主机配置好代理,运行时:

docker run -it --network=host \
        --env http_proxy=${http_proxy}\
        --env https_proxy=${https_proxy} image_name

docker-compose使用代理

version: "3.0"
services:
    my_service:
        image: image_name
        build:
            context: ./
            dockerfile: Dockerfile
        network_mode: "host" #注意使用此模式不需要端口映射,否则会报错
        environment:
            - "http_proxy=127.0.0.1:8999"
            - "https_proxy=127.0.0.1:8999"

容器内使用代理

首先宿主机配好代理,假设运行在7890端口.

# 容器内执行
export ALL_PROXY="http://host.docker.internal:7890"

npm

参考: https://npmmirror.com/open in new window

 npm install -g <package> --registry=https://registry.npmmirror.com

Go

参考:https://goproxy.cn/open in new window

go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct