Let's say I have a docker container on a docker network with ip

Inside that docker container there are two processes running.

  1. An http server listening on port 8000 that always replies 'hello'
  2. A http client that constantly tries to connect to http://example.com

Is it possible, using ip tables on the host machine, to forward all outbound traffic in that container back into the same container.

So in the scenario above, the http request to example.com, would actually goto the 8000 port inside that same container, returning 'hello'.

I feel like the following would work if this wasn't in docker. Am I close?

iptables -t nat -A OUTPUT -s -j REDIRECT --to-ports 8000

But I think this is just forwarding to 8000 on the host machine :(

1 Answer 1


I would suggest editing the /etc/hosts file on the docker container:  example.com

This means that everytime the client attempts to connect to http://example.com:8000 via HTTP, traffic will be pointed to docker container at, and receive a "hello" reply as expected.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .