Docker Redis Connection refused(Docker Redis 连接被拒绝)
问题描述
我正在尝试通过代码访问 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;
}
这篇关于Docker Redis 连接被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Docker Redis 连接被拒绝
基础教程推荐
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01