How can I develop Docker applications using PHP + MySQL?

Source: Internet
Author: User
How can I develop Docker applications using PHP + MySQL?

20141206234218_104.png (170.93 KB, Downloads: 37)

Download attachment

Upload

In program development, only one technology is definitely not enough. many applications need to be developed using multiple technologies. Today, let's talk about how to build a Linux + Apache + MySQL + PHP application using a Docker image based on the LAMP technology stack.

1. create a php Development Application Container

First, select the official PHP image as the basic image of the project.

Dockerfile

From mdaocloud. io/php: 5.6-apache

Next, use the built-in command docker-PHP-ext-install in the official php image to install the MySQL extension dependency of PHP.

Dockerfile

RUNdocker-php-ext-install pdo_mysql

The dependency package is installed through docker-php-ext-install. if the dependency package requires parameter configuration, run the docker-php-ext-configure command.

Install the pdo_mysql PHP extension.

Then, copy the code to the target directory.

Dockerfile

COPY./var/www/html/

Because the exposed port and startup command have been declared in the basic image, this can be omitted.

So far, Docker containers including PHP application development are ready. Parameters required to access the database in PHP code are declared by reading environment variables.

"Php

$ ServerName = env ("MYSQL_PORT_3306_TCP_ADDR", "localhost ");

$ DatabaseName = env ("MYSQL_INSTANCE_NAME", "homestead ");

$ Username = env ("MYSQL_USERNAME", "homestead ");

$ Password = env ("MYSQL_PASSWORD", "secret ");

/**

* Get environment variables

* @ Param $ key

* @ Param null $ default

* @ Returnnull | string

*/

Functionenv ($ key, $ default = null)

{

$ Value = getenv ($ key );

If ($ value = false ){

Return $ default;

Www.maiziedu.com

}

Return $ value;

}

"

Why? Because in the best practices of Docker application development, stateful data services are usually run in another container, container-specific link mechanism is used to dynamically Connect Application containers with data containers.

2. bind a local MySQL data container

First, create a MySQL container.

Bash

Docker run -- name some-mysql-e MYSQL_ROOT_PASSWORD = my-secret-pw-d daocloud. io/mysql: 5.5

Then, the default port (3306) of MySQL can be exposed to the application container through the link mechanism between Docker containers.

Bash

Docker run -- name some-app -- link some-mysql: mysql-d app-that-uses-mysql

3. bind the cloud MySQL data service

Compared with local creation, it is easier to create and bind MySQL data services on the cloud.

A. on GitHub, Fork DaoCloud/php-apache-mysql-sample or add your own code repository.

B. register as a DaoCloud user.

C. on the DaoCloud console, select code build 」.

D. create a new project, select the code source, and start to build the image.

E. create a MySQL service instance in service integration.

F. associate the built application image with the MySQL service instance and deploy it on the cloud.

The above is the specific operation steps for Docker-based applications through PHP + MySQL. if you have any questions, try it yourself.




Recommended Learning: php video tutorial http://www.maiziedu.com/course/php/


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: info-contact@alibabacloud.com 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.