Connecting golang and redis through docker

huangapple go评论95阅读模式
英文:

Connecting golang and redis through docker

问题

我正在尝试使用docker-compose将golang和Redis连接起来,但运气不太好。我已经在https://github.com/davidwilde/docker-compose-golang-redis/tree/stackoverflow_question上发布了我的尝试,并在下面列出了日志。

Redis表示已准备好接受连接,但我的使用gopkg.in/redis.v3的golang应用程序却无法连接。

~/workspace/composetest   master ●  docker-compose up
Starting composetest_db_1...
Starting composetest_web_1...
.
.
.
您的内核配置可能会导致与Redis的延迟和内存使用问题。要解决此问题,请以root身份运行命令'echo never > /sys/kernel/mm/transparent_hugepage/enabled',并将其添加到/etc/rc.local中,以便在重新启动后保留此设置。在禁用THP后,必须重新启动Redis。
db_1 | 1:M 20 Nov 05:58:33.371 * 从磁盘加载数据库: 0.000 秒
db_1 | 1:M 20 Nov 05:58:33.371 * 服务器已准备好在端口6379上接受连接
web_1 | panic: dial tcp [::1]:6379: getsockopt: 连接被拒绝
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: 连接被拒绝
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

英文:

I'm trying to connect golang and reds through Docker using docker-compose but I'm not having much luck. I have published my attempt at https://github.com/davidwilde/docker-compose-golang-redis/tree/stackoverflow_question and listed the logs below.

Redis says it is ready to accept connections but my golang app using gopkg.in/redis.v3 says no.

 ~/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

答案1

得分: 8

所以我们有两个不同的容器,这意味着在这种情况下有两个不同的“localhost”。

client := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "",
DB: 0,
})

因此,您的应用程序正在向其自己的沙箱容器发送请求,而不是向包含redis的“其他”沙箱容器发送请求。

您有两个选项:

在您的compose文件中提供一个映射,例如redisdb:db,并传递该信息而不是localhost。

或者,使用“--net=host”选项为您的容器提供共享网络,而无需更改您的代码。

编辑:拼写错误

英文:

So we have two different containers which means two different "localhost" in this case.

client := redis.NewClient(&redis.Options{
    Addr: "localhost:6379",
    Password: "",
    DB: 0,
})

So, your app is making requests to its own sandboxed container, not to your "other" sandboxed container which includes redis.

You have two options;

Give a mapping in your compose file like redisdb:db and pass that information instead of localhost.

Or, use the "--net=host" option in order to provide common networking for your containers without changing your code.

edit: typo

答案2

得分: 0

@Gladmir的回答很好。只是为了扩展他/她的回答,我不需要从我的Golang代码中删除localhost

client := redis.NewClient(&redis.Options{
    Addr:     "localhost:6379",
    Password: "",
    DB:       0,
})

我改变了我的Docker Compose文件,使用了network_mode: "host"

version: "3.9"
services:
  web:
    build:
      context: .
    network_mode: "host"
  redis:
    container_name: "redis"
    image: "redis:alpine"
    command: redis-server /usr/local/etc/redis/redis.conf
    ports:
      - "6379:6379"
    volumes:
      - $PWD/configs/redis.conf:/usr/local/etc/redis/redis.conf
英文:

The answer from @Gladmir is great. Just to expand on his/her answer, I didn't need to remove localhost from my Golang code:

client := redis.NewClient(&redis.Options{
	Addr:     "localhost:6379",
	Password: "",
	DB:       0,
})

I changeed my Docker Compose file to use network_mode: "host":

version: "3.9"
services:
  web:
    build:
      context: .
    network_mode: "host"
  redis:
    container_name: "redis"
    image: "redis:alpine"
    command: redis-server /usr/local/etc/redis/redis.conf
    ports:
      - "6379:6379"
    volumes:
      - $PWD/configs/redis.conf:/usr/local/etc/redis/redis.conf

huangapple
  • 本文由 发表于 2015年11月20日 14:06:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/33820025.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定