于 2018 年 2 月 10 日更新
有了 docker 选项--config 中的新功能,您不再需要在 Dockerfile 中设置 Proxy。您可以在有或没有代理设置的情况下在公司环境内外使用相同的 Dockerfile。
命令docker run 选项:
--config string Location of client config files (default "~/.docker")
或环境变量DOCKER_CONFIG
`DOCKER_CONFIG` The location of your client configuration files.
$ export DOCKER_CONFIG=~/.docker
https://docs.docker.com/engine/reference/commandline/cli/
https://docs.docker.com/network/proxy/
我建议用httpProxy, httpsProxy, ftpProxy和noProxy设置代理(官方文档漏掉了变量ftpProxy,这个变量有时很有用)
{
"proxies":
{
"default":
{
"httpProxy": "http://192.168.1.12:3128",
"httpsProxy": "http://192.168.1.12:3128",
"ftpProxy": "http://192.168.1.12:3128",
"noProxy": "*.test.example.com,.example2.com,127.0.0.0/8"
}
}
}
为您的代理环境调整代理IP和端口并保存到~/.docker/config.json
设置好之后,就可以正常运行docker build和docker run了。
$ cat Dockerfile
FROM alpine
$ docker build -t demo .
$ docker run -ti --rm demo env|grep -ri proxy
(standard input):HTTP_PROXY=http://192.168.1.12:3128
(standard input):http_proxy=http://192.168.1.12:3128
(standard input):HTTPS_PROXY=http://192.168.1.12:3128
(standard input):https_proxy=http://192.168.1.12:3128
(standard input):NO_PROXY=*.test.example.com,.example2.com,127.0.0.0/8
(standard input):no_proxy=*.test.example.com,.example2.com,127.0.0.0/8
(standard input):FTP_PROXY=http://192.168.1.12:3128
(standard input):ftp_proxy=http://192.168.1.12:3128
旧答案(已停用)
Dockerfile 中的以下设置对我有用。我在CoreOS、Vagrant 和boot2docker 中进行了测试。假设代理端口为3128
###在 Centos 中:
ENV http_proxy=ip:3128
ENV https_proxy=ip:3128
###在 Ubuntu 中:
ENV http_proxy 'http://ip:3128'
ENV https_proxy 'http://ip:3128'
注意格式,有的有http,有的没有,有的只有单一配额。如果 IP 地址为 192.168.0.193,则设置为:
###在 Centos 中:
ENV http_proxy=192.168.0.193:3128
ENV https_proxy=192.168.0.193:3128
###在 Ubuntu 中:
ENV http_proxy 'http://192.168.0.193:3128'
ENV https_proxy 'http://192.168.0.193:3128'
###如果需要在coreos中设置代理,例如拉取镜像
cat /etc/systemd/system/docker.service.d/http-proxy.conf
[Service]
Environment="HTTP_PROXY=http://192.168.0.193:3128"