当前位置: > > > > 在没有 Docker Compose 的情况下启动 Redis 容器时,拨号 tcp 127.0.0.1:6379 时出错:连接:连接被拒绝
来源:stackoverflow
2024-04-22 18:51:31
0浏览
收藏
本篇文章主要是结合我之前面试的各种经历和实战开发中遇到的问题解决经验整理的,希望这篇《在没有 Docker Compose 的情况下启动 Redis 容器时,拨号 tcp 127.0.0.1:6379 时出错:连接:连接被拒绝》对你有很大帮助!欢迎收藏,分享给更多的需要的朋友学习~
问题内容
我尝试使用以下 dockerfile 运行 redis 容器。
from golang:alpine as builder label maintainer="..." run apk update && apk add --no-cache git workdir /app copy go.mod go.sum ./ run go mod download copy . . run cgo_enabled=0 goos=linux go build -a -installsuffix cgo -o main . from alpine:latest run apk --no-cache add ca-certificates workdir /root/ copy --from=builder /app/main . expose 6379 cmd ["./main"]
然后我就跑了
docker build -t redis . docker run -dp 6379:6379 redis
之后,这一边的代码出现了错误:
s.Client = redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", DB: 0, }) if err := s.Client.Ping().Err(); err != nil { log.Fatalf("Failed to create a Redis client: %s", err) }
我在stackoverflow中读到了一些类似的问题,并尝试将地址更改为redis:6379
,但没有成功。有人可以帮我解释一下为什么出现此连接拒绝错误吗?
我读过的一些问题:
- docker redis 连接被拒绝
- docker compose up后,后端到redis的连接被拒绝
正确答案
您的镜像基于 alpine,而不是 redis 镜像。而且我看不到你在 Dockerfile 中的什么位置安装了 redis。
今天关于《在没有 Docker Compose 的情况下启动 Redis 容器时,拨号 tcp 127.0.0.1:6379 时出错:连接:连接被拒绝》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注公众号!