Home > Software engineering >  How to get docker-compose container to see Redis host?
How to get docker-compose container to see Redis host?

Time:06-05

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?

CodePudding user response:

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.

  • Related