安装Go语言环境 开发环境准备 ,并安装IPFS私钥生成工具
go install github.com/Kubuxu/go-ipfs-swarm-key-gen/ipfs-swarm-key-gen@latest
ipfs_01: container_name: ipfs-01 image: ipfs/go-ipfs:latest restart: always volumes: - ./01/staging:/export # (可不修改)修改 挂载目录 ./staging - ./01/data:/data/ipfs # (可不修改)修改 挂载目录 ./data ports: - 4001:4001 - 0.0.0.0:8080:8080 - 0.0.0.0:5001:5001
开启对应的跨域访问
docker exec ipfs01 ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin '["*"]' docker exec ipfs01 ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods '["PUT", "GET", "POST"]'
生成KEY
如果你部署的是多个节点,请保持节点的
swarm.key
内容相同,拥有相同KEY的节点才可以相互通信./ipfs-swarm-key-gen > swarm.key
需要将KEY文件
swarm.key
放到挂载的data
目录下删除自带的节点
docker exec ipfs01 ipfs bootstrap rm --all
重新启动容器
docker retsart ipfs01
下面的内容经过验证可以自动完成,不需要手动进行,当然,是上面配置正确的情况下
docker exec ipfs01 ipfs id
{ "ID": "12D3KooWN6H1sNJJSkBNaQGDjtjdu6eEgCbGFbroTjxoy1LcLjcP", "PublicKey": "CAESILZftaM3L1tyMAqsdTOSa/OVkWD7/ef/jmVM067hTcz0", "Addresses": [ "/ip4/127.0.0.1/tcp/4001/p2p/12D3KooWN6H1sNJJSkBNaQGDjtjdu6eEgCbGFbroTjxoy1LcLjcP", "/ip4/172.22.0.3/tcp/4001/p2p/12D3KooWN6H1sNJJSkBNaQGDjtjdu6eEgCbGFbroTjxoy1LcLjcP" ], "AgentVersion": "kubo/0.16.0/38117db/docker", "ProtocolVersion": "ipfs/0.1.0", "Protocols": [ "/ipfs/bitswap", "/ipfs/bitswap/1.0.0", "/ipfs/bitswap/1.1.0", "/ipfs/bitswap/1.2.0", "/ipfs/id/1.0.0", "/ipfs/id/push/1.0.0", "/ipfs/lan/kad/1.0.0", "/ipfs/ping/1.0.0", "/libp2p/autonat/1.0.0", "/libp2p/circuit/relay/0.1.0", "/libp2p/circuit/relay/0.2.0/stop", "/p2p/id/delta/1.0.0", "/x/" ] }
Addresses
127.0.0.1
docker exec ipfs_host ipfs bootstrap add <复制的内容>
docker exec ipfs01 ipfs swarm peers
参考链接
附件
自己测试时创建的多节点
docker-compose.yml
文件需要使用
docker network create ipfs
创建网络添加完毕
swarm key
后能够自动获取邻居贴心的开启
API
跨域的代码,下面的代码能够开启访问权限docker exec ipfs01 ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin '["*"]' docker exec ipfs01 ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods '["PUT", "GET", "POST"]' docker exec ipfs02 ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin '["*"]' docker exec ipfs02 ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods '["PUT", "GET", "POST"]' docker exec ipfs03 ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin '["*"]' docker exec ipfs03 ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods '["PUT", "GET", "POST"]' docker exec ipfs04 ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin '["*"]' docker exec ipfs04 ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods '["PUT", "GET", "POST"]'
docker exec ipfs01 ipfs bootstrap rm --all