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

cerberuspup's avatar

Having issue running a test on docker

I'm using docker for my laravel and I'm trying to run php artisan test , but when I do I get this error

could not translate host name "pgsql" to address: Temporary failure in name resolution (SQL: select tablename from pg_catalog.pg_tables where schemaname in ('public'))

this is what I have in my .env

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:1SVdFc4F7c8IoufSvE/qtpjct7LOX5ewJGB/wBD3r1w=
APP_DEBUG=true
APP_URL=http://localhost

DB_CONNECTION=pgsql
DB_HOST=pgsql
DB_PORT=5432
DB_DATABASE=laravelDB
DB_USERNAME=laravelDB
DB_PASSWORD=password

TEST_DB_HOST=pgsql
TEST_DB_DATABASE=laravelDB
TEST_DB_USERNAME=laravelDB
TEST_DB_PASSWORD=password

BROADCAST_DRIVER=pusher
CACHE_DRIVER=redis
QUEUE_CONNECTION=redis
SESSION_DRIVER=file
SESSION_LIFETIME=120

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=
MAIL_PASSWORD=
MAIL_ENCRYPTION=tls

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=eu

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
MIX_CUSTOMER_NAME="${CUSTOMER_NAME}"

my phpunit.xml

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
         bootstrap="vendor/autoload.php"
         colors="true"
>
    <testsuites>
        <testsuite name="Business">
            <directory suffix="Test.php">./business/Reporting</directory>
        </testsuite>
        <testsuite name="Unit">
            <directory suffix="Test.php">./Modules/**/Tests/Unit</directory>
            <directory suffix="Test.php">./tests/Unit</directory>
        </testsuite>
        <testsuite name="Feature">
            <directory suffix="Test.php">./Modules/**/Tests/Feature</directory>
            <directory suffix="Test.php">./tests/Feature</directory>
        </testsuite>
    </testsuites>

    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">./app</directory>
        </whitelist>
    </filter>
    <php>
        <server name="APP_ENV" value="testing"/>
        <server name="BCRYPT_ROUNDS" value="4"/>
        <server name="CACHE_DRIVER" value="redis"/>
        <server name="DB_CONNECTION" value="pgsql"/>
        <server name="MAIL_MAILER" value="array"/>
        <server name="QUEUE_CONNECTION" value="sync"/>
        <server name="SESSION_DRIVER" value="array"/>
        <server name="TELESCOPE_ENABLED" value="false"/>
        <server name="DB_HOST" value="pgsql"/>
    </php>
</phpunit>
0 likes
16 replies
Sinnbeck's avatar

Are you running the command from inside the container? If not, then your need to.

Sinnbeck's avatar

And how did you enter the docker container? Which command?

cerberuspup's avatar

@Sinnbeck sure here it is

<?php

use Illuminate\Support\Str;

return [
    'default' => env('DB_CONNECTION', 'pgsql'),
    'connections' => [
        'sqlite' => [
            'driver' => 'sqlite',
            'url' => env('DATABASE_URL'),
            'database' => env('DB_DATABASE', database_path('database.sqlite')),
            'prefix' => '',
            'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
        ],
        'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],
        'pgsql' => [
            'driver' => 'pgsql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '5432'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
            'prefix_indexes' => true,
            'schema' => 'public',
            'sslmode' => 'prefer',
        ],
        'sqlsrv' => [
            'driver' => 'sqlsrv',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '1433'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
            'prefix_indexes' => true,
        ],
        'testing' => [
            'driver' => 'pgsql',
            'url' => env('DATABASE_URL'),
            'host' => '127.0.0.1',
            'port' => '5432',
            'database' => env('TEST_DB_DATABASE',''),
            'username' => env('TEST_DB_USERNAME', ''),
            'password' => env('TEST_DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
            'prefix_indexes' => true,
            'schema' => 'public',
            'sslmode' => 'prefer',
        ],
    ],
    'migrations' => 'migrations',
    'redis' => [
        'client' => env('REDIS_CLIENT', 'phpredis'),
        'options' => [
            'cluster' => env('REDIS_CLUSTER', 'redis'),
            'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_database_'),
        ],
        'default' => [
            'url' => env('REDIS_URL'),
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', '6379'),
            'database' => env('REDIS_DB', '0'),
        ],
        'cache' => [
            'url' => env('REDIS_URL'),
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', '6379'),
            'database' => env('REDIS_CACHE_DB', '1'),
        ],
    ],
];

Sinnbeck's avatar

@cerberuspup Looks fine, except you might not want to use the same database for testing as you use for web use :)

How do you enter the container? What command?

cerberuspup's avatar

@Sinnbeck I'm on windows, so i use the docker app, then I click on the cli button and I then type bash and run the test command

Sinnbeck's avatar

@cerberuspup And you are sure that drops you inside the php container and not just into a shell on your own machine? Run this and check if there is a file called .dockerenv

ls -la /
Sinnbeck's avatar

@cerberuspup I currenly have no clue. That should work (I have several sites with more or less the same config that works). Perhaps show your docker-compose.yml file?

Sinnbeck's avatar

@cerberuspup Ah strange. It sounded like it had the correct hostname cached.. Either way, dont cache on dev :) Cache is for production

siangboon's avatar

i think it was more to this when getting this "Temporary failure in name resolution" error... could try using ip address..

DB_HOST=pgsql

Please or to participate in this conversation.