Home > OS >  Error dial tcp 127.0.0.1:6379: connect: connection refused when starting Redis container without Doc
Error dial tcp 127.0.0.1:6379: connect: connection refused when starting Redis container without Doc

Time:09-30

I tried running a Redis container with the following Dockerfile.

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"]

Then, I ran

docker build -t redis .
docker run -dp 6379:6379 redis

Afterwards, there is an error on this side of the code:

    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)
    }

I have read some similar questions in Stackoverflow and tried changing the address to redis:6379, but it didn't work. Could someone help me explain why there is this connection refused error?

Some questions I've read:

CodePudding user response:

You image is based on alpine, not on redis image. And I can't see where do you install redis in your Dockerfile.

  • Related