doudu161481
doudu161481
采纳率100%
2017-03-27 06:42 阅读 664

尝试使用docker compose文件连接Redis时连接被拒绝

我在尝试使用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/数据库/allSystem.go)的代码:

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

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

我试图改变 localhostredis 但它还是不管用。错误如下:

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

你知道哪里出错了吗?

源代码

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    dongnaopa6200 dongnaopa6200 2017-03-27 09:23

    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 dbfirst. 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.

    点赞 评论 复制链接分享
  • douceng7070 douceng7070 2017-03-27 07:53

    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.
    点赞 评论 复制链接分享

相关推荐