Docker Introduction:
Docker is an open source application container engine that allows developers to package their apps and dependencies into a portable container, and then publish it to any popular Linux machine or virtualization. Containers are completely using the sandbox mechanism, and there will be no interface between each other.
Since the contact with Docker, has been trying to achieve this kind of play. The steps are as follows
1: Establish MySQL based on Docker, reference articles
Mac on a brew-installed MySQL instead of Docker execution
2: Build PHP image based on Docker
In the current directory, set up the Dockerfile, which reads as follows
From Php:7.0-cli
maintainer Terry Zhang <zterry@qq.com>
RUN docker-php-ext-install pdo_mysql mysqli
3. Create a PHP image
4. Write PHP script, you can read data from MySQL database:
<?php
$host = ' mysql ';
$user = ' root ';
$pwd = ' password ';
$db = ' test ';
$mysqli = new Mysqli ($host, $user, $pwd, $db);
if ($mysqli->connect_errno) {
echo "errno:". $mysqli->connect_errno. "\ n";
}
$sql = ' SELECT * from users ';
if ($res = $mysqli->query ($sql)) {while
($row = $res->fetch_assoc ()) {
print_r ($row);
}
}
?>
5. Execute PHP container with the following parameters:
Bash Docker run-it--rm-v (pwd):/var--link my-mysql-server1:mysql php-mysql:latest php/var/mysql.php
The place to note is the--link parameter, which is called the container named My-mysql-server1, whose host in the PHP container is MySQL. You can verify this by using the following command:
Docker run-it--rm php-mysql Ping MySQL
The above is a small set for you to introduce the PHP based on the Docker Docker MySQL database, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!