Docker Redis Connection refused

Docker Redis Connection refused

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Docker Redis Connection refused

When you see Connection refused while trying to connect a PHP application to a Redis container, it almost always points to a network topology issue rather than a broken Redis installation. In your specific scenario, the ability to run redis-cli directly inside the Redis container confirms that the Redis server process is healthy and listening on port 6379. The failure occurs strictly because the PHP container cannot reach that host.

Looking at your docker-compose.yml, the most immediate red flag is the usage of the links directive combined with hardcoded connection logic in PHP. You are attempting to connect using 127.0.0.1. Inside a containerized environment, 127.0.0.1 refers only to the loopback interface of the specific container where the code is running. It does not act as a wildcard that resolves to other containers on the same network.

In your configuration, the php service is connected to both the frontend and backend networks, while redis is not explicitly listed in a network definition or relies on the implicit default network created by older Docker versions (though your compose file attempts to define networks). However, the critical mismatch here is likely how the application resolves the hostname. If you are connecting via IP, 127.0.0.1 will never resolve to the Redis container's internal IP address.

The primary solution is to stop using IP addresses and rely on Docker's built-in service discovery. When you define a service named redis in your docker-compose.yml, Docker automatically creates a DNS entry with that exact name on the shared networks. To fix this, update your PHP connection logic to use 'redis' as the host instead of '127.0.0.1'.

$redis = new \Redis();
try {
    // Use the service name defined in docker-compose.yml
    $redis->connect('redis', 6379);
} catch (\Exception $e) {
    var_dump($e->getMessage());
    die;
}

This approach assumes that both the php and redis services are attached to the same Docker network. In your current setup, you have defined a backend network but only attached it to the db and php services. The redis service is missing from any explicit network definition. While older versions of Docker allowed implicit networking on the default bridge, modern best practices require all communicating services to be explicitly placed on the same subnet.

Update your docker-compose.yml to ensure redis joins the backend network:

  redis:
    build:
      context: .
      dockerfile: Dockerfile_redis
    ports:
      - "6379:6379"
    networks:
      - backend # Explicitly add this line

With redis and php both on the backend network, they can resolve each other by name. You should also remove the links directive from your services. The links feature is deprecated in Docker Compose v2 and newer versions of Docker Engine because it does not provide network isolation or security benefits; it merely updates the /etc/hosts file, which is unnecessary when using native DNS resolution.

Additionally, consider exposing Redis on a non-standard port or removing the host port mapping (6379:6379) entirely if Redis is strictly an internal service. Mapping port 6379 to the host exposes it to external traffic, which increases the attack surface. If your PHP app needs to connect from outside Docker, you can pass the connection string via environment variables or a configuration file rather than hardcoding the IP.

If you are transitioning from a monolithic setup to a containerized microservices architecture, understanding these networking nuances is critical. For more robust service orchestration and easier scaling of such setups, you might explore managed Redis solutions or advanced Docker networking features discussed on platforms like https://laravelcompany.com. However, for a standard development environment, ensuring all services share a common network namespace and resolving hosts by service name is the industry-standard fix.

Our network