dongqiao8502 2019-05-15 12:19
浏览 448

无法使用Redis连接到单独的Docker容器

I have redis exposed via a container:

service_redis 0.0.0.0:6379->6379/tcp

And I can even connect to it via redis-cli command with. But what I actually can't do, is to connect to via Go code (which also exists in a separate container, btw)

Here's how my client looks like:

opts := &redis.Options{
    Addr: addr,
    Password: pass,
    DB: db,
}


cli := redis.NewClient(opts)
fmt.Println(master.Ping().Err())
fmt.Println(slave.Get("REAL_KEY").String())

I've tried to put in the addr variable the following:

  1. 0.0.0.0:6379 AND 127.0.0.1:6379. The response was e.g.:
<nil>
get REAL KEY: dial tcp 0.0.0.0:6379: connect: connection refused
  1. service_redis:6379 with the following response:

<nil>
get REAL KEY: dial tcp: lookup service_redis on 169.254.169.254:53: no such host

How can I solve the problem and what did I wrong?

  • 写回答

2条回答 默认 最新

  • duanning9110 2019-05-15 12:54
    关注

    You could use something like this

    docker-compose.yaml

    web:
        build: .
        working_dir: /go/src
        ports:
         - "5000:5000"
        volumes:
         - .:/go/src
        links:
         - db
    db:
        image: redis
        ports:
         - "6379:6379"
    

    Its creating a link between you containers so they can communicate to eachother. If you don't want to use docker-compose.yaml then you could use the --link flag when building your image

    评论

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示