Docker builds its own PHP development environment

Source: Internet
Author: User
Tags fpm php development environment docker run
This article is to introduce how to build a PHP development environment in Docker, will use Zphal-dockerfiles as an example, the need for small partners can refer to the following

1. Preface

1.1 Why use Docker?

Whether there is such a scenario, you have a project, in the local development needs to build the environment, put on the line also need to set up the environment, to the company want to poke to play a bit to build the environment, not a good, because you have a lot of environmental dependence. If there is Docker at this time, just need to install a docker on the machine, put on the written dockerfile, a line of command to do this automatically, convenient and efficient, it is very cool?

1.2 Preparation

Next, this article describes how to build a PHP development environment, will use Zphal-dockerfiles as an example, this is my blog system prepared for a set of dockerfile.

Now, whether it's Windows, Mac or Linux,docker, including Windows systems, Docker for Windows is pretty good in a win 10 system, which is to compare memory.

With the Docker command line, we can do a lot of things, pull mirrors, run containers, execute commands inside the container, and so on, but for now, we're going to write dockerfiles files in a much simpler and more crude way, and then manage those files through Docker-compose, simplifying the process.

What is Dockerfile?

Dockerfile is a script consisting of a series of commands and parameters that apply to the underlying image of a pull and eventually create a new image, through dockerfile we can create an image you need, which contains the software you want to install, which is equivalent to an extension that is ready to be installed in advance. The execution of commands, and then one-click execution, greatly simplifies the operation process.

To build your environment according to this article, you need:

First look at some of the basic operations of Docker and Docker, and what Docker-compose is.
Then you need to install Docker and docker-compose, and I'll use docker-compose to manage my dockerfiles.
Note that writing dockerfile is alive, not dead, and everyone writes out the dockerfile will be different, depending on your needs.

The official Docker documentation is very clear, although it is in English, but basically everything has a problem with the document flipping is very sensible: Docker documentation.

2. Start writing

The next is to take zphal-dockerfiles as an example, the complete can point the link to see, the following is just fragments.

2.1 Preview

First of all, let's take a look at the Dockerfile project I've created, and I've probably broken it down into the following directory (which, of course, is not required to typeset your files):

zphal-dockerfilesapp/index.php phpinfo.phpdata/. gitignorefiles/mysql/conf.d/  mysql-file.cnf Dockerfile nginx/ conf.d/  default.conf  zphal.conf Dockerfile nginx.conf php/pkg/  . Gitignore Dockerfile php.ini Php-dev.ini php-fpm.conf Redis/dockerfile docker-compose.ymllogs/.gitgnorereadme.md

In this project, I used PHP, MySQL, Nginx, Redis and composer, Phalcon expansion and so on.

In general, we have three processes to do this: write the dockerfile of each software, write the configuration file, and process all the dockerfile through Docker-compose, including throwing the configuration profile into the image that the Dockerfile file will build.

2.2 Writing Dockerfile files

2.2.1 PHP

Here's the PHP Dockerfile:

From PHP:7.2-FPM

Maintainer Goozp "gzp@goozp.com"
Setting the time zone

ENV Tz=asia/shanghai

RUN ln-snf/usr/share/zoneinfo/$TZ/etc/localtime && echo $TZ >/etc/timezone

Update installation dependency packages and PHP core extensions

