Docker Redis Connection 被拒绝
2022-08-30 18:05:28
我正在尝试通过代码访问Redis服务器,但它没有连接。但是,如果我抨击到redis容器,我可以访问redis-cli。
docker-compose.yml 看起来像这样
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
当我尝试使用此代码连接到redis服务器时
$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容器?