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

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

connection refused when trying to connect redis using docker compose file

问题

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

version: "3"

services:
  driver:
    build: ./API-Golang
    command: go run app.go
    volumes:
     - ./API-Golang:/app
    ports:
      - "8080:8080"
    depends_on:
      - db
      - redis
    links :
      - redis
      - db
  redis:
    image: redis
    container_name: redis
    ports: ["6379:6379"]
  db:
    image: mongo:3.4.2
    container_name: mongodb
    ports : ["27017:27017"]

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

redisConn := RedisHost{
	Address:  "localhost:6379",
	Password: "",
	DB:       0,
}

redisConnection, err := redisConn.Connect()
if err != nil {
	panic(err)
}

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

panic: dial tcp [::1]:6379: getsockopt: connection refused

goroutine 1 [running]:
github.com/Gujarats/API-Golang/database.SystemConnection(0x48)
	/go/src/github.com/Gujarats/API-Golang/database/allSystem.go:32 +0x3d3
main.main()
	/go/src/github.com/Gujarats/API-Golang/app.go:24 +0x34
exit status 2

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

源代码

英文:

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

version: "3"

services:
  driver:
    build: ./API-Golang
    command: go run app.go
    volumes:
     - ./API-Golang:/app
    ports:
      - "8080:8080"
    depends_on:
      - db
      - redis
    links :
      - redis
      - db
  redis:
    image: redis
    container_name: redis
    ports: ["6379:6379"]
  db:
    image: mongo:3.4.2
    container_name: mongodb
    ports : ["27017:27017"]

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

redisConn := RedisHost{
	Address:  "localhost:6379",
	Password: "",
	DB:       0,
}

redisConnection, err := redisConn.Connect()
if err != nil {
	panic(err)
}

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

driver_1  | panic: dial tcp [::1]:6379: getsockopt: connection refused
driver_1  | 
driver_1  | goroutine 1 [running]:
driver_1  | github.com/Gujarats/API-Golang/database.SystemConnection(0x48)
driver_1  | 	/go/src/github.com/Gujarats/API-Golang/database/allSystem.go:32 +0x3d3
driver_1  | main.main()
driver_1  | 	/go/src/github.com/Gujarats/API-Golang/app.go:24 +0x34
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写错了顺序,我修复了这个问题,像这样按顺序写:

depends_on:
  - redis
  - 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 :

depends_on:
  - redis
  - 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:

确定