Design scheme for Deploying PHP applications using Docker

Source: Internet
Author: User
Design scheme for Deploying PHP applications using Docker 1. Docker

The official definition of Docker is:

Docker allows you to package an application with all of its dependencies into a standardized unit for software development.


There is no doubt that Docker solves a huge problem in application deployment:

Customer: The installation is complete and cannot be used.

Publisher: No problem on my machine.

How to solve the dependency of each application is a headache before the emergence of Docker. now, with only one configuration, Dockerfile or image as the final delivery, it can run perfectly on any Linux. It is easy to say, but there are many questions worth thinking about in the Docker configuration process: how to arrange the various components of the application? Does one Container solve the problem or refine the Container? What is the communication between containers? And so on .. The following describes these problems with the most common WEB application configuration and deployment.

NOTE: This article assumes that the reader has a basic understanding of some concepts in Docker. if you do not know much about them, I recommend this article:



The typical PHP application configuration scheme is LAMP or LNMP. This article uses LNMP as an example.

For example ):

An application consists of four components: Nginx, PHP-FPM (PHP), MySQL, and WWW. The four components run in an independent container created by the respective image. Www iner is only a Container that stores business code and static resources. it can be regarded as "dead.

In fact, the LNMP architecture adopts the above design method, which is the easiest and clearest. each component has relative independence. In addition to the WWW container, all the other three containers can be directly built using official images.

However, this is not the case on the internet. it is not so detailed. Nginx and WWW are usually put in a container, or they are all put in a container. You can learn about Dockerfile:

Https:// Utf8 =? & Q = docker-lnmp

Refine the advantages and disadvantages of the Container design:

The coupling between containers increases. We can see the coupling between the PHP-FPM container and the other three containers, MySQL container is the most independent.

Although the coupling is large, the file system coupling relationship can be solved by adding several running options, which will be described later.

As containers divide the entire architecture, the content in containers becomes very independent and secure. For example, I want to update the code in WWW online, just need to enter the WWW container for modification, does not affect Nginx, PHP-FPM or MySQL.

Containers can be flexibly disassembled and replaced. for example, if I want to replace MySQL with Mongodb, or simply move the business code to a home, it will not affect other containers (only change the relevant configuration file)

Since each container is created through an official image, you can use the latest official Image at minimal cost at any time.

The occupied space is relatively large. if a simple application needs to do so, the four images will occupy a large amount of storage space.

2.1 Inter-container communication problems

Refining the Container involves another problem: how to implement inter-Container communication. The following describes the data process:

The http request of the client reaches port 80 of the server. The port is mapped to port 80 of the Nginx Container, so Nginx is processed. Nginx analyzes the requested resource and determines whether it is a static resource or a php script. if it is a static resource, it will directly retrieve it from WWW and send it back to the client. if it is a script program, you need to tell the PHP-FPM to get the corresponding script at WWW, and then process it through php-cgi.

Fast-cgi uses php to execute scripts and access MySQL to access data if necessary.

The coupling relationship is as follows:

Nginx needs to connect to the PHP-FPM open port 9000, need to access the file system in WWW.

The PHP-FPM also needs to access the file system in WWW and Port 3306 of MySQL.

2.2 solve the problem

There are two types of coupling relationships: Port and file system.

For port coupling, docker uses the -- link option. for file system coupling, docker uses the -- volumes-from option.

Solve the first coupling relationship:

$ Sudo docker run-p 80: 80-p 443: 443 # host port ING to container -- volume-from WWW_CONTAINER_NAME # mount the folder of the WWW container VOLUME to the started container -- link PHP_FPM_CONTAINER_NAME: fpmservice # before the colon is the name of the running FPM container, followed by an alias. the alias will be visible as hostname in the started container-d # detachNGINX_IMAGE # Image name

Solve the second coupling relationship:

$ sudo docker run --volume-from WWW_CONTAINER_NAME--link MYSQL_CONTAINER_NAME:mysql-dPHP_FPM_IMAGE


The order of container startup is as follows:

MySQL Container

WWW Container (because there is no service running, the Container will exit immediately after run. you can use block commands such as tail-f to keep the Container running without exiting)

The PHP-FPM Container.

Nginx Container

Where 1 and 2 can be switched.

3. Summary

Using Docker to deploy Web applications can bring a lot of convenience. the macro implementation of application componentization lays the foundation for the implementation of distributed systems.

We can see that it is very convenient to share data between Docker Containers. it is not difficult to figure out the dependencies between containers.

P.s. This article is my understanding of docker two days later. it is inevitable that there are leaks. please make an axe if you have any mistakes.

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.