To demonstrating the issue, I setup a sample laravel app in a dockerfile which uses php built-in server:
FROM composer:latest
WORKDIR /var/www/html
RUN composer create-project laravel/laravel . --no-dev
RUN mv ./.env.example .env
RUN php artisan key:generate
RUN echo "<?php Route::get('test', fn() => dump(getenv('MY_ENV')));" > routes/api.php
CMD php artisan serve --host 0.0.0.0
As you can see i added a route to simply dump the MY_ENV
variable which defined in docker-compose.yml
file:
version: "3.8"
services:
laravel:
build: .
tty: true
ports:
- '8000:8000'
environment:
MY_ENV: 123
And: docker compose up -d
then head over to localhost:8000/api/test
.
As you can see getenv
return false but i expected it to be 123
The interesting part is, When i use laravel tinker the variable is available :
But this variable is not available in the actual app, What would be the problem here?