Skip to content

Instantly share code, notes, and snippets.

@mendhak
Last active August 27, 2024 21:04
Show Gist options
  • Save mendhak/4c0fd6f92090894d6a07c4c4cebe53c7 to your computer and use it in GitHub Desktop.
Save mendhak/4c0fd6f92090894d6a07c4c4cebe53c7 to your computer and use it in GitHub Desktop.
In docker, transparently route a domain to another domain through TCP proxying

Basically I want a container to call 'example.com' and when it does, it should go to Nginx which in turn forward the request to some-other-domain.com

This worked with curl and other basic applications.

This doesn't seem to work with Playwright though, for some reason Chrome just ignores the 'DNS' being set up here. For Playwright, I used this method with Squid.

Bring up the nginx

docker-compose up nginx

Then bring up the 'client', isntall curl, openssl, and make some requests. Notice that the IP resolves to the nginx container.

docker compose run --rm -it client /bin/bash
root@527e66b985d3:/# apt update && apt install -y curl openssl 
root@527e66b985d3:/# curl -kv https://example.com
*   Trying 10.1.0.100:443...
* Connected to example.com (10.1.0.100) port 443 (#0)
server {
listen 443;
#TCP traffic will be forwarded to the specified server
proxy_pass $TARGET_SERVER:$TARGET_PORT;
}
services:
client:
image: ubuntu:22.04
extra_hosts:
- "example.com:10.1.0.100"
networks:
development:
ipv4_address: 10.1.0.55
nginx:
image: nginx
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
- ./default.conf.template:/etc/nginx/templates/default.conf.template
environment:
- TARGET_SERVER=some-other-domain.com
- TARGET_PORT=443
networks:
development:
ipv4_address: 10.1.0.100
networks:
development:
ipam:
driver: default
config:
- subnet: 10.1.0.0/24
events {}
stream {
include /etc/nginx/conf.d/*.conf;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment