PHP calls based on Docker Docker MySQL database _docker

Source: Internet
Author: User
Tags php script docker run

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 <>
RUN docker-php-ext-install pdo_mysql mysqli

3. Create a PHP image

Docker Build-t Php-mysql

4. Write PHP script, you can read data from MySQL database:

$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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.