Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

579
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda