[IPFS 系列]最近在研究 IPFS 相关的东西, 给大家简单分享一下过程中攒下的一些小经验 - 使用脚本快速安装 Docker 版 IPFS.
我把 swarm 端口改成 4002 是因为 Planet 抢占了 4001 端口。
这脚本大概这么干活:
- 先瞅一眼:看看 Docker 装了没,别忙活半天白干。再检查一下有没有叫
ipfs_host的老容器赖着不走,有的话就报错开溜,坚决不给自己留烂摊子。 - 搭俩小窝:在当前目录下建
ipfs_staging和ipfs_data两个文件夹,给 IPFS 的数据找个地方住。 - 门户大开(但换了号):因为 4001 被占了,咱就让 swarm 走 4002。API 端口是 8080,网关端口是 5001,按你的习惯随便改。
- 拉起来跑:用最新的
ipfs/kubo镜像把容器跑起来,把刚才设的端口和文件夹都挂载好。 - 直接开门迎客:跑起来没问题的话,自动帮你打开浏览器,跳到
http://localhost:5001/webui这个管理页面。接下来传点猫图试试手呗。
怎么用?
简单到不行:
复制
# 1. 给脚本加个执行权限
chmod +x deployment.sh
# 2. 运行它
./deployment.sh
脚本跑完没报错,你的浏览器就会蹦出 IPFS 的 Web 界面了。
脚本在这儿 (deployment.sh):
复制
#!/bin/bash
# 检查 Docker 是否安装
if ! [ -x "$(command -v docker)" ]; then
echo 'Error: Docker is not installed.' >&2
exit 1
fi
echo 'Docker is installed.'
# 检查是否有正在运行的 IPFS 容器
if [ "$(docker ps -q -f name=ipfs_host)" ]; then
echo 'Error: An IPFS container is already running.' >&2
exit 1
fi
echo 'No running IPFS container found.'
# 检查是否有同名的停止状态的 IPFS 容器
if [ "$(docker ps -aq -f status=exited -f name=ipfs_host)" ]; then
echo 'Error: A stopped IPFS container with the same name already exists.' >&2
exit 1
fi
echo 'No stopped IPFS container with the same name found.'
echo 'Proceeding with deployment...'
# 创建存储目录
current_dir=$(pwd)
cd $current_dir
mkdir -p ./ipfs_staging
mkdir -p ./ipfs_data
echo 'Storage directories created.'
# 设置环境变量
export ipfs_staging=./ipfs_staging
export ipfs_data=./ipfs_data
export ipfs_swarm_port=4002
export ipfs_api_port=8080
export ipfs_gateway_port=5001
echo "IPFS staging directory: ${ipfs_staging}"
echo "IPFS data directory: ${ipfs_data}"
echo "IPFS swarm port: ${ipfs_swarm_port}"
echo "IPFS API port: ${ipfs_api_port}"
echo "IPFS gateway port: ${ipfs_gateway_port}"
# 运行 IPFS 容器
docker run -d --name ipfs_host -v ${ipfs_staging}:/export -v ${ipfs_data}:/data/ipfs -p ${ipfs_swarm_port}:4001 -p ${ipfs_api_port}:8080 -p ${ipfs_gateway_port}:5001 ipfs/kubo:latest
if [ $? -ne 0 ]; then
docker rm -f ipfs_host 2>/dev/null
rm -rf ./ipfs_staging
rm -rf ./ipfs_data
echo 'Error: Failed to start the IPFS container.' >&2
exit 1
fi
echo 'IPFS container started successfully.'
open "http://localhost:${ipfs_gateway_port}/webui"
说实话, AI 润色的确实不咋地 😂
代码也算字数的是吧?