2

I'm trying to connect PHP with redis using docker (compose)

docker-compose.yml

version: '2'
services:
  redis:
    image: redis:3.2.2
  php:
    image: company/php:dev7
    volumes:
      - .:/var/www/
    networks:
      - net
    links:
      - redis
  nginx:
    image: company/nginx
    volumes:
      - .:/var/www/
      - ./docker/nginx_conf:/etc/nginx/conf.d/
    networks:
      - net
    ports:
      - 80:80
networks:
  net:
    driver: bridge

This all works well and I'm able to run nginx and php. However when I'm trying to connect with Redit it tells me it cannot get the address info:

Fatal error: Uncaught Predis\Connection\ConnectionException: php_network_getaddresses: getaddrinfo failed: Name or service not known [tcp://redis:6379] in /var/www/htdocs/vendor/predis/predis/src/Connection/AbstractConnection.php on line 155

This is the way I'm trying to connect:

$client = new \Predis\Client([
    'host'   => 'redis',
]);

Also when I look into the redis docker container and look into /etc/hosts there is no redis hostname. At least I was expecting it over here as I'm trying to link it in the docker-compose.yml.

What do I configure wrong?

Paul
  • 79
  • 1
  • 7

1 Answers1

3

You forgot add redis container to dev network.

You can update docker-compose.yml

version: '2'
services:
  redis:
    image: redis:3.2.2
    networks:
      - net
  php:
    image: company/php:dev7
    volumes:
      - .:/var/www/
    networks:
      - net
  nginx:
    image: company/nginx
    volumes:
      - .:/var/www/
      - ./docker/nginx_conf:/etc/nginx/conf.d/
    networks:
      - net
    ports:
      - 80:80
networks:
  net:
    driver: bridge

Also, you can omit container's links if you defined bridge network.

Also 2. You'll never find linked container IP and hostnames in hosts file. Docker use internal DNS service

Bukharov Sergey
  • 9,767
  • 5
  • 39
  • 54