how to run docker build behind a proxy (docker build host machine behind a proxy)

 most time, my work environment is behind proxy(to jump out Chixx Gateway). Now there is a problem. when we run docker run. the build will fail if we try to access the internet.

the solution is as below:

1. when you write dockerfile, you should write like this:

#######################################################
# Dockerfile to build session DB proxy CI base image
#######################################################
# Base image is centos 7
FROM centos:7
# Author: Max Cong
MAINTAINER Max Cong <savagecm@qq.com>
# Proxy variable for yum download etc to work
ARG PROXY
# Set http and https proxy according to PROXY argument
ENV http_proxy ${PROXY:-}
ENV https_proxy ${PROXY:-}
# Setup a temp directory for Git repo
ENV GITCACHE /var/cache/gitcache
# Bash profile for root
COPY ./.bash_profile /root/
# Use shell script to modify yum proxy
COPY ./cfg_yum_proxy.sh /
# Download and install dependent packages
# for session DB proxy CI build and test
RUN cp /etc/yum.conf /etc/yum.conf.ofc \
    && /cfg_yum_proxy.sh $http_proxy \
    && yum -y install \
    boost-devel \
    cmake \
    gcc \
    gcc-c++ \
    git \
    libcurl-devel \
    libevent-devel \
    make \
    valgrind \
    && mkdir -p ${GITCACHE} \
    && cd ${GITCACHE} \
    && git clone https://github.com/redis/hiredis.git \
    && git clone https://github.com/antirez/redis.git \
    && git clone https://github.com/lloyd/yajl.git \
    && cd ${GITCACHE}/hiredis && make install \
    && cd ${GITCACHE}/redis && make install \
    && mv /etc/yum.conf.ofc /etc/yum.conf \
    && rm /cfg_yum_proxy.sh \
    && yum clean all
# unset proxy
ENV http_proxy ""
ENV https_proxy ""
# Setup library search path
ENV LD_LIBRARY_PATH /usr/lib64:/usr/local/lib64:/usr/lib:/usr/local/lib
# Useable shell
CMD ["/bin/bash"]
2. the docker run command

sudo docker build --build-arg PROXY="172.17.0.1:8000" .

3.  the proxy should be set to the docker0 in your host machine

[mcong@SessionDBProxy-CI docker]$ ifconfig
docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 0.0.0.0
        inet6 fe80::42:8dff:fea5:ac95  prefixlen 64  scopeid 0x20<link>
        ether 02:42:8d:a5:ac:95  txqueuelen 0  (Ethernet)
        RX packets 60694  bytes 3190979 (3.0 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 60799  bytes 189773555 (180.9 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

why this work?

the answer is docker build use the default bridge network, it is bind to docker0 in your host machine:

[mcong@SessionDBProxy-CIRAPPORT_dbwrapper]$ ifconfig

docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500

       inet 172.17.0.1  netmask 255.255.0.0  broadcast 0.0.0.0



  {
        "Name": "bridge",
        "Id": "5e6943bf0eeb9ad473ea45637b1e625975687a895fece756ee2f5410d2021fe4",
        "Created": "2017-03-07T17:06:34.371450497+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.17.0.0/16",
                    "Gateway": "172.17.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Containers": {
            "150b40335fc0e322c42c994def35a83d1939323b1fa41455b66acd2702b4830b": {
                "Name": "blissful_snyder",
                "EndpointID": "e18f58f4476c6c41b1feca7ca4548d1bdafedc66639d32e521e71b0bea96399f",
                "MacAddress": "02:42:ac:11:00:02",
                "IPv4Address": "172.17.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {
            "com.docker.network.bridge.default_bridge": "true",
            "com.docker.network.bridge.enable_icc": "true",
            "com.docker.network.bridge.enable_ip_masquerade": "true",
            "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
            "com.docker.network.bridge.name": "docker0",
            "com.docker.network.driver.mtu": "1500"
        },
        "Labels": {}
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值