0

I am trying to connect to a redis instance on a docker network via a webserver on the same network. My code is in go and I'm using My docker-compose.yml:

thor-redis:
    image: redis:5.0.7
    networks:
      - thorcast
    ports:
      - "6380:6379"
    volumes:
      - ./redis.conf:/usr/local/etc/redis.conf
    entrypoint: redis-server /usr/local/etc/redis.conf
thorcast-server:
    build: ../../thorcast-server
    volumes:
      - ../../thorcast-server:/app/thorcast-server
    networks:
      - thorcast
    ports:
      - "8000:8000"
    env_file:
      - ../../thorcast-server/env.list

Relevant section of the env.list file:

REDIS_HOST=thor-redis
REDIS_PORT=6380
REDIS_DB=0
REDIS_PASSWORD=redis_pass

The code in my app to connect to redis is:

a.Redis = redis.NewClient(&redis.Options{
    Addr:       fmt.Sprintf("%s:%s", conf.redisHost, conf.redisPort),
    Password:   conf.redisPassword,
    DB:         conf.redisDb,
})

My code for populating conf is:

type config struct {
    sqlUsername     string
    sqlPassword     string
    sqlHost         string
    sqlPort         string
    sqlDbName       string
    redisPassword   string
    redisHost       string
    redisPort       string
    redisDb         int
}
// method to initialize config struct from environment variables
func (conf *config) configure() {
    conf.sqlUsername = os.Getenv("THORCAST_DB_USERNAME")
    conf.sqlPassword = os.Getenv("THORCAST_DB_PASSWORD")
    conf.sqlHost = os.Getenv("THORCAST_DB_HOST")
    conf.sqlPort = os.Getenv("THORCAST_DB_PORT")
    conf.sqlDbName = os.Getenv("THORCAST_DB_NAME")
    conf.redisPassword = os.Getenv("REDIS_PASSWORD")
    conf.redisHost = os.Getenv("REDIS_HOST")
    conf.redisPort = os.Getenv("REDIS_PORT")
    conf.redisDb, _ = strconv.Atoi(os.Getenv("REDIS_DB"))
}
var conf = config{}

type App struct {
    Router *mux.Router
    Logger http.Handler
    DB     *sql.DB
    Redis  *redis.Client
}

In the redis.conf file, no bind IPs are specified, and requirepass is set to the same value as redis_pass. Furthermore, the error I'm seeing on my server is:

{"error":"dial tcp 172.21.0.5:6380: connect: connection refused"}

When I inspect the redis image, I can confirm it has this IP address:

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' dev_thor- 
redis_1
172.21.0.5

When I try to connect off of my local machine via redis-cli, I can access the server without issue. I have googled around to try and find similar issues, but they all seem to be with people trying to access redis via localhost, which I am not trying to do. Any help is greatly appreciated.

K Pekosh
  • 633
  • 1
  • 6
  • 15
  • 2
    `ports:` don't affect inter-container connections (and aren't required). Use the default Redis port 6379. – David Maze Feb 19 '20 at 13:25
  • @DavidMaze thanks for letting me know. I should mention that I have a Postgres container running as well, forwarding 5436:5432, and the webserver connects to that db via the exposed port, so I replicated the pattern with redis. – K Pekosh Feb 19 '20 at 15:21
  • Are you sure that redis is running at the time you try to connect to it? – todinov Feb 20 '20 at 14:27
  • Yes, I am able to connect to the server from my local machine after spinning up the containers. – K Pekosh Feb 20 '20 at 14:54

1 Answers1

-2

You may forgot to link you app with redis from network poitn of view

    thor-redis:
        image: redis:5.0.7
        networks:
          - thorcast
        ports:
          - "6380:6379"
        volumes:
          - ./redis.conf:/usr/local/etc/redis.conf
        entrypoint: redis-server /usr/local/etc/redis.conf
    thorcast-server:
        build: ../../thorcast-server
        volumes:
          - ../../thorcast-server:/app/thorcast-server
        networks:
          - thorcast
        ports:
          - "8000:8000"
        env_file:
          - ../../thorcast-server/env.list
        links:          # <-- Add this
          - thor-redis  # <-- Add this
J-Jacques M
  • 978
  • 6
  • 16
  • that is not needed. They are on the same network anyway. Plus the links declaration will soon be depricated anyway – Mihai Feb 19 '20 at 13:44