Your problem
Docker Compose creates a separate docker container for different services. Each container, logically, as well as different separate computer servers that are only connected to each other through the docker network.
, , :
+----------------------------------------------------------+
| your machine |
+----------------------------------------------------------+
| | |
+-----------------+ +-------------------+ +----------------+
| "php" container | | "redis" container | | "db" container |
+-----------------+ +-------------------+ +----------------+
PHP redis "localhost", redis. , MySQL "localhost". redis "redis". MySQL "db".
, , - (.. ports ):
redis:
build:
context: .
dockerfile: Dockerfile_redis
ports:
- "6379:6379"
6379 "redis" , . . "127.0.0.1:6379", php .
Networking in Docker Compose, - , . , , php, MySQL- db.
, redis redis
$redis = new \Redis();
try {
$redis->connect('redis', 6379);
} catch (\Exception $e) {
var_dump($e->getMessage()) ;
die;
}