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.

  • 1
    "bear bone"? Do you mean bare-bones? Or is it some new distro? Also, questions on distros other than Ubuntu (including Debian and other Debian derivatives) should be asked on Unix & Linux. – muru Oct 9 '18 at 9:34
  • @muru, fixed. Thanks; Also the same question is for ubuntu:latest. – Chen Levy Oct 9 '18 at 11:16
  • in that case, make it about ubuntu:latest. There's no guarantee that a Debian image and an Ubuntu image are going to have the same tools installed. – muru Oct 9 '18 at 14:00
  • @muru, tested with ubuntu and updated the question. – Chen Levy Oct 10 '18 at 7:51
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.

  • As for the /dev/tcp/..., I first needed to mknod /dev/tcp c 30 36, and then read -t0 < /dev/tcp/<proxy-ip>/<proxy-port> it help me distinguish between <port-ip>/<proxy-port> and <proxy-ip>/<other-port>, but it hangs on <other-ip>/<proxy-port>. – Chen Levy Oct 10 '18 at 9:19

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.