Access IPFS running on host machine from docker container

Does anyone have experience how to access IPFS from a docker container using python api (ipfsapi) ? It seems that I cannot connect to localhost, where IPFS is running from the docker container.
This is the command to connect:
api = ipfsapi.connect(ā€˜127.0.0.1ā€™, 5001)

EDIT: I figured out that from mac I should use
api = ipfsapi.connect(ā€˜docker.for.mac.host.internalā€™,5001)
and it works fine.
What exactly is ā€œdocker.for.mac.host.internalā€ ?
How can I connect from linux machines?

2 Likes

quick question : could you share your docker ps result please ?

Here it is:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a9b384196a2c ubuntu ā€œ/bin/bashā€ 9 hours ago Up 8 hours testipfs

and I run the container like this:

docker run -it --name testipfs ubuntu

I also tried with docker run -it -p 5001:5001 --name testubuntu ubuntu

1 Like

maybe you can try with image https://hub.docker.com/r/ipfs/go-ipfs/ there are Docker file https://github.com/ipfs/go-ipfs/blob/master/Dockerfile please publish 5001 4001 4001/udp 8080 8081

reagards

1 Like

I would like to my own imageā€¦

donā€™t you just need to connect from ā€˜0.0.0.0ā€™, 5001 (instead of 127.0.0.1) when running within a container. I think that should work on both Mac and Linux

Hereā€™s my Docker file https://github.com/jeffreycwitt/dll-review-registry/blob/master/Dockerfile and and ipfs config file:
https://github.com/jeffreycwitt/dll-review-registry/blob/master/ipfs-config.json

I could be wrong, but isnā€™t it best not to expose 5001. Usually 5001 is only needed internally within the Docker container to run CLI commands.

My understanding is that you would only want to expose it if you wanted to be able to remotely control the CLI

See Cannot transfer between two firewalled machines

So 5001 is for controlling the ipfs daemon you mean?

@jeffreycwitt I will try

ā€˜0.0.0.0ā€™, 5001

thatā€™s my understanding.

But since 5001 is only needed inside the container 127.0.0.1 should work

But if you want to access 4001 and 8080 outside the container you should set the config to 0.0.0.0