尝试使用Docker Compose文件连接Redis时出现连接被拒绝的错误。

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

connection refused when trying to connect redis using docker compose file

问题

我遇到了一个问题,尝试使用redis-golang驱动程序连接到Redis。这是我的docker-compose.yml文件:

  1. version: "3"
  2. services:
  3. driver:
  4. build: ./API-Golang
  5. command: go run app.go
  6. volumes:
  7. - ./API-Golang:/app
  8. ports:
  9. - "8080:8080"
  10. depends_on:
  11. - db
  12. - redis
  13. links :
  14. - redis
  15. - db
  16. redis:
  17. image: redis
  18. container_name: redis
  19. ports: ["6379:6379"]
  20. db:
  21. image: mongo:3.4.2
  22. container_name: mongodb
  23. ports : ["27017:27017"]

这是我尝试连接到Redis的代码(API-Golang/database/allSystem.go):

  1. redisConn := RedisHost{
  2. Address: "localhost:6379",
  3. Password: "",
  4. DB: 0,
  5. }
  6. redisConnection, err := redisConn.Connect()
  7. if err != nil {
  8. panic(err)
  9. }

我尝试将localhost更改为redis作为主机,但仍然无法工作。这是错误信息:

  1. panic: dial tcp [::1]:6379: getsockopt: connection refused
  2. goroutine 1 [running]:
  3. github.com/Gujarats/API-Golang/database.SystemConnection(0x48)
  4. /go/src/github.com/Gujarats/API-Golang/database/allSystem.go:32 +0x3d3
  5. main.main()
  6. /go/src/github.com/Gujarats/API-Golang/app.go:24 +0x34
  7. exit status 2

有什么想法是什么出了问题?

源代码

英文:

I have a problem trying to connect to redis using redis-golang driver.
this is my docker-compose.yml file :

  1. version: "3"
  2. services:
  3. driver:
  4. build: ./API-Golang
  5. command: go run app.go
  6. volumes:
  7. - ./API-Golang:/app
  8. ports:
  9. - "8080:8080"
  10. depends_on:
  11. - db
  12. - redis
  13. links :
  14. - redis
  15. - db
  16. redis:
  17. image: redis
  18. container_name: redis
  19. ports: ["6379:6379"]
  20. db:
  21. image: mongo:3.4.2
  22. container_name: mongodb
  23. ports : ["27017:27017"]

and this the code where I try to connect to redis (API-Golang/database/allSystem.go):

  1. redisConn := RedisHost{
  2. Address: "localhost:6379",
  3. Password: "",
  4. DB: 0,
  5. }
  6. redisConnection, err := redisConn.Connect()
  7. if err != nil {
  8. panic(err)
  9. }

I have tried to change localhost to redis for the host but still it doesn't work. Here is the error :

  1. driver_1 | panic: dial tcp [::1]:6379: getsockopt: connection refused
  2. driver_1 |
  3. driver_1 | goroutine 1 [running]:
  4. driver_1 | github.com/Gujarats/API-Golang/database.SystemConnection(0x48)
  5. driver_1 | /go/src/github.com/Gujarats/API-Golang/database/allSystem.go:32 +0x3d3
  6. driver_1 | main.main()
  7. driver_1 | /go/src/github.com/Gujarats/API-Golang/app.go:24 +0x34
  8. driver_1 | exit status 2

Any idea what's wrong?

Source Code

答案1

得分: 1

使用depends_on时需要注意以下几点:

  • depends_on不会等待db和redis“准备就绪”再启动web服务,只会等待它们启动。如果需要等待服务准备就绪,请参考控制启动顺序以了解此问题及解决策略。

  • 版本3不再支持depends_on的条件形式。

  • 在使用版本3的Compose文件进行Swarm模式下的堆栈部署时,depends_on选项将被忽略。

英文:

There are several things to be aware of when using depends_on:

  • depends_on will not wait for db and redis to be “ready” before starting web - only until they have been started. If you need to wait for a service to be ready, see Controlling startup order for more on this problem and strategies for solving it.
  • Version 3 no longer supports the condition form of depends_on.
  • The depends_on option is ignored when deploying a stack in swarm mode with a version 3 Compose file.

答案2

得分: 0

这很愚蠢,我把depends_on写错了顺序,我修复了这个问题,像这样按顺序写:

  1. depends_on:
  2. - redis
  3. - db

之前我先写了db。不要忘记将主机更改为适当的名称,就像在docker-compose.yml文件中一样。在这种情况下,redis = redis,mongodb = db

我忘记在app.go中先调用redis,然后是mongodb。

英文:

This is stupid I wrote the depends_on not in order I fix the issue writing the order like this :

  1. depends_on:
  2. - redis
  3. - db

Before this I wrote it db first. and do not forget to change the host to appropriate name just like in the docker-compose.yml file. In this case is redis = redis and mongodb = db.

I forget that in app.go I call redis first then mongodb.

huangapple
  • 本文由 发表于 2017年3月27日 14:42:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/43039501.html
匿名

发表评论

匿名网友

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

确定