Docker Redis connection refused

I am trying to access a Redis server through code and am not connecting. But if I bash into the redis container, I can access redis-cli.

docker-compose.yml is as follows

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

FROM redis:latest

When I try to connect to a redis server using this code

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

He gives this warning

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

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

+11
source share
4 answers

Your problem

Docker Compose creates a separate docker container for different services. Each container, logically, as well as different separate computer servers that are only connected to each other through the docker network.

, , :

+----------------------------------------------------------+
|                       your machine                       |
+----------------------------------------------------------+
        |                      |                   |
+-----------------+ +-------------------+ +----------------+
| "php" container | | "redis" container | | "db" container |
+-----------------+ +-------------------+ +----------------+

PHP redis "localhost", redis. , MySQL "localhost". redis "redis". MySQL "db".

, , - (.. ports ):

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

6379 "redis" , . . "127.0.0.1:6379", php .

Networking in Docker Compose, - , . , , php, MySQL- db.

, redis redis

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

redis redis redis .env Laravel.

REDIS_HOST=redis
#REDIS_PORT=6379
+2

static-ip, localhost

$redis->connect('<static-ip>', 6379);
0

docker-compose.yml

    lnmp-redis:
    image: yahuiwong/lnmp:redis3.2.9
    hostname: lnmp-redis-v3
    ports:
        - "63789:63789"

    $redis = new \Redis();
var_dump($redis);
try {
    $redis->connect('lnmp-redis-v3', 63789);
    var_dump($redis->keys("*"));
} catch (\Exception $e) {
    var_dump($e->getMessage())  ;
}
0

Source: https://habr.com/ru/post/1670369/


All Articles