GGGforce
GGGforce

Reputation: 653

Can't clear Laravel cache when using multiple Docker containers

I am unable to refresh the Laravel cache and I think the problem could be related to using multiple Docker containers running the Laravel stack. I've tried php artisan cache:clear and php artisan config:cache. I've deleted bootstrap/cache/config.php but the file just reappears with the old config like some undead creature haunting me.

The only way I am able to get the cache to refresh is by completely removing containers with docker-compose down and then running the containers again.

In my docker-compose below you can see that my app, queue and scheduler containers all run the Laravel stack with the codebase mounted from the host. Could this be contributing to the problem?

docker-compose.yml

version: '3'
services:

  #PHP Service
  app:
    build:
      context: .
      dockerfile: Dockerfile
    image: digitalocean.com/php
    container_name: app
    restart: unless-stopped
    tty: true
    env_file: '.env'
    environment:
      SERVICE_NAME: app
      SERVICE_TAGS: dev
      #APP_ENV: local
    working_dir: /var/www
    volumes:
      - ./:/var/www
      - ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
      - ./php/opcache.ini:/usr/local/etc/php/conf.d/opcache.ini
    networks:
      - app-network

  scheduler:
    image: digitalocean.com/php
    container_name: scheduler
    restart: unless-stopped
    depends_on:
      - app
    volumes:
      - .:/var/www
    env_file: '.env'
    environment:
      CONTAINER_ROLE: scheduler
    networks:
      - app-network

  queue:
    image: digitalocean.com/php
    container_name: queue
    restart: unless-stopped
    depends_on:
      - app
    volumes:
      - .:/var/www
      - ./supervisor/supervisord.conf:/etc/supervisor/supervisord.conf
      - ./supervisor/conf.d:/etc/supervisor/conf.d
    env_file: '.env'
    environment:
      CONTAINER_ROLE: queue
    networks:
      - app-network

  #Nginx Service
  webserver:
    image: nginx:alpine
    container_name: webserver
    restart: unless-stopped
    tty: true
    ports:
      - "${local_ip}:${host_web_port}:80"
      #- "${local_ip}:443:443"
    volumes:
      - ./:/var/www
      - ./nginx/conf.d/:/etc/nginx/conf.d/
    networks:
      - app-network

  #MySQL Service
  db:
    image: mysql:5.7.26
    container_name: db
    restart: unless-stopped
    tty: true
    ports:
      - "${local_ip}:${host_db_port}:3306"
    environment:
      MYSQL_DATABASE: dbname
      MYSQL_USER: laravel
      MYSQL_PASSWORD: password
      MYSQL_ROOT_PASSWORD: root_password
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql
    volumes:
      - dbdata:/var/lib/mysql/
      - ./mysql/my.cnf:/etc/mysql/my.cnf
    networks:
      - app-network

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

#Volumes
volumes:
  dbdata:
    driver: local

Can somebody tell me how I can reload the cache without the need to destroy the containers?

For the sake of completeness, I set up the Docker environment using this guide: https://www.digitalocean.com/community/tutorials/how-to-set-up-laravel-nginx-and-mysql-with-docker-compose

I am running the queue and scheduler containers using this guide: https://laravel-news.com/laravel-scheduler-queue-docker . However I've modified the queue to run Supervisor.

start.sh

#!/usr/bin/env bash

set -e

role=${CONTAINER_ROLE:-app}
env=${APP_ENV:-production}

if [ "$env" != "local" ]; then
    echo "Caching configuration..."
    (php artisan config:cache && php artisan view:cache) # && php artisan route:cache - can't cache route closures
fi

if [ "$role" = "app" ]; then

    exec php-fpm

elif [ "$role" = "queue" ]; then

    echo "Running the queue..."
    /usr/bin/supervisord

elif [ "$role" = "scheduler" ]; then

    while [ true ]
    do
      php artisan schedule:run --verbose --no-interaction &
      sleep 60
    done

else
    echo "Could not match the container role \"$role\""
    exit 1
fi

Upvotes: 4

Views: 3507

Answers (1)

GGGforce
GGGforce

Reputation: 653

I think I now understand what is happening to me. My Laravel config options are defined by a .env file which my docker-compose.yml file is also reading to set the container environment variables. These container environment variables cannot be changed at runtime, and always take precedence over changes made to the .env file while the containers are running.

Stopping and starting the container does not change the environment variables. Only by destroying the container and getting it to start again, thereby re-reading the .env file to set environment variables, do my changes take effect.

The proper set up then should be to use .env for Laravel only, and a separate file for container environment variables. In other words, change the env_file: '.env' entry in docker-compose.yml.

Upvotes: 6

Related Questions