Home > Article > Backend Development > How to Connect a Dockerized Go Application to a Local MongoDB Database?
Connecting Local MongoDB Database to Docker Go Application
When attempting to connect a Dockerized Go application to a local MongoDB database, you may encounter a "no reachable servers" error. This issue stems from the isolated network environment created by Docker, where the container has its own IP address.
To resolve this, you need to establish communication between the container and the host machine. Docker provides a special URL for this purpose: host.docker.internal.
Assuming your MongoDB instance is running on the host machine and bound to all interfaces, you can connect to it from within the Docker container using the following connection string:
mongodb://host.docker.internal:21017/database
Replace "database" with the name of your MongoDB database.
By using host.docker.internal as the hostname, you establish a connection between the container and the database running on the host machine. Remember, the container does not have direct access to the host's local IP address, so you must use host.docker.internal as a proxy.
The above is the detailed content of How to Connect a Dockerized Go Application to a Local MongoDB Database?. For more information, please follow other related articles on the PHP Chinese website!