what is the replacement for host.docker.internal in production? I read the docker docs, host.docker.internal is not recommended to use it for production.
Let’s refer to the relevant part from the documentation:
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.internal
which resolves to the internal IP address used by the host. This is for development purpose and does not work in a production environment outside of Docker Desktop.
So as the first sentence says:
The host has a changing IP address, or none if you have no network access.
When you are using your laptop the IP address can change so you can’t always use the same P address. This is why it is supported in Docker Desktop, but you don’t need it on server using Docker CE, since servers has static IP addresses so you can use the IP address of the server and make sure the process on the host is listening on an IP that the container can reach. So not on localhost. You could also use the Docker gateway addresses if you don’t want the process to listen on the public IP.
If you can tell us more about your usecase, we may be able to give you a better answer.
in my usecase i want to access database which resides in another container. I use localhost but there is an error localhost not found. Even though in the container where the database is located, I have exposed the port with -p 5432:5432
.
Localhost will not work from a container. I have a tutorial in which I try to explain it:
https://dev.to/rimelek/docker-network-and-network-namespaces-in-practice-5h9#network-traffic-between-a-container-and-the-outside-world
You will need to scrolldown until “localhost”.
One container can access another container using the container’s name or also service name in case of Docker Compose. You need to use user-defined network to have DNS resolution for containers.
docker network create mynet
docker run --network mynet ...
Note that the docker run command is not finished I just wanted to show the --network
option.
You can also share the network namespace to be able to use localhost meaning that “localhost” is the same for both containers but not for the host.