Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 8 is not reading the env values passed by docker-compose

Hope you are well,

I have a project that I recently upgraded to laravel 8 from laravel 6.

it was dockerized both for dev and prod, after update unfortunately the app does not retrieve the env values that are passed by the docker-compose file and only works if I pass a .env file to the container.

this is while, inside the container I can still do: php artisan tinker >> print_r(env("DB_HOST")) && print_r($_ENV)and see the env values even without passing the file itself

so the compose file passes the env values to the container but the laravel app does not pick them up

has anyone else faced this issue with laravel 8?

Thank you in advance,

like image 209
Mehdi Amenein Avatar asked Sep 06 '25 03:09

Mehdi Amenein


1 Answers

This could be a caching issue. Please also make sure to access all env variables through config() calls and not to env() directly - that won't resolve if you're caching config.

This is the entrypoint script I'm using for production laravel images:

#!/bin/sh

artisan="/usr/local/bin/php /var/www/artisan"
$artisan down
$artisan cache:clear

$artisan config:clear
$artisan config:cache

$artisan event:clear
$artisan event:cache

$artisan route:clear

$artisan view:clear

$artisan up

apache2-foreground

This clears all caches and caches them again when starting the container to ensure all values are up to date after a restart.

like image 108
kolaente Avatar answered Sep 07 '25 20:09

kolaente