Docker Redis连接被拒绝
我正在尝试通过代码访问Redis服务器,但未连接。但是,如果我猛击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容器没有任何Redis在 localhost中,因为其中没有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中的网络中所述撰写,每个Docker撰写容器都可以使用服务名称作为主机名来访问其他容器。例如,您通过服务 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
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;
}