英文:
How to get docker-compose container to see Redis host?
问题
我有这个简单的docker-compose.yml
文件:
version: '3.8'
services:
bot:
build:
dockerfile: Dockerfile
context: .
links:
- redis
depends_on:
- redis
redis:
image: redis:7.0.0-alpine
ports:
- "6379:6379"
environment:
- REDIS_REPLICATION_MODE=master
restart: always
volumes:
- cache:/data
command: redis-server
volumes:
cache:
driver: local
这是bot
(使用Go语言)连接到redis
的方式:
import "github.com/go-redis/redis/v8"
func setRedisClient() {
rdb = redis.NewClient(&redis.Options{
Addr: "redis:6379",
Password: "",
DB: 0,
})
}
bot
的Dockerfile:
FROM golang:1.18.3-alpine3.16
WORKDIR /go/src/bot-go
COPY . .
RUN go build .
RUN ./bot-go
但是当我运行docker-compose up --build
时,我总是得到以下错误:
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:
version: '3.8'
services:
bot:
build:
dockerfile: Dockerfile
context: .
links:
- redis
depends_on:
- redis
redis:
image: redis:7.0.0-alpine
ports:
- "6379:6379"
environment:
- REDIS_REPLICATION_MODE=master
restart: always
volumes:
- cache:/data
command: redis-server
volumes:
cache:
driver: local
This is how the bot
(in Go) connects to redis
:
import "github.com/go-redis/redis/v8"
func setRedisClient() {
rdb = redis.NewClient(&redis.Options{
Addr: "redis:6379",
Password: "",
DB: 0,
})
}
bot
Dockerfile:
FROM golang:1.18.3-alpine3.16
WORKDIR /go/src/bot-go
COPY . .
RUN go build .
RUN ./bot-go
But when I run docker-compose up --build
I always get:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论