Connecting golang and redis through docker
Asked Answered
B

2

5

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
Breault answered 20/11, 2015 at 6:6 Comment(1)
before invoke the source, use telnet or nc to check the connectivity.Strangulate
R
8

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

Roughandready answered 20/11, 2015 at 9:54 Comment(2)
Thank you so much. I simply changed app.go to Addr: "db:6379",Breault
Working version on githubBreault
C
0

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
Ciprian answered 4/5, 2022 at 9:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.