Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

danvim's avatar

Dockerfile build Laravel trying to connect to Redis service yet to “up”

I am mirroring it from my StackOverflow question: https://stackoverflow.com/questions/59810019/dockerfile-build-laravel-trying-to-connect-to-redis-service-yet-to-up

My project is defined in a docker-compose file, but I'm not too familiar with docker-compose definitions.

When I try to docker-compose up -d in a fresh setup, the following error occurred during the build of a docker image.

This is after composer install, under post-autoload-dump. Laravel tries to auto discover packages (php artisan package:discover).

Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi

   RedisException  : php_network_getaddresses: getaddrinfo failed: Name or service not known

  at [internal]:0
    1|

  Exception trace:

  1   ErrorException::("Redis::connect(): php_network_getaddresses: getaddrinfo failed: Name or service not known")
      /var/www/vendor/laravel/framework/src/Illuminate/Redis/Connectors/PhpRedisConnector.php:126

  2   Redis::connect("my_redis", "6379")
      /var/www/vendor/laravel/framework/src/Illuminate/Redis/Connectors/PhpRedisConnector.php:126

  Please use the argument -v to see more details.
Script @php artisan package:discover --ansi handling the post-autoload-dump event returned with error code 1
ERROR: Service 'my_app' failed to build: The command '/bin/sh -c composer global require hirak/prestissimo && composer install' returned a non-zero code: 1

The reason it cannot connect to my_redis:6379 is because my_redis is another service in the same docker-compose.yml file. So I assume the domain is not ready yet, since docker-compose wants to first build my images before hosting containers.

How can I resolve this problem? Is there a way to force Redis container to up first before building my_app? Or is there a Laravel way to prevent any domain discovery? Or is there a way to specify the building of an image depends on another service to be available?

If you want to see my docker-compose.yml:

version: '3.6'
services:

  # Redis Service
  my_redis:
    image: redis:5.0-alpine
    container_name: my_redis
    restart: unless-stopped
    tty: true
    ports:
      - "6379:6379"
    volumes:
      - ./redis/redis.conf:/usr/local/etc/redis/redis.conf
      - redisdata:/data
    networks:
      - app-network

  # Postgres Service
  my_db:
    image: postgres:12-alpine
    container_name: my_db
    restart: unless-stopped
    tty: true
    ports:
      - "5432:5432"
    environment:
      POSTGRES_DB: my
      POSTGRES_PASSWORD: admin
      SERVICE_TAGS: dev
      SERVICE_NAME: postgres
    volumes:
      - dbdata:/var/lib/postgresql
      - ./postgres/init:/docker-entrypoint-initdb.d
    networks:
      - app-network

  # PHP Service
  my_app:
    build:
      context: .
      dockerfile: Dockerfile
    image: my/php
    container_name: my_app
    restart: unless-stopped
    tty: true
    environment:
      SERVICE_NAME: my_app
      SERVICE_TAGS: dev
    working_dir: /var/www
    volumes:
      - ./:/var/www
      - /tmp:/tmp #For CS Fixer
      - ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
      - fsdata:/my
    networks:
      - app-network

  # Nginx Service
  my_webserver:
    image: nginx:alpine
    container_name: my_webserver
    restart: unless-stopped
    tty: true
    ports:
      - "8080:80"
    volumes:
      - ./:/var/www
      - ./nginx/conf.d/:/etc/nginx/conf.d/
    networks:
      - app-network

# Docker Networks
networks:
  app-network:
    driver: bridge

# Volumes
volumes:
  dbdata:
    driver: local
  redisdata:
    driver: local
  fsdata:
    driver: local
0 likes
7 replies
DigitalMasters's avatar

You have to use the service name as redis host in your .env file.

For example: If your redis container in your docker-compose.ymlis named redis-container, you have to fill in the .env file with

REDIS_HOST=redis-container

If thats already the case, you have to execute composer install within the docker-container with `docker exec -it {container-name} composer install

1 like
phamduchuy552's avatar

@DigitalMasters Thank you. You saved my day. I don't know if that answered OP's question but you saved my day. I followed this guide (here) and when I try to connect from Laravel to Cache driver, which is Redis, it says

"Connection refused [tcp://127.0.0.1:6379]". 

Later did I found out that

REDIS_HOST=127.0.0.1

Which was not gonna work.

danvim's avatar

@digitalmasters It's already that. The point is, when docker-compose tried to build the image, none of the services are up yet, so "my_redis:6379" is not reachable during the Docker build, the container ran the composer install command, which would trigger Laravel's package discovery script.

PHP Redis tried to run during the package discovery, but since nothing was up yet, it couldn't connect to the Redis service, failing the build. I either have to manually turn up a Redis server for it to build properly, or prevent Laravel from running any scripts after composer command, and instead run those "when the container starts".

To simplify, the timeline is this:

  1. No docker container is up
  2. docker-compose up -d. Pulls required images from repository.
  3. docker-compose Issues a build command for my Laravel image my_project/laravel.
    1. composer install inside the image.
    2. Composer completes installation, run post install scripts.
    3. Laravel package discovery script is run.
      1. PHP Redis tries to connect to Redis service at my_redis:6379 as stated in .env. (This fails. Things afterwards are hypothetical situations.)
  4. my_project/laravel is built.
  5. Running containers.
    1. Redis and DB are up.
    2. Laravel is up.
DigitalMasters's avatar

Ah okay then i missunderstood your question.

The only way to solve this, is to use setup script and trigger it after the containers are up and running. If you di this, your containers are running and the setup script will trigger composer installwith all the laravel auto discovery things.

wensonsmith's avatar

I use Laradock, after add

extra_hosts:
        - "dockerhost:${DOCKER_HOST_IP}"

to redis service, it works.

so try add extra_host to redis and php-worker service .

Please or to participate in this conversation.