3

I have a web application running inside a php:7.1.8-apache docker container. The application has port 80 inside the container and port 8080 outside of it.

One part of the application sends requests to itself, but uses the outside hostname/port (for example to http://outsidehostname.local:8080).

This doesn't work because the port and the hostname does not exist inside the container.

I already tried the --hostname flag, but this doesn't solve the problem with the different port inside and outside of my container. So I am looking for a different solution.

The hostname (outsidehostname.local) comes from the host os (in my case macos). I am using dnsmasq to resolve all *.local hostnames to 127.0.0.1.

Is there any way to configure docker so that this request works without changing the behavior of the application?

Constantin
  • 388
  • 3
  • 15

1 Answers1

0

In docker you have various options to set hostnames that can be resolved from container to container: When to use --hostname in docker?

This doesn't work because the port and the hostname does not exist inside the container.

Why not? Where does this outside hostname come from?

Hostnames that cannot be resolved by docker could be resolved by other DNS servers configured on OS or network level. In general how a hostname will be resolved is not a trivial question and you first need to understand how / where your outside hostname is defined and resolved.

UPDATE:

The hostname (outsidehostname.local) comes from the host os (in my case macos). I am using dnsmasq to resolve all *.local hostnames to 127.0.0.1.

This explains your problem: log in to your running container (assuming it's Linux based) using docker exec -it <containerId> /bin/sh then inside the container if you try to look up outsidehostname.local you should see that outsidehostname.local cannot be resolved because there is no such DNS info inside the container OS. If it could be resolved to 127.0.0.1, your next problem would indeed be the wrong port.

Basically running the webserver inside the container defeats the purpose of running your own OSX DNS resolver outside the container. I don't know enough about your use case to really suggest a good solution, but for Linux based images you can always edit /etc/hosts or /etc/resolv.conf.

B M
  • 3,893
  • 3
  • 33
  • 47
  • I already tried the `--hostname` flag, but this doesn't solve the problem with the different port inside and outside of my container. So I am looking for a different solution. The hostname comes from the host os (in my case macos). I am using `dnsmasq` to resolve all `*.local` hostnames to `127.0.0.1`. – Constantin Dec 04 '18 at 22:40
  • Could you add this info to your question so that I can add an answer to my response instead of here in the comments? But in short: the port is not the issue, `127.0.0.1` is. You need to resolve to an IP both network interfaces share. – B M Dec 05 '18 at 07:08
  • 1
    I added my comment to the original question. I kind of understand your short answer, but I have no idea how to achieve that. – Constantin Dec 05 '18 at 23:37