matrix
matrix

Reputation: 349

Use proxy inside a Docker container

My host server need a proxy to talk to the outside world. It's defined in env like http_proxy=http://10.10.123.123:8080 https_proxy=http://10.10.123.123:8080. I run an image tensorflow/tensorflow, container named tf1.

Inside tf1(by exec into the container), I would like to install some package like grpcio and tensorflow-serving-api with pip, but fail with network error.

How can I use the proxy of the host inside the container? I have tried exec with -e option but fail because of low version docker, so I don't know whether it works.

OS: CentOS 7.2, Docker:1.12.3

Upvotes: 5

Views: 8489

Answers (3)

and1er
and1er

Reputation: 1101

Helped for me to export the proxy settings in the same RUN instruction just before the apt-get in Dockerfile

FROM ubuntu

RUN export "http_proxy=http://host:port" \
    && export "https_proxy=http://host:port" \
    && apt-get update \
    && apt-get install -y SOME-PACKAGE

After that Ubuntu system in container was able to install the packages.

The noted way makes the proxy available only for this RUN instruction.

If the whole image should use the proxy the ENV instruction should be used:

FROM ubuntu

ENV http_proxy http://host:port
ENV https_proxy http://host:port

RUN apt-get update \
    && apt-get install -y SOME-PACKAGE

ENTRYPOINT [ "printenv" ]

Building the image $ docker image build -t test . and running the container $ docker run test will show that the proxy persist

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=8513fc1fb635
http_proxy=http://host:port
https_proxy=http://host:port
HOME=/root

Upvotes: 2

yamenk
yamenk

Reputation: 51738

My recommendation for working with proxies is to install a tool that transparently routes all traffic to the proxy. A popular tool for Linux is redsocks.

Redsocks can be installed on the host as illustrated here. There is also a docker image to get redsocks in case you don't want to install it manually.

Once you install redsocks, all traffic from your host or containers will be redirected to the proxy and you don't need any more to configure proxy env variables.

Upvotes: 1

Girdhar Sojitra
Girdhar Sojitra

Reputation: 678

You can use docker-proxy for using host proxy inside container from https://github.com/silarsis/docker-proxy

Upvotes: 2

Related Questions