如何让docker-compose容器看到Redis主机?

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

How to get docker-compose container to see Redis host?

问题

我有这个简单的docker-compose.yml文件:

  1. version: '3.8'
  2. services:
  3. bot:
  4. build:
  5. dockerfile: Dockerfile
  6. context: .
  7. links:
  8. - redis
  9. depends_on:
  10. - redis
  11. redis:
  12. image: redis:7.0.0-alpine
  13. ports:
  14. - "6379:6379"
  15. environment:
  16. - REDIS_REPLICATION_MODE=master
  17. restart: always
  18. volumes:
  19. - cache:/data
  20. command: redis-server
  21. volumes:
  22. cache:
  23. driver: local

这是bot(使用Go语言)连接到redis的方式:

  1. import "github.com/go-redis/redis/v8"
  2. func setRedisClient() {
  3. rdb = redis.NewClient(&redis.Options{
  4. Addr: "redis:6379",
  5. Password: "",
  6. DB: 0,
  7. })
  8. }

bot的Dockerfile:

  1. FROM golang:1.18.3-alpine3.16
  2. WORKDIR /go/src/bot-go
  3. COPY . .
  4. RUN go build .
  5. RUN ./bot-go

但是当我运行docker-compose up --build时,我总是得到以下错误:

  1. panic: dial tcp: lookup redis on 192.168.65.5:53: no such host

无论我对主机还是对docker-compose文件进行了什么更改,都无法找到redis主机。

当我将客户端配置为本地时,应用程序在没有Docker的情况下可以正常工作。

我到底做错了什么?

英文:

I have this simple docker-compose.yml file:

  1. version: '3.8'
  2. services:
  3. bot:
  4. build:
  5. dockerfile: Dockerfile
  6. context: .
  7. links:
  8. - redis
  9. depends_on:
  10. - redis
  11. redis:
  12. image: redis:7.0.0-alpine
  13. ports:
  14. - "6379:6379"
  15. environment:
  16. - REDIS_REPLICATION_MODE=master
  17. restart: always
  18. volumes:
  19. - cache:/data
  20. command: redis-server
  21. volumes:
  22. cache:
  23. driver: local

This is how the bot (in Go) connects to redis:

  1. import "github.com/go-redis/redis/v8"
  2. func setRedisClient() {
  3. rdb = redis.NewClient(&redis.Options{
  4. Addr: "redis:6379",
  5. Password: "",
  6. DB: 0,
  7. })
  8. }

bot Dockerfile:

  1. FROM golang:1.18.3-alpine3.16
  2. WORKDIR /go/src/bot-go
  3. COPY . .
  4. RUN go build .
  5. RUN ./bot-go

But when I run docker-compose up --build I always get:

  1. panic: dial tcp: lookup redis on 192.168.65.5:53: no such host

redis host is never seen no matter what changes I make to the host or to docker-compose file.

The app does work without Docker when I configure the client to local.

What I am doing wrong exactly?

答案1

得分: 3

问题是bot-go镜像一直在构建。在Dockerfile中将RUN ./bot-go改为CMD ["./bot-go"],一切都会正常工作。

英文:

The problem is the bot-go image never stops building. Change RUN ./bot-go to CMD [ "./bot-go" ] in the Dockerfile and everything will work fine.

huangapple
  • 本文由 发表于 2022年6月4日 23:44:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/72501074.html
匿名

发表评论

匿名网友

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

确定