登录
首页 >  Golang >  Go问答

Web服务器无法通过docker-compose连接到Redis

来源:stackoverflow

时间:2024-04-22 21:36:32 458浏览 收藏

哈喽!今天心血来潮给大家带来了《Web服务器无法通过docker-compose连接到Redis》,想必大家应该对Golang都不陌生吧,那么阅读本文就都不会很困难,以下内容主要涉及到,若是你正在学习Golang,千万别错过这篇文章~希望能帮助到你!

问题内容

我正在尝试通过同一网络上的网络服务器连接到 docker 网络上的 redis 实例。我的代码在 go 中并且我正在使用 我的 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

env.list 文件的相关部分:

redis_host=thor-redis
redis_port=6380
redis_db=0
redis_password=redis_pass

我的应用程序中连接到 redis 的代码是:

a.redis = redis.newclient(&redis.options{
    addr:       fmt.sprintf("%s:%s", conf.redishost, conf.redisport),
    password:   conf.redispassword,
    db:         conf.redisdb,
})

我的填充conf的代码是:

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
}

在redis.conf文件中,没有指定绑定ip,并且requirepass设置为与redis_pass相同的值。此外,我在服务器上看到的错误是:

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

当我检查 redis 映像时,我可以确认它具有以下 ip 地址:

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

当我尝试通过 redis-cli 连接本地计算机时,我可以毫无问题地访问服务器。我在谷歌上搜索了一下,试图找到类似的问题,但它们似乎都与试图通过本地主机访问 redis 的人有关,而我并不想这样做。非常感谢任何帮助。


解决方案


您可能忘记从网络角度将您的应用程序与 redis 链接

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

好了,本文到此结束,带大家了解了《Web服务器无法通过docker-compose连接到Redis》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>