Docker introduction:
Docker is an open source application container engine that allows developers to package their applications and dependencies into a portable container, then publish them to any popular Linux machine, or virtualize them. Containers use sandboxing mechanism completely and there will be no interface between them.
I have always wanted to implement this way of playing since I came into contact with docker. The following are the steps
1: Create docker-based mysql, refer to the article
Use Docker to execute MySql installed by brew on Mac
2: Create a docker-based php image
In the current directory, create a Dockerfile, the content is as follows
FROM php:7.0-cli MAINTAINER Terry Zhang <zterry@> RUN docker-php-ext-install pdo_mysql mysqli
3. Create a php image
docker build -t php-mysql
4. Write a php script that can read data from the 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. The container for executing php, the parameters are as follows:
bash docker run -it --rm -v (pwd):/var --link my-mysql-server1:mysql php-mysql:latest php /var/
The point that needs to be noted is the --link parameter, where the container named my-mysql-server1 is called, and its host in the php container is mysql. You can verify it by following command:
docker run -it --rm php-mysql ping mysql
The above is the Docker-based PHP call Docker-based Mysql database introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!