Predis无法通过PHP连接

Predis无法通过PHP连接

本文介绍了Docker-compose Predis无法通过PHP连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用docker(撰写)将PHP与Redis连接起来

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

这一切都很好,我能够运行nginx和php。但是,当我尝试与Redit连接时,它告诉我它无法获取地址信息:

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:

这是我尝试连接的方式:

This is the way I'm trying to connect:

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

另外,当我查看redis docker容器并查看 / etc / hosts 没有redis主机名。至少在我试图将其链接到docker-compose.yml中的时候,我一直在期待它。

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.

我配置错了吗?

推荐答案

您忘记将Redis容器添加到 dev 网络中。

You forgot add redis container to dev network.

您可以更新docker-compose.yml

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

此外,如果定义了 bridge 网络,则可以省略容器的链接

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

也2.您将永远找不到链接的容器主机文件中的IP和主机名。 Docker使用内部DNS服务

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

这篇关于Docker-compose Predis无法通过PHP连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 03:15