0

I'm creating a lamp stack docker-compose.

for get the ip of the mysql container on my machine, i execute these command for get it

docker ps --format \
"table {{.ID}}\t{{.Status}}\t{{.Names}}"
docker inspect -f \
'{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'
<the container id>

and when i get the mysql container id, I change my php connection file as this


$servername = "<the container ip>:3306";
$username = "user";
$password = "password";
$dbname = "db";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

Did there are a way for does this automatically for when I deploy it to my server for I doesn't do the change it by myself ?

Thank for your response

Thibautdev
  • 11
  • 1
  • use the container name of mysql as the `$servername`. Also, don't mix it with the port, that is a separate, and optional [arguement](https://www.php.net/manual/en/class.mysqli.php) – danblack May 01 '23 at 11:58
  • [This answer shows an example of a Docker-Compose file where another service can access the database by a hostname (`db`)](https://stackoverflow.com/a/71497968/316310). Compose makes all containers in the same network available under one or more customizable hostnames. Use the `links` property to add aliases. – RickN May 01 '23 at 12:02

0 Answers0