Go后端在使用Docker Compose启动后,无法连接到Redis,提示连接被拒绝。

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

Go backend to redis connection refused after docker compose up

问题

我目前正在尝试将Docker Compose引入我的项目中。它包括一个使用Redis内存数据库的Golang后端。

以下是Docker Compose文件的内容:

version: "3.9"
services:
  frontend:
    ...
  backend:
    build:
      context: ./backend
    ports:
      - "8080:8080"
    environment:
      - NODE_ENV=production
    env_file:
      - ./backend/.env
  redis:
    image: "redis"
    ports:
      - "6379:6379"

以下是后端Dockerfile的内容:

FROM golang:1.16-alpine
RUN mkdir -p /usr/src/app
ENV PORT 8080
WORKDIR /usr/src/app
COPY go.mod /usr/src/app
COPY . /usr/src/app
RUN go build -o main .
EXPOSE 8080
CMD ["./main"]

构建成功后,但在启动服务后,Go后端立即退出,并抛出以下错误:

Error trying to ping redis: dial tcp 127.0.0.1:6379: connect: connection refused

错误被捕获在以下代码中:

_, err = client.Ping(ctx).Result()
if err != nil {
	log.Fatalf("Error trying to ping redis: %v", err)
}

为什么后端Docker服务无法连接到Redis?重要提示:当Redis服务正在运行并且我手动使用go run *.go启动后端时,没有错误,后端成功启动。

英文:

I'm currently trying to introduce docker compose to my project. It includes a golang backend using the redis in-memory database.

version: "3.9"
services:
  frontend:
    ...
  backend:
    build:
      context: ./backend
    ports:
      - "8080:8080"
    environment:
      - NODE_ENV=production
    env_file:
      - ./backend/.env
  redis:
    image: "redis"
    ports:
      - "6379:6379"
FROM golang:1.16-alpine
RUN mkdir -p /usr/src/app
ENV PORT 8080
WORKDIR /usr/src/app
COPY go.mod /usr/src/app
COPY . /usr/src/app
RUN go build -o main .
EXPOSE 8080
CMD [ "./main" ]

The build runs successfully, but after starting the services, the go backend immediately exits throwing following error:

> Error trying to ping redis: dial tcp 127.0.0.1:6379: connect: connection refused

Error being catched here:

_, err = client.Ping(ctx).Result()
if err != nil {
	log.Fatalf("Error trying to ping redis: %v", err)
}

How come the backend docker service isn't able to connect to redis? Important note: when the redis service is running and I start my backend manually using go run *.go, there's no error and the backend starts successfully.

答案1

得分: 7

当您在Docker容器中运行Go应用程序时,本地主机IP 127.0.0.1 是指向该容器的。您应该使用Redis容器的主机名来从Go容器连接,因此您的连接字符串应为:

redis://redis
英文:

When you run your Go application inside a docker container, the localhost IP 127.0.0.1 is referring to this container. You should use the hostname of your Redis container to connect from your Go container, so your connection string would be:

redis://redis

答案2

得分: 4

我发现我遇到了同样的问题。只需将redis.NewClient(&redis.Options{...})中的Addr: "localhost:6379"改为Addr: "redis:6379"即可解决。

英文:

I found I was having this same issue. Simply changing (in redis.NewClient(&redis.Options{...}) Addr: "localhost:6379"to Addr: "redis:6379" worked.

答案3

得分: 0

在使用Golang和Redis时遇到了类似的问题。

版本:'3.0'
服务:
redisdb:
image:redis:6.0
restart:always
ports:
- "6379:6379"
container_name:redisdb-container
command:["redis-server", "--bind", "redisdb", "--port", "6379"]

urlshortnerservice:
depends_on:
- redisdb
ports:
- "7777:7777"
restart:always
container_name:url-shortner-container
image:url-shortner-service

在Redis配置中使用:

redisClient := redis.NewClient(&redis.Options{
Addr: "redisdb:6379",
Password: "",
DB: 0,
})

英文:

Faced similar issue with Golang and redis.

version: '3.0'
services:
  redisdb:
    image: redis:6.0
    restart: always
    ports:
      - "6379:6379"
    container_name: redisdb-container
    command: ["redis-server", "--bind", "redisdb", "--port", "6379"]

  urlshortnerservice:
    depends_on:
      - redisdb
    ports:
      - "7777:7777"
    restart: always
    container_name: url-shortner-container
    image: url-shortner-service

In redis configuration use

redisClient := redis.NewClient(&redis.Options{
		Addr:     "redisdb:6379",
 		Password: "",
 		DB:       0,
 	})

huangapple
  • 本文由 发表于 2021年6月13日 02:14:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/67951731.html
匿名

发表评论

匿名网友

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

确定