+-

我正在尝试使用docker-compose通过Docker连接golang和reds,但我没有太多运气.我已经在 https://github.com/davidwilde/docker-compose-golang-redis/tree/stackoverflow_question发布了我的尝试并列出了下面的日志.
Redis表示已经准备好接受连接,但是我的golang应用程序使用gopkg.in/redis.v3说没有.
~/workspace/composetest master ● docker-compose up
Starting composetest_db_1...
Starting composetest_web_1...
.
.
.
ur kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
db_1 | 1:M 20 Nov 05:58:33.371 * DB loaded from disk: 0.000 seconds
db_1 | 1:M 20 Nov 05:58:33.371 * The server is now ready to accept connections on port 6379
web_1 | panic: dial tcp [::1]:6379: getsockopt: connection refused
web_1 |
web_1 | goroutine 1 [running]:
web_1 | main.main()
web_1 | /go/src/app/app.go:19 +0x131
web_1 |
web_1 | goroutine 17 [syscall, locked to thread]:
web_1 | runtime.goexit()
web_1 | /usr/local/go/src/runtime/asm_amd64.s:1696 +0x1
web_1 | panic: dial tcp [::1]:6379: getsockopt: connection refused
web_1 |
web_1 | goroutine 1 [running]:
web_1 | main.main()
web_1 | /go/src/app/app.go:19 +0x131
web_1 |
web_1 | goroutine 17 [syscall, locked to thread]:
web_1 | runtime.goexit()
web_1 | /usr/local/go/src/runtime/asm_amd64.s:1696 +0x1
最佳答案
所以我们有两个不同的容器,这意味着两个不同的“localhost”.
client := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "",
DB: 0,
})
因此,您的应用程序正在向其自己的沙盒容器发出请求,而不是向包含redis的“其他”沙盒容器发出请求.
你有两个选择;
在compose文件中提供映射,如redisdb:db,并传递该信息而不是localhost.
或者,使用“–net = host”选项,以便为容器提供通用网络,而无需更改代码.
编辑:错字
点击查看更多相关文章
转载注明原文:通过Docker连接golang和redis - 乐贴网