Docker部署APISIX无法连接指定etcd节点问题求助
问题描述
我在Docker环境部署APISIX时遇到问题,已创建自定义Docker网络gomath-net(子网10.10.10.0/24),并在其中启动etcd(IP 10.10.10.4)和APISIX(IP 10.10.10.5)容器。
ETCD启动命令
docker run -d ` --name etcd ` --network gomath-net ` --ip 10.10.10.4 ` -p 2379:2379 ` -p 2380:2380 ` -e ALLOW_NONE_AUTHENTICATION=yes ` -e ETCD_NAME=etcd1 ` -e ETCD_ADVERTISE_CLIENT_URLS=http://10.10.10.4:2379 ` -e ETCD_LISTEN_CLIENT_URLS=http://0.0.0.0:2379 ` -e ETCD_LISTEN_PEER_URLS=http://0.0.0.0:2380 ` -e ETCD_INITIAL_ADVERTISE_PEER_URLS=http://10.10.10.4:2380 ` -e ETCD_INITIAL_CLUSTER="etcd1=http://10.10.10.4:2380" ` -e ETCD_INITIAL_CLUSTER_TOKEN=etcd-cluster ` -e ETCD_INITIAL_CLUSTER_STATE=new ` -v etcd-data:/bitnami/etcd ` bitnami/etcd:3.5
APISIX启动命令
docker run -d ` --name apisix ` --network gomath-net ` --ip 10.10.10.5 ` -p 9080:9080 ` -p 9091:9091 ` -p 9443:9443 ` -v ${PWD}\config.yaml:/usr/local/apisix/conf/config.yaml ` apache/apisix:3.8.0-debian
调整后的APISIX config.yaml配置
apisix: node_listen: 9080 enable_admin: true plugins: - proxy-rewrite - limit-req - prometheus deployment: admin: admin_key: - name: admin key: edd1c9f034335f136f87ad84b625c8f1 role: admin allow_admin: - 0.0.0.0/0 etcd: host: - "http://10.10.10.4:2379" prefix: /apisix timeout: 30
错误日志
/usr/local/openresty//luajit/bin/luajit ./apisix/cli/apisix.lua init /usr/local/openresty//luajit/bin/luajit ./apisix/cli/apisix.lua init_etcd Warning! Request etcd endpoint 'http://127.0.0.1:2379/version' error, connection refused, retry time=1 Warning! Request etcd endpoint 'http://127.0.0.1:2379/version' error, connection refused, retry time=2 request etcd endpoint 'http://127.0.0.1:2379/version' error, connection refused all etcd nodes are unavailable
APISIX容器无法启动,始终尝试访问http://127.0.0.1:2379,而非配置文件中指定的etcd地址,请问该如何解决?
原因分析与解决方案
核心原因
APISIX未读取到你挂载的自定义config.yaml,主要是以下两种情况:
- 挂载路径格式错误:Windows风格的
\分隔符不适用于Linux容器,导致文件映射失败; - 容器内仍使用默认配置文件:默认配置中etcd地址就是
http://127.0.0.1:2379。
解决步骤
1. 修正挂载路径格式
将APISIX启动命令中的挂载路径改为Linux风格的/分隔符:
docker run -d ` --name apisix ` --network gomath-net ` --ip 10.10.10.5 ` -p 9080:9080 ` -p 9091:9091 ` -p 9443:9443 ` -v ${PWD}/config.yaml:/usr/local/apisix/conf/config.yaml ` apache/apisix:3.8.0-debian
如果是Windows PowerShell环境,可将${PWD}替换为$(Get-Location).Path,或直接使用绝对路径(如C:/your-project-path/config.yaml),注意替换所有\为/。
2. 验证挂载是否成功
先删除原有APISIX容器:
docker stop apisix && docker rm apisix
重新启动容器后,进入容器检查配置文件内容:
docker exec -it apisix cat /usr/local/apisix/conf/config.yaml
若输出与本地修改的配置一致,说明挂载成功;若仍为默认配置,需重新确认本地config.yaml的路径是否正确。
3. 检查文件权限(Linux/macOS环境)
确保本地config.yaml的权限允许容器读取:
chmod 644 config.yaml
也可在挂载时添加ro标记设置只读挂载,提升安全性:
-v ${PWD}/config.yaml:/usr/local/apisix/conf/config.yaml:ro
4. 备选方案:用环境变量指定etcd地址
若挂载配置文件仍有问题,可直接通过环境变量传递etcd地址,跳过配置文件修改:
docker run -d ` --name apisix ` --network gomath-net ` --ip 10.10.10.5 ` -p 9080:9080 ` -p 9091:9091 ` -p 9443:9443 ` -e APISIX_DEPLOYMENT_ETCD_HOST="http://10.10.10.4:2379" ` apache/apisix:3.8.0-debian
APISIX支持用环境变量覆盖配置,格式为APISIX_<配置层级>,例如deployment.etcd.host对应APISIX_DEPLOYMENT_ETCD_HOST。
5. 验证etcd连通性
在APISIX容器内测试etcd是否可达:
docker exec -it apisix curl http://10.10.10.4:2379/version
若返回etcd版本信息,说明网络正常;若不通,检查etcd容器是否正常运行,或自定义网络gomath-net的配置是否正确。
内容的提问来源于stack exchange,提问作者ussrback

