0

I setup an internal apt proxy, that is mostly used by my Docker containers for our development environment.

And thus in my Dockerfile I might have:

FROM ubuntu
RUN echo 'Acquire::http::Proxy "http://<proxy-ip>:<proxy-port>/";' > /etc/apt/apt.conf.d/proxy.conf

But this will work only when I build my Docker image inside the firewall.

Since I also want to build that Docker image on a cloud service host, that will not be able to access : nor it will make sense to do so, I thought I will put a check before configuring the apt-proxy client:

RUN <check-connection> http://<proxy-ip>:<proxy-port>/ && \
    echo 'Acquire::http::Proxy "http://<proxy-ip>:<proxy-port>/";' > /etc/apt/apt.conf.d/proxy.conf

Since I am running this on a bare-bones ubuntu Docker image, when apt is not yet configured, I am somewhat limited in what the <check-connection> tool might be, to verify that http://<proxy-ip>:<proxy-port>/ is responding as expected.

The best solution I got so far is to ask apt itself to check if it can connect:

apt -o Acquire::http::Proxy="http://<proxy-ip>:<proxy-port>/" update

This works pretty well when my proxy can respond, but takes a long time to fail if not.

The next thing I did is use the timeout utility to limit time to fail:

timeout 10 apt -o Acquire::http::Proxy="http://<proxy-ip>:<proxy-port>/" update

But the timeout (here 10 seconds) might be both too short and too long.

So my question is: On a bare-bones Debian host (read Docker ubuntu image), what is the best (quickest and most robust) way to check if an apt-proxy is reachable?


Edit: The original question mentioned a debian:9 docker image, but for the sake of complying with the ask-ubuntu's guidelines I tested it also with ubuntu:latest, got the same result, and updated the question.

Chen Levy
  • 121

1 Answers1

1

You can test if a TCP connection can be made to to a given host and port using bash:

bash -c 'read -t0 < /dev/tcp/<proxy-ip>/<proxy-port>'

This doesn't check if the service on the other end is a proxy, but merely if there is something listening on the other end.

Another option would be to run apt-get for a single repository, for example, by writing deb http://archive.ubuntu.com/ubuntu $(lsb_release -sc) restricted to a file and using it as the source.

muru
  • 207,228