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-addr]和[proxy-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
相关推荐
Betty’s Sweet3 分钟前
[Linux]:线程(三)
linux·线程·信号量·生产者消费者模型
zixingcai16 分钟前
dockertop提示Failed to fetch extensions
docker
程序员南飞2 小时前
ps aux | grep smart_webrtc这条指令代表什么意思
java·linux·ubuntu·webrtc
StrokeAce2 小时前
linux桌面软件(wps)内嵌到主窗口后的关闭问题
linux·c++·qt·wps·窗口内嵌
热爱嵌入式的小许6 小时前
Linux基础项目开发1:量产工具——显示系统
linux·运维·服务器·韦东山量产工具
小堃学编程6 小时前
计算机网络(十) —— IP协议详解,理解运营商和全球网络
网络·tcp/ip·计算机网络
IPFoxy6669 小时前
探索路由器静态IP的获取方式
网络·智能路由器
雪域迷影9 小时前
PostgreSQL Docker Error – 5432: 地址已被占用
数据库·docker·postgresql
menge23339 小时前
VLAN:虚拟局域网
网络·智能路由器
ZachOn1y9 小时前
计算机网络:计算机网络概述 —— 初识计算机网络
网络·计算机网络·知识点汇总·考研必备