RUN apt-get update && apt-get install-y \git \libfreetype6-dev \libjpeg62-turbo-dev \libpng-dev \&& Docke R-php-ext-configure GD--with-freetype-dir=/usr/include/--with-jpeg-dir=/usr/include/\&& docker-php-ext-install-j$ (Nproc) GD \&& docker-php-ext-install zip \&& docker-php-ext-install pdo_ MySQL \&& docker-php-ext-install opcache \&& docker-php-ext-install mysqli \&& rm-r/var/lib/ apt/lists/*

Copy the pre-downloaded expansion pack from the host

COPY./pkg/redis.tgz/home/redis.tgzcopy./pkg/cphalcon.tar.gz/home/cphalcon.tar.gz

Install the PECL extension, here we are installing Redis

RUN pecl install/home/redis.tgz && echo "extension=redis.so" >/usr/local/etc/php/conf.d/redis.ini

Install third party expansion, here is Phalcon expansion

RUN cd/home \&& tar-zxvf cphalcon.tar.gz \&& mv cphalcon-* phalcon \&& CD phalcon/build \&&A MP;./install \&& echo "extension=phalcon.so" >/usr/local/etc/php/conf.d/phalcon.ini

Installing Composer

ENV Composer_home/root/composerrun Curl-ss Https://getcomposer.org/installer | PHP----install-dir=/usr/local/bin--filename=composerenv PATH $COMPOSER _home/vendor/bin: $PATHRUN rm-f/home/ Redis.tgz \rm-f/home/cphalcon.tar.gz workdir/datawrite permissionrun usermod-u-Www-data

The first line defines the underlying image, where we use the FPM version of Php 7.2, where the second line defines a maintainer.

Then define the time zone, in each dockerfile defined this sentence, mainly to make all the container time and host synchronization, in fact, we can be defined in the Docker-composer.yml file:

Services

PHP-FPM:

Volumes

-/etc/localtime:/etc/localtime:ro
However, in non-Linux systems, such as windows, we can not take the/etc/localtime, in order to be more compatible with all platforms, I write the time synchronously into the dockerfile.

Then install some expansion, in fact, the installation of the expansion process is similar to our bare hands in Linux to install PHP extension, it is worth mentioning that composer. I installed the composer directly in the PHP-FPM image, in fact, the official also provided a composer mirror, pull composer image execution can also achieve the purpose, because we use composer just to execute the composer command to manage our package, If the composer is a separate container, we can also turn the container off when not in use, but here I put the composer directly into the PHP-FPM mirror, mainly my project installed some PHP extensions, In writing the Composer.json file, I defined extension dependencies so that composer execution would check to see if the environment was installed with these dependencies, and if I were to use the composer image directly, I would have to install the extension into the mirror, and it would be a lot more hassle, so I was directly in the PHP image It doesn't make any difference what you do with it, it depends on how you use it.

2.2.2 Nginx

Here is the Nginx dockerfile:

From nginx:1.12
Set Timezome

ENV Tz=asia/shanghai

RUN ln-snf/usr/share/zoneinfo/$TZ/etc/localtime && echo $TZ >/etc/timezone

This is a lot easier, I only set a time. Because I don't need to install anything else, I can use the official image directly.

Of course, we need to modify the configuration file, as long as the pre-written configuration files, and finally in the Docker-compose.yml file, the configuration file thrown in, this will say, including PHP configuration files, MySQL configuration file, are the same.

2.2.3 MySQL

Here's the MySQL Dockerfile:

From mysql:5.7
Set Timezome

ENV Tz=asia/shanghai

RUN ln-snf/usr/share/zoneinfo/$TZ/etc/localtime && echo $TZ >/etc/timezone

MySQL is nothing special, just use the official image.

2.2.4 Redis

The following are Redis's, and also directly use the official image:

From redis:3.2
Set Timezome

ENV Tz=asia/shanghai

RUN ln-snf/usr/share/zoneinfo/$TZ/etc/localtime && echo $TZ >/etc/timezone

2.3 Writing the configuration file

How to handle the configuration file, I classify the configuration file, PHP configuration file in the PHP directory, nginx configuration is placed in the Nginx directory, as to whether to create a new subfolder to see the situation, such as the CONF.D folder.

The following is an example of Nginx configuration file, the first Nginx directory is this:

nginx/

conf.d/

Default.conf

Zphal.conf

Dockerfile

Nginx.conf

In addition to nginx.conf, there is a subfolder conf.d used to store all the domain name configuration files, in Linux under the PHP environment should be more familiar. These are the files that we are going to pass in the container, and we will not use them on the host.

So the most important thing to note is that the path that appears in the configuration file is the path to the environment within the container, not the host's path, and each container has a running environment, which is a tiny small system that is the path within the container. We can synchronize the file by mounting the communication with the container, and we need to mount the file path on the command line to launch the container, and now we'll use Docker-compose to fix it.

The following is an example of a configuration file:

server {Listen default;index index.html index.htm;server_name localhost docker;root/data/www;index index.php Index.html index.htm;location/{try_files $uri $uri//index.html;} Location ~ \.php {include fastcgi_params; Fastcgi_pass php-fpm:9000; Fastcgi_index index.php; Fastcgi_param SCRIPT_FILEN ame/data/www/$fastcgi _script_name;}}

In Root/data/www, the/data/www path is the path of the Nginx container, not the current host's path, so we will mount the Web program to this path.

2.4 Writing Docker-compose.yml

In the PHP, Nginx and other directories of the same class, we create a docker-compose.yml, we execute docker-compose related commands, will automatically find this file, and according to the contents of the inside to execute.

Next to the Nginx example, let's talk about mount, because this is the most important step. In Docker-compose.yml, the Nginx part:

Build:./nginxdepends_on:-php-fpmlinks:-php-fpm:php-fpmvolumes:-. /APP:/DATA/WWW:RW-./nginx/conf.d:/etc/nginx/conf.d:ro-./nginx/nginx.conf:/etc/nginx/nginx.conf:ro-. /logs/nginx:/var/log/nginxports:-"80:80"-"8080:8080"-"443:443" restart:alwayscommand:nginx-g ' daemon off; '

There is a volumes parameter, here is the relevant configuration of the directory we want to mount, the first we will. /app is mounted to/data/www, which is also the default listener root defined in our profile, and the app directory is a directory on our host where we can put our project files directly into the app and Docker will help you transfer them to the/data/in the container. The WWW directory.

Other parameters:

Build defines where your dockerfile is, and if you don't write dockerfile you can use the images parameter to define an official image, such as image:mysql:5.7;
Depends_on said will rely on other mirrors, such as nginx dependency php-fpm, without it I can not play the nginx;
Links define connections, such as connecting to PHP-FPM containers, which are php-fpm:php-fpm, followed by aliases;
The ports represents a port mapping, and 80:80 indicates that 80 ports are mapped to 80 ports on the host;
Restart restart, Restart:always said it will automatically restart;
command is the automatic execution of commands;
......
A lot of parameters, more can refer to official documents.

The following is a complete docker-compose.yml file:

Version: ' 3.2 ' Services:php-fpm:build:./php/ports:-"9000:9000" Links:-Mysql-db:mysql-db-redis-db:redis-dbvolumes: - .. /APP:/DATA/WWW:RW-./php/php-dev.ini:/usr/local/etc/php/php.ini:ro-./php/php-fpm.conf:/usr/local/etc/ Php-fpm.conf:ro-... /logs/php-fpm:/var/log/php-fpm:rwrestart:alwayscommand:php-fpmnginx:build:./nginxdepends_on:-Php-fpmlinks:- Php-fpm:php-fpmvolumes:-.. /APP:/DATA/WWW:RW-./nginx/conf.d:/etc/nginx/conf.d:ro-./nginx/nginx.conf:/etc/nginx/nginx.conf:ro-. /logs/nginx:/var/log/nginxports:-"80:80"-"8080:8080"-"443:443" restart:alwayscommand:nginx-g ' daemon off; ' Mysql-db:build:./mysql ports:-"3306:3306" volumes:-.. /DATA/MYSQL:/VAR/LIB/MYSQL:RW-... /LOGS/MYSQL:/VAR/LIB/MYSQL-LOGS:RW-./mysql/conf.d:/etc/mysql/conf.d:ro environment:mysql_root_password:123456 Mysql_database:zphaldb mysql_user:zphal mysql_password:zphal123 restart:always command: "--character-set-server= UTF8 "Redis-db:build:./redis ports:-" 6379:6379 "volumes:-.. /datA/redis:/data restart:always 

3. Use

How do we use this set of writing down?

3.1 Using a built-in environment

First, enter the project Dockerfiles directory, here is the files directory:

CD Zphal-dockerfiles/files

wget Https://pecl.php.net/get/redis-3.1.6.tgz-O php/pkg/redis.tgz

wget Https://codeload.github.com/phalcon/cphalcon/tar.gz/v3.3.1-O php/pkg/cphalcon.tar.gz
Then download the PHP extension package that we will use.

Execute command:

Docker-compose up
Docker automatically builds images with well-written docker-compose.yml content and launches containers.

If no problem, the next boot can be enabled in the daemon mode, all containers will run in the background:

Docker-compose up-d
To close the container:

You can then close the container and delete the service:

Docker-compose down
Using Docker-compose is basically that simple, using Stop,start and other commands to manipulate the container service. And more work is to write Dockerfile and docker-compose.yml files.

3.2 Using composer

What do we do when we want to use composer? We have installed the composer in the php-fpm.

To operate with Docker-compose:

Docker-compose Run--rm-w/data/www/zphal php-fpm composer Update
-w/data/www/zphal for the work area in PHP-FPM, the Zphal project is also mounted on the inside, all of which we can run directly in the container composer.

Or go to the Host app directory with the Docker command:

CD Zphal-dockerfiles/app

Docker run-it--rm-v ' pwd ':/data/www/-w/data/www/zphal files_php-fpm Composer Update

4. Precautions

Note the mount path.
When the build fails, notice the error in the container.
Accelerates mirroring. If the process download image is slow, you can use the accelerated mirroring service in the country.

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.