Docker、Linux网络代理设置

网络代理

linux机器通过windows主机代理访问外网

windows机器借用 CCProxy 软件,官网下载免费版(http://www.ccproxy.com/)

CCProxy 默认使用808端口,如果端口冲突可以在设置处修改

在帐号处添加允许的linux机器ip,也可以直接允许所有ip,其中ip段用-符号连接

再获取windows电脑本机的ipv4的ip

linux端配置

复制代码
$ vim /etc/profile
export http_proxy=http://192.168.50.29:808
export https_proxy=http://192.168.50.29:808
export ftp_proxy=http://192.168.50.29:808

$ source /etc/profile

$ curl -v www.baidu.com

当某些域名不需要使用代理时,也可以使用no_proxy

复制代码
$ vim /etc/profile
export http_proxy=http://192.168.50.29:808
export https_proxy=http://192.168.50.29:808
export ftp_proxy=http://192.168.50.29:808
export no_proxy=www.baidu.com

$ source /etc/profile

linux机器通过linux机器代理访问外网

复制代码
centos
$ yum -y install tinyproxy
ubuntu 
$ apt-get -y install tinyproxy

如果报错,比如:No package tinyproxy available.先安装epel,再安装tinyproxy

复制代码
$ yum install -y epel-release
$ yum -y install tinyproxy

配置tinyproxy

复制代码
$ vi /etc/tinyproxy/tinyproxy.conf
Port 8888 #默认端口8888,如果冲突了修改
Allow 127.0.0.1 #注释掉此行,代表允许所有ip,ip段可设置

启动tinyproxy,可以查看相关日志

复制代码
$ systemctl start tinyproxy
$ tail -f /var/log/tinyproxy/tinyproxy.log 

需要配置代理的linux服务器

复制代码
$ vim /etc/profile
export http_proxy=http://192.168.60.29:8888
export https_proxy=http://192.168.60.29:8888
export ftp_proxy=http://192.168.60.29:8888

$ source /etc/profile

Docker网络代理设置

在一些实验室环境,服务器没有直接连接外网的权限,需要通过网络代理。我们通常会将网络代理直接配置在/etc/profile之类的配置文件中,这对于大部分访问外网操作都是可行的。然而,docker命令却使用不了这些代理。比如docker pull时需要从外网下载镜像,就会出现如下错误

复制代码
$ docker pull hello-world

Unable to find image 'hello-world:latest' locally
Pulling repository docker.io/library/hello-world
docker: Network timed out while trying to connect to https://index.docker.io/v1/repositories/library/hello-world/images. You may want to check your internet connection or if you are behind a proxy..
See 'docker run --help'.

1、为docker服务创建一个内嵌的systemd目录

复制代码
$ mkdir -p /etc/systemd/system/docker.service.d

2、创建/etc/systemd/system/docker.service.d/http-proxy.conf文件,并添加HTTP_PROXY环境变量。其中proxy-addrproxy-port分别改成实际情况的代理地址和端口,如果还有内部的不需要使用代理来访问的Docker registries,那么还需要制定NO_PROXY环境变量:

复制代码
[Service]
Environment="HTTP_PROXY=http://[proxy-addr]:[proxy-port]/" "HTTPS_PROXY=https://[proxy-addr]:[proxy-port]/" "NO_PROXY=localhost,127.0.0.1,docker-registry.somecorporation.com"

3、更新配置,重启docker

复制代码
$ systemctl daemon-reload
$ systemctl restart docker

Docker容器内代理设置

上述设置的网络代理适用于Docker获取镜像,但是当Docker容器内需要访问外网时,需要设置~/.docker/config.json

json 复制代码
$ vi ~/.docker/config.json
{
 "proxies": {
   "default": {
     "httpProxy": "http://proxy.example.com:3128",
     "httpsProxy": "https://proxy.example.com:3129",
     "noProxy": "*.test.example.com,.example.org,127.0.0.0/8"
   }
 }
}

$ systemctl daemon-reload
$ systemctl restart docker
相关推荐
一叶龙洲4 小时前
wslg打开Ubuntu24.04默认打开图形界面
linux·服务器·数据库·ubuntu
星恒讯工业路由器5 小时前
5G FWA技术演进与趋势
网络·5g·信息与通信·4g·5g fwa·3gpp规范·fwa趋势
敖行客 Allthinker5 小时前
Parallels Ubuntu虚拟机项目如何让手机访问?完整解决方案
linux·运维·ubuntu
BullSmall6 小时前
Anolis OS 8.10 完整安装 Docker CE(生产可用,解决 podman 冲突)
docker·容器·podman
keyipatience7 小时前
线程栈与TLS和线程互斥
java·linux·服务器·开发语言·ubuntu
j7~7 小时前
【Linux】二十二.《Linux 信号机制完全指南:表示、捕捉与处理》
linux·信号处理·volatile·可入重函数·保存信号
观山岳五楼8 小时前
Ubuntu 24 怎么使用Ubuntu 20 的镜像源
linux·运维·ubuntu
星夜夏空998 小时前
网络编程(1)
服务器·网络
上海云盾-小余8 小时前
中小站点防护避坑:低价高防服务存在的各类安全短板剖析
网络·爬虫·安全·ddos
m0_715646768 小时前
20260720
linux·arm