Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting golang and redis through docker

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
like image 593
Mr Wilde Avatar asked Nov 20 '15 06:11

Mr Wilde


1 Answers

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

like image 51
Gladmir Avatar answered Oct 30 '22 13:10

Gladmir