Search code examples
mysqldrupalbitbucket-pipelines

Failed to connect to $DB_HOST port 80 after 0 ms: Couldn't connect to server


I'm writing a bitbucket pipeline to run PHPUnit tests. Here it is:

image: php:8.1

pipelines:
  default:
    - step:
        name: Build and Test
        services:
          - mysql
        caches:
          - composer
        script:
          - apt-get update && apt-get install -y default-mysql-client mariadb-server zip unzip libfreetype6-dev libjpeg62-turbo-dev libpng-dev zlib1g-dev
          - docker-php-ext-configure gd --with-freetype --with-jpeg
          - docker-php-ext-install -j$(nproc) gd exif pdo pdo_mysql
          - mysql -h 127.0.0.1 -u test_user -ptest_user_password -e "CREATE DATABASE IF NOT EXISTS pipelines"
          - export SIMPLETEST_DB='mysql://test_user:test_user_password@127.0.0.1/pipelines'
          - curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
          - composer install
          - vendor/bin/phpunit -c phpunit.xml web/modules/custom/

definitions:
  services:
    mysql:
      image: mysql:5.7
      variables:
        MYSQL_DATABASE: pipelines
        MYSQL_USER: test_user
        MYSQL_PASSWORD: test_user_password
        MYSQL_ROOT_PASSWORD: root_user_password
        MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
        MYSQL_RANDOM_ROOT_PASSWORD: 'yes'

When running the test, I encountered a problem:

GuzzleHttp\Exception\ConnectException: cURL error 7: Failed to connect to $DB_HOST port 80 after 0 ms: Couldn't connect to server (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for http://$DB_HOST

And here are my settings.php configurations related to the database:

$databases['default']['default'] = [
    'database' => 'pipelines',
    'username' => 'test_user',
    'password' => 'test_user_password',
    'host' => '127.0.0.1',
    'port' => '3306',
    'driver' => 'mysql',
    'namespace' => 'Drupal\\mysql\\Driver\\Database\\mysql',
    'prefix' => '',
    'autoload' => 'core/modules/mysql/src/Driver/Database/mysql/',
];

I've tried to change the settings.php many times and I don't even know what else to do. I just need to run the tests and that's it


Solution

  • I just needed to run the application itself, because the localhost was not listening on any port. So I just added the command php -S 0.0.0.0:8080 -t web & and it saved my life.