Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

578
Views
MYSQLI Connection refused when connecting to another docker container running MariaDB

I'm starting to get my hands dirty with Docker and I'm trying to get my PHP application running in a docker container with nginx to connect to the database running in another container running MariaDB.

When I run the containers, I can connect without any problems to the database from my computer (using Sequel Pro) but when i try to connect to the database from the PHP application running in the nginx container I get the following mysqli error:

Warning: mysqli_connect(): (HY000/2002): Connection refused in >/app/web/php/db-config.php on line 7 Failed to connect to the database, died with error:

The db-config.php where the error happens is as follows:

    define('DB_HOST', '0.0.0.0:3306');
    define('DB_NAME', 'Jumpooling');
    define('DB_USER', 'root');
    define('DB_PASSWORD', 'root');

    $con=mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die('Failed to connect to the database, died with error:');

All the docker-compose.yml file content is in this repository.

What am I missing?

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

I managed to find the solution in this answer.

The problem was that the host was not to be defined as an IP address but with the link name that it is given in the docker-compose.yml file, that is db.

The final db-config.php is, thus:

define('DB_HOST', 'db');
define('DB_NAME', 'Jumpooling');
define('DB_USER', 'root');
define('DB_PASSWORD', 'root');

$con=mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME, 3306) or die('Failed to connect to the database, died with error:');
about 4 years ago · Santiago Trujillo Report

0

Links are not required to enable services to communicate - by default, any service can reach any other service at that service’s name.

Containers for the linked service will be reachable at a hostname identical to the alias, or the service name if no alias was specified.

about 4 years ago · Santiago Trujillo Report

0

You should specify the ports container/browser explicitly as -p 3306:3306:

docker run --name mysql-container -p 3306:3306 -e MYSQL_ROOT_PASSWORD=secret -d mysql:latest
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!