How to connect different services between two different docker compose?
The TLDR answer:
Since you are in two different docker-compose services, you can't use localhost neither you can use the service name. One way is to try using the host machine IP but I have faced issues multiple times. The safest option which has always worked for me is to use
host.docker.internal followed by the port in which you are exposing the service. The port number should be exposed to the host machine for this to work.
The Detailed Answer:
We all can agree with the fact that Docker has solved a lot of environment and installation issues. We all also agree to the fact that once in a while it notoriously gives us sleepless nights when we are not able to find out the exact reason why things are not working as expected or we want to do something which is not so common. One such use case, you might have come across is when you have 2 different docker-compose projects and then you want to connect 2 different containers present in different projects.
If you are using Docker for Linux then you can use
network="host", quoting from the documentation
If you use the
hostnetwork mode for a container, that container’s network stack is not isolated from the Docker host (the container shares the host’s networking namespace), and the container does not get its own IP-address allocated.
Unfortunately, the host networking driver only works on Linux hosts and is not supported on Docker Desktop for Mac, Docker Desktop for Windows, or Docker EE for Windows Server.
Docker Desktop for Mac is something a lot of developers might use so the solution which I have found to be working is
host.docker.internal:<PORT>. The official documentation states the following
The host has a changing IP address (or none if you have no network access). We recommend that you connect to the special DNS name
host.docker.internalwhich resolves to the internal IP address used by the host. This is for development purpose and will not work in a production environment outside of Docker Desktop for Mac. You can also reach the gateway using
As of docker 20.4
host.docker.internal is supported across all platforms Mac, Linux, and windows.