I have a problem connecting to remote database instances from a docker container.
I have a Docker container with a simple piece of Python code that connects to a remote MongoDB instance
client = MongoClient('mongodb-example_conn_string.com')
db = client.test_db
collection = db.test_collection
print(collection.find_one())
I can run this piece of code from my host machine (a laptop running Linux Mint 20) and it prints the result as expected.
When I build a Docker image (python:3.6.10-alpine) for this script and Docker Run then image I get an error message. The Container is running on the host laptop.
e.g.
docker build . -t py_connection_test
docker run --rm py_connection_test run
I get this error
pymongo.errors.ServerSelectionTimeoutError: mongodb-example_conn_string.com:27017: [Errno -2] Name does not resolve, Timeout: 30s, Topology Description: <TopologyDescription id: 60106f40288b81e007fe75a8, topology_type: Single, servers: [<ServerDescription ('mongodb-example_conn_string.com', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('mongodb-example_conn_string.com:27017: [Errno -2] Name does not resolve',)>]>
The MongoDB remote instance is an internal database at work and a VPN (Using OpenVPN) is required to access it. I've used traceroute on host machine and docker container to confirm that network traffic is routed through the VPN, all seems to be fine there.
I've tried Docker Run with flag
--network="host"
But the same thing happens
I'm scratching my head at this, why does the same connection url not working in both cases? Is there something simple I've missed?
question from:
https://stackoverflow.com/questions/65908673/cannot-connect-to-remote-database-instance-from-my-docker-container-however-can 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…