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.