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

oleynikd's avatar

Testing environment is not loaded inside a docker container

Hello,

I have this code inside my Tests\CreatesApplication trait:

public function createApplication()
{
    $app = require __DIR__.'/../bootstrap/app.php';

    $app->make(Kernel::class)->bootstrap();

    dd(config('app.env'));

    return $app;
}

When I try to run php artisan test from my host machine, everything works fine and I get:

...
"testing"

But when I run it inside a Docker container (docker exec -it php_1 php artisan test) I always get:

...
"local"

Seems like testing environment is not properly loaded if you are trying to run a test inside a container.

I have created a sample repo with fresh Laravel installation and a docker-compose.yml file to run a simple container with a php artisan test

https://github.com/oleynikd/laravel-docker-test-env-issue

This leads to an unexpected behavior, especially considering Illuminate\Foundation\Testing\RefreshDatabase trait that will just erase all of your production database if someone will run a php artisan test on a production server...

Is this an issue or am I just doing something wrong? Thanks.

0 likes
1 reply
AnnaD's avatar

OK, until I changed a phpnit.xml's <php> section to this:

<php>
    <env name="APP_ENV" value="testing" force="true"/>
    <env name="BCRYPT_ROUNDS" value="4" force="true"/>
    <env name="CACHE_DRIVER" value="array" force="true"/>
    <env name="DB_CONNECTION" value="sqlite" force="true"/>
    <env name="DB_DATABASE" value=":memory:" force="true"/>
    <env name="MAIL_MAILER" value="array" force="true"/>
    <env name="QUEUE_CONNECTION" value="sync" force="true"/>
    <env name="SESSION_DRIVER" value="array" force="true"/>
    <env name="TELESCOPE_ENABLED" value="false" force="true"/>
</php>

Nothing worked inside a container. Can anyone explain why there are server instead of env tags in default phpunit.xml ? Also please note the force attribute.

Thanks to https://stackoverflow.com/a/53832084/983914

Please or to participate in this conversation.