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

$ 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
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值