<legend id='6Y8nY'><style id='6Y8nY'><dir id='6Y8nY'><q id='6Y8nY'></q></dir></style></legend>

<small id='6Y8nY'></small><noframes id='6Y8nY'>

    <i id='6Y8nY'><tr id='6Y8nY'><dt id='6Y8nY'><q id='6Y8nY'><span id='6Y8nY'><b id='6Y8nY'><form id='6Y8nY'><ins id='6Y8nY'></ins><ul id='6Y8nY'></ul><sub id='6Y8nY'></sub></form><legend id='6Y8nY'></legend><bdo id='6Y8nY'><pre id='6Y8nY'><center id='6Y8nY'></center></pre></bdo></b><th id='6Y8nY'></th></span></q></dt></tr></i><div id='6Y8nY'><tfoot id='6Y8nY'></tfoot><dl id='6Y8nY'><fieldset id='6Y8nY'></fieldset></dl></div>

    1. <tfoot id='6Y8nY'></tfoot>

        • <bdo id='6Y8nY'></bdo><ul id='6Y8nY'></ul>

        Docker Redis 连接被拒绝

        时间:2024-05-11

          <tbody id='HPFfU'></tbody>
          <bdo id='HPFfU'></bdo><ul id='HPFfU'></ul>

          <legend id='HPFfU'><style id='HPFfU'><dir id='HPFfU'><q id='HPFfU'></q></dir></style></legend>
        • <i id='HPFfU'><tr id='HPFfU'><dt id='HPFfU'><q id='HPFfU'><span id='HPFfU'><b id='HPFfU'><form id='HPFfU'><ins id='HPFfU'></ins><ul id='HPFfU'></ul><sub id='HPFfU'></sub></form><legend id='HPFfU'></legend><bdo id='HPFfU'><pre id='HPFfU'><center id='HPFfU'></center></pre></bdo></b><th id='HPFfU'></th></span></q></dt></tr></i><div id='HPFfU'><tfoot id='HPFfU'></tfoot><dl id='HPFfU'><fieldset id='HPFfU'></fieldset></dl></div>

                • <tfoot id='HPFfU'></tfoot>

                  <small id='HPFfU'></small><noframes id='HPFfU'>

                • 本文介绍了Docker Redis 连接被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试通过代码访问 Redis 服务器,但它没有连接.但是如果我 bash 到 redis 容器,我可以访问 redis-cli.

                  I'm trying to access Redis server through the code and it's not connecting. But if i bash to the redis container i can access the redis-cli.

                  docker-compose.yml 看起来像这样

                  docker-compose.yml looks like this

                  version: '2'
                  services:
                    web:
                     build:
                      context: .
                      dockerfile: Dockerfile_nginx
                     ports:
                      - "9000:80"
                     environment:
                      - NGINX_SERVERNAME=xxx.dev *.xxx.dev
                     command: /bin/bash -c "envsubst '$$NGINX_SERVERNAME' < /var/www/site.template > /etc/nginx/conf.d/default.conf
                                            && dos2unix /var/www/provision/init_storage.sh && sh /var/www/provision/init_storage.sh
                                            && nginx -g 'daemon off;'"
                     volumes:
                       - .:/var/www
                     links:
                       - php
                     networks:
                       frontend
                  
                    php:
                      build:
                        context: .
                        dockerfile: Dockerfile_php-fpm
                      command: /bin/bash -c "composer install
                                && php-fpm"
                      volumes:
                            - .:/var/www
                      environment:
                            - APP_ENV=local
                            - APP_DEBUG=true
                      networks:
                        - frontend
                        - backend
                      links:
                           - redis
                    db:
                      build:
                        context: .
                        dockerfile: Dockerfile_mariadb
                      volumes:
                        - ./initdb:/docker-entrypoint-initdb.d
                      ports:
                        - "3309:3306"
                      environment:
                        MYSQL_ROOT_PASSWORD: xxxx
                        MYSQL_DATABASE: xxxx
                      networks:
                        - backend
                    redis:
                      build:
                        context: .
                        dockerfile: Dockerfile_redis
                      ports:
                        - "6379:6379"
                  
                  networks:
                    frontend:
                      driver: bridge
                    backend:
                      driver: bridge
                  

                  Dockerfile_redis

                  Dockerfile_redis

                  FROM redis:latest
                  

                  当我尝试使用此代码连接到 redis 服务器时

                  When i try to connect to the redis server using this code

                  $redis = new Redis();
                      try {
                          $redis->connect('127.0.0.1', 6379);
                      } catch (Exception $e) {
                          var_dump($e->getMessage())  ;
                          die;
                      }
                  

                  它给出了这个警告

                  Warning: Redis::connect(): connect() failed: Connection refused
                  

                  有人知道如何将 Redis 容器连接到 PHP 容器吗?

                  Does anyone know how to connect Redis container to PHP container ?

                  推荐答案

                  你的问题

                  Docker Compose 为不同的服务创建独立的 docker 容器.从逻辑上讲,每个容器就像不同的独立计算机服务器,它们只能通过 docker 网络相互连接.

                  Your Problem

                  Docker Compose creates separated docker container for different services. Each container are, logically speaking, like different separated computer servers that only connected with each other through docker network.

                  将此图中的每个框视为一台单独的计算机,那么这实际上就是您所拥有的:

                  Consider each boxes in this diagram as an individual computer, then this is practically what you have:

                  +----------------------------------------------------------+
                  |                       your machine                       |
                  +----------------------------------------------------------+
                                                 |                    
                          +------ (virtual network by docker) -------+
                          |                      |                   |
                  +-----------------+ +-------------------+ +----------------+
                  | "php" container | | "redis" container | | "db" container |
                  +-----------------+ +-------------------+ +----------------+
                  

                  您的 PHP 容器在localhost"中看不到任何 redis,因为其中没有 redis.就像它在localhost"中看不到任何 MySQL 一样.您的 redis 正在redis"容器中运行.您的 MySQL 正在您的db"容器中运行.

                  Your PHP container doesn't see any redis in "localhost" because there is no redis in it. Just like it would't see any MySQL in "localhost". Your redis is running in the "redis" container. Your MySQL is running in your "db" container.

                  让您感到困惑的是端口绑定指令(即此定义中的 ports):

                  The things that confuses you is the port binding directives (i.e. ports in this definition):

                  redis:
                    build:
                      context: .
                      dockerfile: Dockerfile_redis
                    ports:
                      - "6379:6379"
                  

                  redis"容器的端口 6379 绑定到您的计算机,但仅绑定到您的计算机.其他容器对端口绑定没有相同的访问权限.所以即使你的电脑可以连接到 '127.0.0.1:6379',php 容器也做不到.

                  The port 6379 of the "redis" container is binded to your computer, but to your computer ONLY. Other container doesn't have the same access to the port bindings. So even your computer can connect it with '127.0.0.1:6379', the php container cannot do the same.

                  如Docker Compose 中的网络 中所述,每个 docker compose 容器可以通过以下方式访问其他容器使用服务名称作为主机名.例如,您通过服务 php 运行的程序可以使用主机名 db 访问您的 MySQL 服务.

                  As described in Networking in Docker Compose, each docker compose container can access other container by using the service name as hostname. For example, your programming running by service php can access your MySQL service with the hostname db.

                  所以你应该用它的主机名连接redis redis

                  So you should connect redis with its hostname redis

                  $redis = new Redis();
                  try {
                      $redis->connect('redis', 6379);
                  } catch (Exception $e) {
                      var_dump($e->getMessage())  ;
                      die;
                  }
                  

                  这篇关于Docker Redis 连接被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何使用官方 PHP Docker 镜像方法安装 php-redis 扩展? 下一篇:在 WooCommerce 中禁用基于用户国家地理 IP 的所有付款方式

                  相关文章

                  <legend id='yiVkN'><style id='yiVkN'><dir id='yiVkN'><q id='yiVkN'></q></dir></style></legend>

                      • <bdo id='yiVkN'></bdo><ul id='yiVkN'></ul>
                    1. <small id='yiVkN'></small><noframes id='yiVkN'>

                    2. <i id='yiVkN'><tr id='yiVkN'><dt id='yiVkN'><q id='yiVkN'><span id='yiVkN'><b id='yiVkN'><form id='yiVkN'><ins id='yiVkN'></ins><ul id='yiVkN'></ul><sub id='yiVkN'></sub></form><legend id='yiVkN'></legend><bdo id='yiVkN'><pre id='yiVkN'><center id='yiVkN'></center></pre></bdo></b><th id='yiVkN'></th></span></q></dt></tr></i><div id='yiVkN'><tfoot id='yiVkN'></tfoot><dl id='yiVkN'><fieldset id='yiVkN'></fieldset></dl></div>

                    3. <tfoot id='yiVkN'></tfoot>