Home  >  Article  >  Backend Development  >  How to build a PHP development environment with Docker

How to build a PHP development environment with Docker

小云云
小云云Original
2018-02-26 10:05:333221browse

1.1 Why use Docker?

Is there such a scenario? You have a project and you need to set up an environment when you develop it locally. You also need to set up an environment when you put it online. When you go to the company and want to play it secretly, you need to set up an environment. , it’s not enough if they don’t match, because you have a lot of environmental dependencies. If you have Docker at this time, you only need to install Docker on the machine and put the written Dockerfile. This will be done automatically with one line of commands. It is convenient and efficient. Wouldn't it be great?

1.2 Preparation

Next, this article introduces how to set up a PHP development environment, using zPhal-dockerfiles as an example. This is a set of Dockerfiles I prepared for my blog system.

Now, whether it is Windows, Mac or Linux, Docker can support it very well, including Windows systems. Docker for Windows under Win 10 system is actually quite good, but it consumes more memory.

Through the Docker command line, we can do a lot of things, pull images, run containers, execute commands within the container, etc., but now, we have to use a simpler and more crude way to write the Dockerfiles file, and then use docker -compose manages these files and simplifies the operation process.

What is a Dockerfile?

Dockerfile is a script composed of a series of commands and parameters. These commands are applied to the pulled base image and finally create a new image. Through Dockerfile we can create an image you need, which contains The software you want to install is equivalent to customizing the extensions to be installed, the commands to be executed, etc., and then executing them with one click, greatly simplifying the operation process.

To set up an environment according to this article, you need to:

First understand Docker and some basic operations of Docker, and what docker-compose is.
Then you need to install Docker and docker-compose, I will use docker-compose to manage my Dockerfiles.
Note that writing a Dockerfile is a living thing, not a dead thing. The Dockerfile written by everyone will be different, depending on your needs.

Docker’s official documentation is very clear. Although it is in English, it basically has everything. If you have any questions, it is very wise to refer to the document: Docker Documentation.

2. Start writing

The following will take zPhal-dockerfiles as an example. You can click on the link to view the complete file. The following is just a fragment.

2.1 Preview

First of all, let’s take a look at the Dockerfile project I created. I roughly divided it into the following directories (of course, this is determined by myself, and it is not required to format you this way. File):


zPhal-dockerfiles
app/
 index.php
 phpinfo.php
data/
 .gitignore
files/
 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.yml
logs/
.gitgnore
README.md

In this project, I used PHP, MySQL, Nginx, Redis, Composer, Phalcon extensions, etc.

In general, we have three processes to do this: write the Dockerfile of each software; write the configuration file; process all Dockerfiles through docker-compose, including throwing the configuration configuration file into the Dockerfile file in the image that will be built.

2.2 Writing a Dockerfile

2.2.1 PHP

The following is the Dockerfile for PHP:

FROM php:7.2-fpm

MAINTAINER goozp "gzp@goozp.com"
Set time zone

ENV TZ=Asia/Shanghai

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

Update and install dependency packages and PHP core extensions


RUN apt-get update && apt-get install -y \
git \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng-dev \
&& docker-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 package from the host Go in


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

Install PECL extension, here we install Redis

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

Install third-party extensions, here is the Phalcon extension


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

Install Composer


ENV COMPOSER_HOME /root/composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
ENV PATH $COMPOSER_HOME/vendor/bin:$PATH
RUN rm -f /home/redis.tgz \
rm -f /home/cphalcon.tar.gz 
WORKDIR /data
Write Permission
RUN usermod -u 1000 www-data

The first line defines the base image. Here we use the fpm version of PHP 7.2. The second line here defines a maintainer.

Next, the time zone is defined. This sentence is defined in every Dockerfile. The main purpose is to synchronize the time of all containers with the host. In fact, we can define it in the docker-composer.yml file. :

services:

php-fpm:

volumes:

- /etc/localtime:/etc/localtime:ro
But in When running in non-Linux systems, such as Windows, we cannot get /etc/localtime. In order to be more compatible with all platforms, I wrote the time synchronization into the Dockerfile.

接下来安装一些拓展,其实安装拓展的过程类似于我们徒手在Linux中安装PHP拓展,值得一提的是Composer。我将Composer直接安装在了php-fpm的镜像中,其实官方也提供了Composer的镜像,拉取Composer镜像执行也可以达到目的,因为我们使用Composer只是为了执行Composer命令来管理我们的包,如果Composer单独是一个容器的话,我们在不用时,还可以将容器关掉;但是在这里,我直接将Composer装进php-fpm镜像中,主要是我的项目安装了一些PHP拓展,在编写composer.json文件时,我定义了extension的依赖,这样Composer执行时会检查环境是否安装了这些依赖,所有如果我直接用Composer镜像的话,还需要把我用的拓展安装到镜像里,就麻烦多了,所以我直接在PHP镜像中就把这个事做了,其实没什么区别,取决于你怎么用。

2.2.2 Nginx

下面是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

这个就简单多了,我只设置了一个时间。因为我不需要安装其它的东西,可以直接使用官方的镜像。

当然,我们需要修改配置文件,只要事先写好配置文件就行,最后在 docker-compose.yml 文件中,将配置文件扔进去,这个下面会讲,包括PHP的配置文件,MySQL的配置文件,都是一样的。

2.2.3 MySQL

下面是 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也没有什么特别之处,直接使用官方的镜像。

