drstuggels
drstuggels

Reputation: 132

Have a docker container use a proxy inside another docker container with docker-compose

Let's say I have two docker containers. One containing an image with a proxy on 0.0.0.0:PORT. The other container needs to use that proxy.

To clarify, I DO NOT want to use the host for anything here. So no network_mode: host and running the proxy on the host machine. I want to containerize both the proxy and the service that will use that proxy.

I use docker-compose so if you could provide me with an example of that, I would be glad.

If you need to know, the proxy is the tor proxy using this image.

Thank you! (:

Upvotes: 0

Views: 1297

Answers (1)

Ervin Szilagyi
Ervin Szilagyi

Reputation: 16815

I suggest using bridge networking mode, which is the default for docker compose. Just to give you an example:

version: '3.7'
services:
  tor:
    image: osminogin/tor-simple
    restart: always

  curl:
    image: curlimages/curl
    tty: true
    stdin_open: true
    command: ["sh"]
    depends_on:
      - tor

Here you can see that we set up a the tor-simple proxy and a curlimages which obviously will be used to send a request to the Tor network via the proxy. By default docker compose will set up a single network in which each container's host name will be the container name itself, so tor for the proxy and curl for the curlimage.

To prove that we can connect to tor with the proxy, first we bring up the containers with docker compose up. Then we can attach to the curl image with docker attach <image-id>, which will give us a shell (command: [sh] and also curl is running in interactive mode)

Now, we should validated our Tor connection:

curl --socks5 tor:9050 --socks5-hostname tor:9050 -s https://check.torproject.org/ | cat | grep -m 1 Congratulations | xargs

Pleas note that curl will connect to tor-simple using port 9500 (tor:9050) which will proxy the request to https://check.torproject.org/.

This should print something like:

Congratulations. This browser is configured to use Tor.

Upvotes: 2

Related Questions