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