2.2.4 Redis

下面是 Redis 的,也直接使用官方镜像:

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 编写配置文件

如何处理配置文件呢,我将配置文件进行归类,PHP的配置文件放在PHP目录下,Nginx的配置放在Nginx目录下,至于要不要再新建一个子文件夹就看情况了,比如conf.d文件夹。

下面以Nginx配置文件为例,首先Nginx目录是这样的:

nginx/

conf.d/

    default.conf

    zphal.conf

Dockerfile

nginx.conf

除了nginx.conf外,还有一个子文件夹conf.d用来存放所有的域名配置文件,在Linux下搭建过PHP环境的应该都比较熟悉。这些配置文件就是我们到时候要传进去容器中的文件,我们并不会在宿主机使用这些文件。

所以需要注意的最重要一点就是,配置文件中出现的路径是容器内环境的路径,而不是宿主机的路径,每一个容器内都有一个运行环境,都是一台微型小系统,这些路径都是容器内的路径。我们可以通过挂载与容器内通讯来同步文件,在命令行启动容器也需要挂载文件路径,而现在挂载这一步我们也用docker-compose来解决。

下面是一个配置文件示例:


server {
listen 80 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_FILENAME /data/www/$fastcgi_script_name;
}
}

而root /data/www中,/data/www路径,是到时候Nginx容器的路径,而不是当前在操作的宿主机的路径,所以到时候我们要挂载Web程序放的位置到这个路径。

2.4 编写 docker-compose.yml

在PHP、Nginx等目录的同级,我们创建一个docker-compose.yml,我们在执行docker-compose相关命令时,会自动找到这个文件,并根据里面的内容来执行。

接上面Nginx的例子,我们先谈挂载,因为这是最重要的一步。在docker-compose.yml中,Nginx的部分:


build: ./nginx
depends_on:
 - php-fpm
links:
 - php-fpm:php-fpm
volumes:
 - ../app:/data/www:rw
 - ./nginx/conf.d:/etc/nginx/conf.d:ro
 - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
 - ../logs/nginx:/var/log/nginx
ports:
 - "80:80"
 - "8080:8080"
 - "443:443"
restart: always
command: nginx -g 'daemon off;'

有一个volumes参数,这里就是我们要挂载的目录的相关配置,第一条我们将../app挂载到/data/www之中,也是我们配置文件中定义的默认监听的root,而APP目录是我们宿主机中的一个目录,通过这样挂载我们可以直接将我们的项目文件放到APP中,Docker会帮你传输到容器内的/data/www目录下。

其它的参数:

build定义了你的Dockerfile在哪里,如果没有写Dockerfile可以不用build,可以用images参数定义官方镜像,比如image:mysql:5.7;
depends_on表示将依赖其它镜像,比如Nginx依赖php-fpm,没有它我Nginx没法玩;
links定义连接,比如要连接到php-fpm容器,就是php-fpm:php-fpm,后面是别名;
ports表示端口映射,80:80表示将80端口映射到宿主机的80端口;
restart重启,restart: always表示将自动重启;
command是自动执行的命令;
……
参数很多,更多的可以参考官方文档。

下面是一个完整的 docker-compose.yml 文件:


version: '3.2'
services:
php-fpm:
build: ./php/
ports:
 - "9000:9000"
links:
 - mysql-db:mysql-db
 - redis-db:redis-db
volumes:
 - ../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:rw
restart: always
command: php-fpm

nginx:
build: ./nginx
depends_on:
 - php-fpm
links:
 - php-fpm:php-fpm
volumes:
 - ../app:/data/www:rw
 - ./nginx/conf.d:/etc/nginx/conf.d:ro
 - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
 - ../logs/nginx:/var/log/nginx
ports:
 - "80:80"
 - "8080:8080"
 - "443:443"
restart: always
command: 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. 使用

这一套编写下来,我们怎么用呢?

3.1 使用搭建好的环境

首先,进入项目Dockerfiles的目录下,这里是files目录:

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
然后下载我们会用到的PHP拓展包。

执行命令:

docker-compose up
Docker会自动通过编写好的docker-compose.yml内容构建镜像,并且启动容器。

如果没问题,下次启动时可以以守护模式启用,所有容器将后台运行:

docker-compose up -d
关闭容器:

可以这样关闭容器并删除服务:

docker-compose down
使用 docker-compose 基本上就这么简单,用stop,start等这些命令来操纵容器服务。而更多的工作是在于编写Dockerfile和docker-compose.yml文件。

3.2 使用Composer

当我们要使用Composer时怎么做呢? 我们已经在php-fpm里安装了Composer。

用docker-compose进行操作:

docker-compose run --rm -w /data/www/zPhal php-fpm composer update
-w /data/www/zPhal为在php-fpm的工作区域,zPhal项目也是挂载在里面,所有我们可以直接在容器里运行Composer。

或者进入宿主机APP目录下用Docker命令:

cd zPhal-dockerfiles/app

docker run -it --rm -v `pwd`:/data/www/ -w /data/www/zPhal files_php-fpm composer update

4. 注意事项

注意挂载路径。
构建失败时,注意容器内是否报错。
加速镜像。如果过程下载镜像很慢,可以使用国内的加速镜像服务。

相关推荐:

php开发环境独立安装(二):PHP与Apache整合图文教程

The above is the detailed content of How to build a PHP development environment with Docker. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn