Host and port forwarding in docker compose

Viewed 853

Hopefully an expert in docker networking can help.

I have a django application that integrates with several third-party APIs. These APIs call-back to my application using a hardcoded URL - this means that the callbacks during testing and development use the production hostname and port. I.e. the callback goes to foo.com:443 but in development my app runs on localhost:8000.

I currently get around this by using squid (with a hosts file that maps foo.com to 127.0.0.1), and an iptables command that forwards port 443 to 8000. Then I connect my browser to squid and everything works.

Now I am trying to include this proxy/port forwarding setup in my docker-compose environment.

I already use docker-compose to launch the other services I use for development (postgres, redis, etc). I want to launch the proxy as well.

My current service in docker-compose:

proxy:
  network_mode: host
  build:
    context: .
    dockerfile: proxy.Dockerfile

The dockerfile for squid:

FROM ubuntu
RUN apt update
RUN DEBIAN_FRONTEND="noninteractive" apt-get install -y squid
COPY .squid.conf /squid/.squid.conf
COPY .squid_hosts /squid/.squid_hosts
CMD squid -N -f /squid/squid.conf 

But I still have to run iptables to forward 443 to 8000

So at this point I could start hacking iptables into the squid container, and try to somehow discover the host ip address and forward 127.0.0.1:443 to <host_ip>:8000 but surely docker's networking features can help me?

I have a feeling that I'm doing it wrong, even with the custom hosts file for the squid instance, and that a lot of my problem can be solved on the docker-compose level?

Summary: How can I run a proxy in docker, that when connected to with a browser will forward foo.com:443 to docker-host:8000

EDIT: I got it working using socat to do the port forwarding. So the container runs on a bridge network, with squid as a proxy - squid has a hosts file that sends foo.com to 127.0.0.1 (localhost on the container). Then I listen to port 443 in the container with socat - and relay traffic to 8000 on the host.

So it works. Still waiting for a docker guru to tell me how i'm doing it wrong though :|

0 Answers
Related