Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
380 views
in Technique[技术] by (71.8m points)

python - Cannot connect to remote Database instance from my docker container, however can connect from my host computer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...