Python container cannot see other ipfs container inside a docker network

I have a python application running in docker which uses the ifps-http-api and another container which is the ipfs-node found here.
I start the ipfs container like so :

docker run -d --name ipfs-node
-v /tmp/ipfs-docker-staging:/export -v /tmp/ipfs-docker-data:/data/ipfs
–net=my-network
jbenet/go-ipfs:latest

and the python container like :

docker run -d --name my-own-container --net=my-network -p 8070:8070 my-own-container

Port 8070 is used for dash as a web UI. When I start my-own-container, I get the error :

ipfshttpclient.exceptions.ConnectionError: ConnectionError: HTTPConnectionPool(host=‘0.0.0.0’, port=5001): Max retries exceeded with url: /api/v0/version?stream-channels=true (Caused by NewConnectionError(‘<ipfshttpclient.requests_wrapper.HTTPConnection object at 0x7ff644802eb8>: Failed to establish a new connection: [Errno 111] Connection refused’))

which resemble the error I get when starting the python script on its own without the ipfs daemon running.
The specific line where the error is caused is this one:

client = ipfshttpclient.connect(‘/ip4/127.0.0.1/tcp/5001/http’)

I have tried to replace 127.0.0.1 with 0.0.0.0 and the container name without success.
Thus, I think this is caused by the fact that the python container cannot see the daemon from ipfs-node. I verified it was up and running by doing :

docker exec -it ipfs-node sh

Any idea what I could do wrong?

Two ideas:

  • You should use ipfs/go-ipfs for your docker image. jbenet/go-ipfs is probably years out of date. I have updated the blog post.
  • Doublecheck that things are listening on the right ports. Connection refused says that nothing is listening on 5001. See if you can find out why (might be related to using a very old go-ipfs).

Remember to check your ipfs configs and the logs for errors.

I switched to ipfs/go-ipfs with the same result. Instead of starting 2 containers, I figure I should make sure that my container can talk correctly to the local ipfs daemon as it is unlikely that my issue comes from the go-ipfs container.
Here is what I am doing.
1 - Start the ipfs daemon locally (on mac os)

ipfs daemon
I can see it is up and running and can use the ipfs network as I should

2 - Start the container

docker run -it --name my-own-container -p 5001:5001 -p 8070:8070 my-own-container

I then get the following error :

Traceback (most recent call last):
File “./UI.py”, line 13, in
import encryption
File “/app/encryption.py”, line 11, in
import ipfs
File “/app/ipfs.py”, line 3, in
client = ipfshttpclient.connect(‘/ip4/0.0.0.0/tcp/5001/http’)
File “/usr/local/lib/python3.7/site-packages/ipfshttpclient/client/init.py”, line 101, in connect
assert_version(client.version()Ä’Version’Å)
File “/usr/local/lib/python3.7/site-packages/ipfshttpclient/client/base.py”, line 15, in wrapper
result = func(*args, **kwargs)
File “/usr/local/lib/python3.7/site-packages/ipfshttpclient/client/miscellaneous.py”, line 204, in version
return self._client.request(‘/version’, decoder=‘json’, **kwargs)
File “/usr/local/lib/python3.7/site-packages/ipfshttpclient/http.py”, line 50, in wrapper
return func(self, *args, **merged)
File “/usr/local/lib/python3.7/site-packages/ipfshttpclient/http.py”, line 370, in request
files, headers, data, timeout=timeout)
File “/usr/local/lib/python3.7/site-packages/ipfshttpclient/http.py”, line 288, in _request
timeout=timeout)
File “/usr/local/lib/python3.7/site-packages/ipfshttpclient/http.py”, line 255, in _do_request
six.raise_from(exceptions.ConnectionError(error), error)
File “”, line 3, in raise_from
ipfshttpclient.exceptions.ConnectionError: ConnectionError: HTTPConnectionPool(host=‘0.0.0.0’, port=5001): Max retries exceeded with url: /api/v0/version?stream-channels=true (Caused by NewConnectionError(‘<ipfshttpclient.requests_wrapper.HTTPConnection object at 0x7fc079715ef0>: Failed to establish a new connection: ÄErrno 111Å Connection refused’))

which points to the part of the file

import ipfshttpclient
#client = ipfshttpclient.connect(‘/ip4/127.0.0.1/tcp/5001/http’)
client = ipfshttpclient.connect(‘/ip4/0.0.0.0/tcp/5001/http’)

def addFileIPFS(filename):
res = client.add(filename)
print("Address is: ", res[‘Hash’])#client.cat(res[‘Hash’]))
return res[‘Hash’]

I don’t think docker can bind on 5001 as this will be already taken by the ipfs daemon that you are running locally.

Your problems is that your container cannot talk to the port you try to connect. You can only connect to /ip4/127.0.0.1/tcp/5001/http’ if ipfs is running locally. You need to run the python container with --net=host or, if you run two containers, you need to put the right IP/dns name in that multiaddress because it should talk to the other container which is not running on 127.0.0.1. /dns4/the-name-of-your-other-container/tcp/5001 might work

Thanks to you!
Using the dns4/container-name/ worked perfectly.
Before, I was using
/ip4/container-name/tcp…
which does not work. I was not aware of the > /dns4/
Both are using the same network to communicate, and the web-UI is specified to 0.0.0.0 which then can be viewed from the host’s browser.
Thanks a lot!