Detailed tutorial on building a Docker-based PHP development environment

Source: Internet
Author: User
Tags php development environment
This article describes how to build a Docker-based PHP development environment. Docker is the best choice for virtual machine technology. For more information, see

This article describes how to build a Docker-based PHP development environment. Docker is the best choice for virtual machine technology. For more information, see

Nowadays, many developers use Vagrant to manage their virtual machine development environments. Vagrant is really cool, but it also has many disadvantages (most importantly, it occupies too much resources ). After the emergence of container technology, Docker and more types of Docker technology, it becomes easy to solve this problem.
Disclaimer

Due to the working method of boot2docker, the methods described in this article may not work properly in your environment. If you want to share folders in a non-Linux environment to a Docker container, pay attention to more details. In the future, I will write an article specifically to introduce the actual problems encountered.
How can it be regarded as a good development environment?

First, we need to know what is a good development environment. For me, a good development environment must have the following features:

Docker supports the above features and even more. You can destroy and recreate the container almost instantly, and you only need to recreate the image you are currently using to update the environment.
What is the PHP development environment?

At present, Web applications are complex and PHP development environments require many things. To ensure the simplicity of the environment, various restrictions are required.
This time we use Nginx, PHP5-FPM, MySQL to run the Synmfony project.

Pet and Cattle

Another focus we should discuss is: deploy the development environment in multiple containers or single containers. The two methods have their own advantages:

  • Single containers are easy to distribute and maintain. Because they are independent, everything runs in the same container, which is like a virtual machine. But this also means that when you want to upgrade something (such as the new version of PHP), you need to re-build the entire container.
  • Multiple containers provide better modularization when adding components. Because each container contains a part of the stack: Web, PHP, MySQL, and so on, you can expand each service or add a service separately without recreating everything.
  • Because I am too lazy and need to place something else in my notebook, here we will only introduce the method of a single container.
    Initialize the project

    The first thing to do is to initialize a new Symfony project. The recommended method is to use the create-project command of composer. You can install composer on the workstation, but it is too simple. This time we use Docker.
    I have previously published an article about Docker commands: make docker commands (well, I lied. I wrote it in this article, then I think it would be better to separate it ).

    You can read it anyway. If you do not have the composer command, you can create your own composer alias.

    $ Alias composer = "docker run-I-t-v \ $ PWD:/srv ubermuda/composer"

    Now you can initialize the Symfony project:

    $ Composer create-project symfony/framwork-standard-edition SomeProject

    Handsome! Let's look at some practical work.

    Container

    Building a self-supplied container that runs the standard Symfony project is quite easy, just install the common Nginx, PHP5-FPM and MySQL-Server, then, the pre-prepared Nginx virtual host configuration file will be thrown in, and some configuration files will be copied.

    The source code of this container can be found in the ubermuda/docker-symfony repository on GitHub. Dockerfile is the configuration file used by Docker to build an image. Let's take a look:

    FROM debian: wheezyENV DEBIAN_FRONTEND noninteractiveRUN apt-get update-yRUN apt-get install-y nginx php5-fpm php5-mysqlnd php5-cli mysql-server supervisorRUN sed-e's /; daemonize = yes/daemonize = no/'-I/etc/php5/fpm/php-fpm.confRUN sed-e's/; listen \. owner/listen. owner/'-I/etc/php5/fpm/pool. d/www. confRUN sed-e's/; listen \. group/listen. group/'-I/etc/php5/fpm/pool. d/www. confRUN echo "\ ndaemon off;">/etc/nginx. confADD vhost. conf/etc/nginx/sites-available/defaultADD supervisor. conf/etc/supervisor/conf. d/supervisor. confADD init. sh/init. shEXPOSE 80 3306 VOLUME ["/srv"] WORKDIR/srvCMD ["/usr/bin/supervisord"]

    We started by extending the basic image debian: wheezy and then configuring Nginx and PHP5-FPM through a series of sed commands.

    The Code is as follows:

    RUN sed-e's/; daemonize = yes/daemonize = no/'-I/etc/php5/fpm/php-fpm.conf
    RUN sed-e's/; listen \. owner/listen. owner/'-I/etc/php5/fpm/pool. d/www. conf
    RUN sed-e's/; listen \. group/listen. group/'-I/etc/php5/fpm/pool. d/www. conf
    RUN echo "\ ndaemon off;">/etc/nginx. conf

    Here we have to do two things. First configure the PHP5-FPM and Nginx so that they can run at the front end so that supervisord can track them.
    Then, configure the PHP5-FPM to run Web-Server with the specified user and handle file permissions.

    Next, you need to install a set of configuration files. The first is the Nginx virtual host configuration file vhost. conf:

    Server {listen 80; server_name _; access_log/var/log/nginx/access. log; error_log/var/log/nginx/error. log; root/srv/web; index app_dev.php; location/{try_files $ uri // app_dev.php? $ Query_string;} location ~ [^/] \. Php (/| $) {fastcgi_pass unix:/var/run/php5-fpm.sock; include fastcgi_params ;}}

    Because we do not need a domain name, we set server_name to _ (a bit like the $ _ placeholder variable in perl) and set the root directory (document root) to/svr/web, we will deploy the application under/srv, and the rest is the standard mg1_+ PHP5-FPM configuration.

    Because a container can only run one program at a time, we need supervisord (or any other process manager, but I prefer supervisord ). Fortunately, this process manager will generate all the processes we need! The following is a short supervisord Configuration:

    [Supervisord] nodaemon = true [program: nginx] command =/usr/sbin/nginx [program: php5-fpm] command =/usr/sbin/php5-fpm [program: mysql] command =/usr/bin/mysqld_safe [program: init] command =/init. shautorestart = falseredirect_stderr = trueredirect_stdout =/srv/app/logs/init. log

    What we need to do here is to define all the services and add a special program: init process. It is not an actual service, but an original STARTUP script running method.

    The problem with this startup script is that it usually needs to start some services first. For example, you may need to initialize some database tables, but the premise is that you have to run MySQL first. One possible solution is to start MySQL in the startup script and then initialize the table, then, in order to avoid affecting the supervisord process management, you need to stop MySQL and then start supervisord.

    Such a script looks like the following:

    /Etc/init. d/mysql startapp/console doctrine: schema: update -- force/etc/init. d/mysql stopexec/usr/bin/supervisord

    It looks ugly. Let's use another method to let the supervisor run it and never restart it.
    The actual init. sh script is as follows:

    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.