2

There were two related questions but they didn't quite answer the question. But if mods think this is a duplicate, please let me know.

I have a docker-compose.yml file that deploys phpmyadmin. There is a mysql server hosted locally on the host proper (NOT as a container).

I have a config file for phpmyadmin to connect to my database. I can't seem to find a domain name for docker host so I've been taking the subnet that the containers deploy on and using the .1 of the subnet. For example, initially containers deployed to 172.16.0.0/24 and so I declared in phpmyadmin's configuration to connect to 172.16.0.1

This question was born out of the fact that every time I re-deployed, i.e. issued docker-compose down && docker-compose up -d the network address kept changing. My work around is to explicitly declare the default ipam network subnet, which is a fine workaround and actually preferred because I can then pin mysql user logins to the ip address range.

But given that docker knows to resolve the container service "phpmyadmin" to the container's IP address, I figured there MUST be something for the host so I wouldn't have to re-declare the IP address each time.

So, is there a "hostname" that a container can use to talk to the host or am I stuck using IP addresses?

Edit: I'm using docker on Linux and would very much prefer to not run the container in host mode.

scuba_mike
  • 360
  • 3
  • 12
  • 1
    On native Linux, there isn't a magic hostname like that. The linked question has a couple of workarounds. – David Maze Dec 13 '19 at 12:01

1 Answers1

3

The simplest solution is to set network_mode: "host" in your compose file for the container. See https://docs.docker.com/compose/compose-file/#network_mode.

Alternatively you can use host.docker.internal as the url name from within the container, if using Docker for Windows or Docker for Mac.

ParkerD
  • 1,214
  • 11
  • 18
  • Thanks but I'm using docker on Linux. Though if really like to see this added to Linux in the future. Also, I would prefer not run in host mode if possible. – scuba_mike Dec 13 '19 at 02:15
  • 1
    @scuba_mike Ah, well in that case, you should keep an eye on this issue: https://github.com/docker/for-linux/issues/264. Until that is done, you'll have to use the IP address. Using a script to pass it in as an environment variable somehow might make it less painful. – ParkerD Dec 13 '19 at 02:22
  • Wow that issue is over a year old! I figured docker was using some sort of form of dnsmasq to do the name resolution. Any idea what the hold up on the feature is? – scuba_mike Dec 13 '19 at 12:28