Design for deploying PHP applications using Docker

Source: Internet
Author: User
Tags docker run
1. Docker

The official definition of Docker is:

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

--Https://www.docker.com/whatisdocker

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

Customer: The installation is good, can not use.

Publisher: No problem on my machine.

How to address the dependencies of each application is a headache before Docker, and now it works flawlessly on any Linux with only one configuration, dockerfile or image as the final delivery. It's easy to say, but there are a lot of things to think about in the Docker configuration process: How do you plan to apply individual components? A container solve the problem or refine container? What is the communication between container? Wait a minute.. Here are some of the most common Web application configuration deployments to illustrate these issues.

Note: This article assumes that the reader has a basic understanding of some of the concepts in Docker, and if you do not know it, I recommend this article:

Https://linux.cn/article-6074-weibo.html

2. Lnmp

Typical PHP application configuration scheme is lamp or LNMP, this article takes Lnmp as an example.

Design scenarios such as (I've implemented and run a successful case):

The application consists of 4 components, NGINX,PHP-FPM (PHP), MySQL, and www,4 components running in separate containers created by the respective mirrors. The WWW container is just a container for storing business code and static resources, which can be considered "dead".

The fact that the LNMP architecture should be designed in the way above should be the easiest to think of and the clearest, with each component having relative independence. In addition to the WWW container, the other 3 containers can be built directly through the official image.

However, many students on the Internet do not do so, will not be so fine, usually put nginx and www into a container, or simply put all in a container. We can learn the Dockerfile:

Https://github.com/search?utf8=?&q=docker-lnmp

The advantages and disadvantages of this design refinement container:

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

Although the coupling is relatively large, but this port coupling, the file system coupling relationship can be solved by adding several operating options, followed by the introduction.

Because of the partitioning of the container to the entire architecture, the content in the container becomes very independent and secure. For example, I would like to update the code on the WWW, only need to enter the WWW container to make changes, will not affect the NGINX,PHP-FPM or MySQL.

Each container can be replaced flexibly, for example I want to convert MySQL to MongoDB, or simply move the business code home, will not affect other containers (just change the relevant configuration file)

Because each container is created by an official image, you can use the latest official image at the lowest cost at any time.

The space will be larger, a simple application to do so, four images will occupy a lot of storage space.

2.1 Inter-container communication issues

The refinement of container faces another problem, namely how to communicate between containers. The following is a brief description of the data flow in:

The client's HTTP request reaches the server's 80 port, which is mapped to the 80 port of Nginx container, so it enters nginx processing. Nginx will parse the request resource, determine whether it is a static resource or PHP script, if it is a static resource, it is sent back to the client directly from WWW, if it is a script, tell PHP-FPM to www to get the corresponding script, and then through php-cgi processing.

fast-cgi executes scripts through PHP and accesses MySQL access data as necessary.

So the coupling relationship comes out:

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

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

2.2 Problem Solving

You can see that there are two types of coupling relationships: ports and file systems.

For port coupling, Docker is addressed through the--link option, and Docker is resolved with the--volumes-from option for file system coupling.

Resolve the first coupling relationship:

$ sudo docker run-p 80:80-p 443:443  # Host port mapped to container--volume-from www_container_name  # Mount the folder volume the WWW container to the container that will be started--link Php_fpm_container_name:fpmservice  # Before the colon is the name of the FPM container that is running, followed by an alias, The alias will be visible as hostname in the container that will be launched.-D  # detachnginx_image  # Mirror Name

Resolve the second coupling relationship:

$ sudo docker run--volume-from www_container_name--link mysql_container_name:mysql-dphp_fpm_image

Reference Document: https://docs.docker.com/reference/run/

So the order in which the containers are started comes out:

MySQL Container

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

PHP-FPM Container

Nginx Container

1 and 2 of them can be swapped.

3. Summary

The use of Docker to deploy Web applications can bring a lot of convenience, the implementation of the macro application of components, to achieve the basis of distributed systems.

You can see that it is convenient to actually share data between Docker containers, and it is not difficult to figure out the dependencies of each container.

P.S. This is the experience I learned after two days of Docker, the flaws are unavoidable, if there are errors, please treatise.

  • 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.