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

614
Visualizações
How connect a php script on docker container with network host, with a mysql docker container also on network host

I have:

  • apache+php docker container (net=host)
  • mongo docker container (net=host)
  • mysql docker container (net=host)

In the first container, A php script is dumping a database from mysql and migrating processed data to mongodb. It write to mongodb, but a mysqldump command called from the script is not working. This is the mysqldump command

mysqldump -h 127.0.0.1 -u xxxxx -pxxxxxx 

Got below error

 mysqldump: Got error: 2003: Can't connect to MySQL server on
 '127.0.0.1' (111) when trying to connect

Note that this script is working on production without docker. I tried to add:

--protocol=TCP
--port=3307

Also tried to change 127.0.0.1 to mysql ( container name ) but maybe it dont works with (net=host), but it still doesn't work.

If i do mysql -h 127.0.0.1 -u xxxx -pxxxx from the php container it works, but no from php script.

Commands i use to launch containers:

docker run --restart=unless-stopped --net=host --name mysql   -v /opt/mysql/mysql_config:/etc/mysql/conf.d -v /opt/mysql/mysql_data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=xxxxx -d mysql:5.6

docker run --restart=unless-stopped --net=host --name apachephp -d apachephp 
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The first option is better to use docker-compose networking. But providing a solution base on your current problem.

Using the Host network, the container does not get IP and linking does not work.

so the quick way to fix it can be remove --net host and add linking or you can create docker network as well.

You can publish port if you want to connect from host.

docker run --restart=unless-stopped  --name mysql   -v /opt/mysql/mysql_config:/etc/mysql/conf.d -v /opt/mysql/mysql_data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=xxxxx -d mysql:5.6

docker run --restart=unless-stopped --link mysql --name apachephp -d apachephp 

Now you will be able to access MySQL using below command from PHP container.

mysql -h mysql -uroot -ppassword
over 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