HiveBrain v1.2.0
Get Started
← Back to all entries
snippetpythondockerMinor

How to connect to external etcd server inside of docker container (python application)?

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
etcdconnectcontainerapplicationdockerpythonhowserverexternalinside

Problem

I have a Cement application that runs inside of a docker container. In this image I have to connect to etcd. Inside of my docker image I have config that for etcd is set to localhost that I know is not correct!

On my local machine etcd is up and running:

tcp4       0      0  localhost.2379         localhost.51949        ESTABLISHED


The question is how should I connect to etcd server which is outside of docker container? etcd is installed normally without using docker.

Output of my python application when I run it with docker run -p 8084:8084 my_python_image:

Traceback (most recent call last):
  File "my_app.py", line 64, in 
    app.run()
  File "/usr/local/lib/python2.7/site-packages/cement/core/foundation.py", line 882, in run
    return_val = self.controller._dispatch()
  File "/usr/local/lib/python2.7/site-packages/cement/core/controller.py", line 477, in _dispatch
    return func()
  File "my_app.py", line 45, in default
    module_rpc = rpc.Rpc('my_module', params=app.config.get_section_dict('etcd'))
  File "/usr/local/lib/python2.7/site-packages/rpc_server/rpc.py", line 31, in __init__
    self.etcd_client.write(self.etcd_nodes, None, dir=True)
  File "/usr/local/lib/python2.7/site-packages/etcd/client.py", line 500, in write
    response = self.api_execute(path, method, params=params)
  File "/usr/local/lib/python2.7/site-packages/etcd/client.py", line 893, in wrapper
    cause=e
etcd.EtcdConnectionFailed: Connection to etcd failed due to MaxRetryError(u"HTTPConnectionPool(host=u'localhost', port=2379): Max retries exceeded with url: /v2/keys/my_module/nodes (Caused by NewConnectionError(': Failed to establish a new connection: [Errno 111] Connection refused',))",)

Solution

The error you see in your container is:


etcd.EtcdConnectionFailed: Connection to etcd failed due to MaxRetryError(u"HTTPConnectionPool(host=u'localhost', port=2379): Max retries exceeded with url: /v2/keys/my_module/nodes (Caused by NewConnectionError(': Failed to establish a new connection: [Errno 111] Connection refused',))",)

If you take a close look there, you can see that it's trying to connect to localhost. Inside your container, localhost is means "the current container" (much like localhost on your host means "the current host"). If you want to connect to an etcd instance outside of the container, you'll need to use the ip address (or hostname) of another server.

If you're running (a) running Docker locally, (b) etcd on your host is listening on all addresses, and (c) there are no firewall rules that would otherwise prohibit the connection, then you can use the address of the Docker bridge associated with your container as the address of your host. This will be the default gateway as seen from inside the container. You can extract the address with something like:

ip route show | awk '$1 == "default" {print $3}'

Code Snippets

ip route show | awk '$1 == "default" {print $3}'

Context

StackExchange DevOps Q#2027, answer score: 3

Revisions (0)

No revisions yet.