Issue
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?
Solution
I've figured out the issue, thanks to Max for pointing me to look into DNS.
My problem was a faulty /etc/resolv.conf file on my host machine that the Docker Container was picking up. It contained 2 nameserver entries
In my case I could create the file /etc/docker/daemon.json on my host and add my dns entry there for the Container to pickup when run. e.g. adding lines:
{
"dns": ["172.31.0.2"]
}
Editing / creating this file requires a Docker service restart
I got some helpful hints from https://l-lin.github.io/post/2018/2018-09-03-docker_ubuntu_18_dns/
Answered By - ryansusername
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.