Docker Redis 连接被拒绝

2022-01-14 00:00:00 docker redis dockerfile php

我正在尝试通过代码访问 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;
}

相关文章