Map service in docker compose to host.docker.internal

I am preparing the docker-compose yml file based on a template. Which means sometimes all the services will run inside the composition and sometimes I’ll want to exclude some of the services and run them locally.
So, I want to do something like this:

extra_hosts:
    - "<some_service_name>:host.docker.internal"

Unfortunately it complains that this is invalid.
I would really appreciate if someone could help.

  • I’m using Docker desktop on macOS

And I guess it does say that “host.docker.internal” is an invalid IP address. If you read the documentation, you can see that the first part of an extra host is a hostname, the second is an IP address

You can’t use servicename as the first part and you can’t use a hostname as the second part. You could use “host-gateway” instead of an IP address (which is not really well-documented but it is somewhere in one of the docs. I can’t find it now).

I am not sure I understand. Do you want to use the host network? Can you show an example what you are trying to do?

update: I realized you probably want to run something on the host so you want to access it from the container. In that case “host.docker.internal” should work on Mac without additional configuration, but if not, you can set the extra host this way:

extra_hosts:
    - "host.docker.internal:host-gateway"

If your services are listening on “localhost” and not on the Docker network (172.x.x.x), you need to run your container on the host network.

network_mode: host

You absolutely can use a service name as the first part.
I’m aware of host-gateway, but it won’t help in my case.

I’ve been using docker-machine up until now (which was deprecated a long time ago).
and to achieve this I did:

extra_hosts:
    - "<some_service_name>:192.168.99.1"

To make this work when using docker desktop (since there is no docker0 bridge on macOS) I have to use host.docker.internal because there is no static address…

It is syntactically correct, but that is just hostname and it helps only if your targer service runs on the host network. I didn’t understand what your goal was when I wrote that part of my comment. But you are right, you can use that to access the service using a different IP address.

It doesn’t change the fact that you can’t use “host.docker.internal” instead of an IP address. You could use a script to detect the current ip address on the host or set it as an environment variable in a .env file and use that variable in the docker-compose.yml

extra_hosts:
    - "<some_service_name>:$HOSTIP"

I have a similar script for Linux, but I can’t write a compatible version for Mac now.

Yep, that would definitely work, but I’m looking for an alternative because I need to use a different network interface than my machine’s so I could use the same ports.

Hey m8, could you show me your Dockerfile to run this script ?

I don’t have a Dockerfile. I used that script only to set the IP in my shell environment and use the variable in a compose file like this:

And this is how I set the variable:

export XIP=$(xip.sh mysubdomain)
# result: XIP=mysubdomain.<my ip address>.xip.hu