Hi,
I’m trying to create a simple application using PHP, Apache, and MySQL. The goal is to create a small table in MySQL, then use PHP and Apache to display the contents of that table in the web browser.
I’m running Docker for Desktop (Version 2.0.0.2, Build: 0b030e1) on Windows 10 Pro.
This is what I’ve done so far:
First, I created a network:
docker network create my-net
Build the mydb image using this Dockerfile:
FROM mysql:5
COPY ./createDB/ /docker-entrypoint-initdb.d
docker build -t mydb .
Inside of the createDB folder is a .sql file containing the following SQL statements:
CREATE DATABASE games;
USE games;
CREATE TABLE mygames(
name varchar(255),
genre varchar(255),
rating int);
INSERT INTO mygames (name, genre, rating)
VALUES('Skyrim','RPG',5);
Next, I perform the run
operation to get the database container going:
docker run --detach --name dbcontainer --network my-net --env="MYSQL_ROOT_PASSWORD=password" mydb
Now for the PHP and Apache side.
Build the myphp image using this Dockerfile:
FROM php:7.3-apache
RUN docker-php-ext-install mysqli && docker-php-ext-enable mysqli
COPY ./homepage/ /var/www/html/
docker build -t myphp .
Inside of the homepage folder is this .php file:
<?php
$hostname = "my-net";
$username = "root";
$password = "password";
$link = mysqli_connect($hostname,$username,$password);
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
echo "Success: A proper connection to MySQL was made!" . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;
mysqli_close($link);
?>
I then start the phpcontainer using this run command:
docker run --name phpcontainer -p 8080:80 --network my-net myphp
I navigate to http://localhost:8080 in my browser and encounter this issue:
**Warning** : mysqli_connect(): php_network_getaddresses: getaddrinfo failed: Name or service not known in **/var/www/html/index.php** on line **6**
**Warning** : mysqli_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: Name or service not known in **/var/www/html/index.php** on line **6**
Error: Unable to connect to MySQL. Debugging errno: 2002 Debugging error: php_network_getaddresses: getaddrinfo failed: Name or service not known
Line 6 is referencing:
$link = mysqli_connect($hostname,$username,$password);
Any help would be much appreciated. I’ll be using Docker for school this semester and only started learning it this weekend. I’ve definitely learned a ton in the past few days but I’m a bit stuck!
Thank you!