I am working on preparing docker compose file for some needs and I need to access locally running mongo-db within docker container. In this case, I am using Ubuntu 18.04 as host machine. But other people would also use OS X and Windows but this is not the priority right now.
So, for that, I can use
network_mode: "host"
in docker-compose.yml
but with that, I can not use port-forwarding. (Just fyi, I tried that and it is working well) So I was looking for another solutions and with docker v2.10.0+, other people (ie.) suggested to use "host.docker.internal:host-gateway"
. I tried that and when I am using it, I am getting following error when I would like to access some data from locally, I am getting following error.
pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused, Timeout: 30s, Topology Description: <TopologyDescription id: 614c4d4212a8375b66727410, topology_type: Single, servers: [<ServerDescription ('localhost', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('localhost:27017: [Errno 111] Connection refused')>]>
This was not the case when I use network_mode: "host"
though. So I am looking forward to way to use my locally running mongo-db within docker-container and port-forwarding should be accessible as well.
My current docker-compose file as following
version: '3.9'
services:
engine:
build: .
entrypoint:
- /bin/bash
extra_hosts:
- "host.docker.internal:host-gateway"
That question is different than that as we have special condition with MongoDB and would like to solve problem without blocking port-forwarding so we can not use network_mode = "host"