doudu161481 2017-03-27 06:42 采纳率: 100%
浏览 1714
已采纳

尝试使用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 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