seperate mysql server and apache webserver with docker?

  apache2, docker, mysql, php

I have my apache2 webserver in one docker container. Users can type an email and it will store it in a SQL database. But when i try and run a different docker container specifically for the mysql server, the connection is refused.

Docker container for webserver: docker run -t -d -p 80:80 –name webserver ubuntu

Docker container for mysql server: docker run -t -d -p 3306:3306 –name mysql_server ubuntu

heres my php code to store the data:

<?php

$servername = "1.1.1.1:3306"; //yes i know i need to put a valid IP here. Just put this as an example
$username = "username";
$password = "password";
$dbname = "Emails";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}
$email = $_POST['email'];

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $emailErr = "invalid email format";
}

$sql = "INSERT INTO users (email)
VALUES ('$email')";

if ($conn->query($sql) === TRUE) {
  echo "New record created successfully";
  header("Location: Thankyou.html");
  exit;
} else {
  echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

Source: Docker Questions

LEAVE A COMMENT