Docker环境下Couchbase Sync Gateway无法连接Couchbase Server求助
问题描述
在Docker环境部署Couchbase(CB)和Sync Gateway实例,已成功启动Couchbase Server并完成桶、用户配置,可正常访问管理Web UI。但执行以下Docker命令启动Sync Gateway时报错:
docker run -p 4984:4984 -d couchbase/sync-gateway https://config/sg-config.json
错误日志:
2023-03-02T13:54:22.004Z ==== Couchbase Sync Gateway/3.0.5(8;godeps/) EE ==== 2023-03-02T13:54:22.005Z [INF] Loading content from [https://config/sg-config.json] ... 2023-03-02T13:54:22.879Z [INF] Found unknown fields in startup config. Attempting to read as legacy config. 2023-03-02T13:54:22.879Z [INF] Loading content from [https://config/sg-config.json] ... 2023-03-02T13:54:23.070Z [INF] Config is a legacy config, and disable_persistent_config was not requested. Attempting automatic config upgrade. 2023-03-02T13:54:23.075Z [WRN] gocb: Pipeline Client 0x40002940e0 failed to bootstrap: check server ports and cluster encryption setting: dial tcp 127.0.0.1:11210: connect: connection refused -- base.GoCBCoreLogger.Log() at logger_external.go:80 2023-03-02T13:54:26.075Z [WRN] gocb: CCCPPOLL: Failed to retrieve CCCP config. check server ports and cluster encryption setting: dial tcp 127.0.0.1:11210: connect: connection refused -- base.GoCBCoreLogger.Log() at logger_external.go:80 2023-03-02T13:54:26.075Z [WRN] gocb: CCCPPOLL: Failed to retrieve config from any node. -- base.GoCBCoreLogger.Log() at logger_external.go:80 2023-03-02T13:54:28.076Z [WRN] gocb: Pipeline Client 0x40002940e0 failed to bootstrap: check server ports and cluster encryption setting: dial tcp 127.0.0.1:11210: connect: connection refused -- base.GoCBCoreLogger.Log() at logger_external.go:80 2023-03-02T13:54:31.582Z [WRN] gocb: CCCPPOLL: Failed to retrieve CCCP config. check server ports and cluster encryption setting: dial tcp 127.0.0.1:11210: connect: connection refused -- base.GoCBCoreLogger.Log() at logger_external.go:80 2023-03-02T13:54:31.582Z [WRN] gocb: CCCPPOLL: Failed to retrieve config from any node. -- base.GoCBCoreLogger.Log() at logger_external.go:80 2023-03-02T13:54:33.075Z [ERR] Couldn't start Sync Gateway: unambiguous timeout | {"InnerError":{"InnerError":{"InnerError":{},"Message":"unambiguous timeout"}},"OperationID":"WaitUntilReady","Opaque":"","TimeObserved":10000218462,"RetryReasons":["NOT_READY"],"RetryAttempts":15,"LastDispatchedTo":"","LastDispatchedFrom":"","LastConnectionID":""} -- rest.ServerMain() at main.go:26
使用的sg-config.json配置文件:
{ "log": [ "" ], "databases": { "db": { "server": "http://127.0.0.1:8091", "bucket": "test_bucket", "username": "username", "password": "pwdpwdpwd", "enable_shared_bucket_access": true, "num_index_replicas": 0, "import_docs": true, "users": { "GUEST": { "disabled": true }, "username": { "password": "pwdpwdpwd", "admin_channels": [ "" ] }, }, "sync": "function(doc, oldDoc) {\n if (doc.replRole) {\n requireRole('replicator');\n if (doc.replRole !== 'replicator') {\n requireRole(doc.replRole);\n channel(doc.replRole);\n if (doc.channels && doc.channels.length) {\n doc.channels.each(function(channel) {\n channel(doc.replRole + '_' + channel);\n });\n }\n }\n } else {\n requireRole('sync_daemon');\n channel(doc.channels);\n }\n}" } } }
原因分析
- Docker网络隔离:Sync Gateway容器内的
127.0.0.1指向容器自身,而非Couchbase Server容器或宿主机,导致无法建立连接。 - 配置文件错误:
server字段使用127.0.0.1:8091,在Docker环境下无效;- 配置文件末尾多了一个闭合大括号,属于JSON语法错误;
admin_channels字段为空数组,可能引发权限配置异常。
- 配置加载方式错误:启动命令中使用
https://config/sg-config.json,若未将本地配置文件挂载到容器内,Sync Gateway无法读取有效配置。
解决方案
1. 修正Docker网络与启动命令
方式一:使用自定义Docker网络(推荐)
- 创建专属Docker网络:
docker network create cb-network - 重启Couchbase Server并加入该网络:
docker run -d --name couchbase-server --network cb-network -p 8091-8094:8091-8094 -p 11210:11210 couchbase/server - 启动Sync Gateway时加入同一网络,并挂载本地配置文件:
替换docker run -p 4984:4984 -d --name sync-gateway --network cb-network -v /本地配置文件路径/sg-config.json:/sg-config.json couchbase/sync-gateway /sg-config.json/本地配置文件路径/为实际存放sg-config.json的目录。
方式二:使用宿主机IP连接
- 获取宿主机局域网IP(如
192.168.1.100),修改配置文件中server字段为该IP; - 启动Sync Gateway时挂载本地配置文件:
docker run -p 4984:4984 -d -v /本地配置文件路径/sg-config.json:/sg-config.json couchbase/sync-gateway /sg-config.json
2. 修正配置文件错误
修改sg-config.json:
- 移除末尾多余的闭合大括号;
- 将
server字段替换为Docker网络内的Couchbase容器名(couchbase-server)或宿主机IP; - 调整
admin_channels为有效值(如["*"]表示所有频道); - 修正后的完整配置示例:
{ "log": ["*"], "databases": { "db": { "server": "http://couchbase-server:8091", "bucket": "test_bucket", "username": "username", "password": "pwdpwdpwd", "enable_shared_bucket_access": true, "num_index_replicas": 0, "import_docs": true, "users": { "GUEST": { "disabled": true }, "username": { "password": "pwdpwdpwd", "admin_channels": ["*"] } }, "sync": "function(doc, oldDoc) {\n if (doc.replRole) {\n requireRole('replicator');\n if (doc.replRole !== 'replicator') {\n requireRole(doc.replRole);\n channel(doc.replRole);\n if (doc.channels && doc.channels.length) {\n doc.channels.each(function(channel) {\n channel(doc.replRole + '_' + channel);\n });\n }\n }\n } else {\n requireRole('sync_daemon');\n channel(doc.channels);\n }\n}" } } }
3. 验证连接
- 查看Sync Gateway日志确认启动状态:
docker logs sync-gateway - 访问
http://localhost:4984/db,若返回类似{"committed_update_seq":0,"db_name":"db","instance_start_time":1677750000,"update_seq":0}的JSON响应,说明连接成功。
内容的提问来源于stack exchange,提问作者Nie Selam
相关产品推荐
相关产品推荐

