前言 我的本地机器是 mac ,使用 vagrant + virtualbox 安装了一个 centos7 的虚拟机,利用 vagrant 的共享目录来实现 centos 和本地机器的代码同步。然后在虚拟机里使用 docker 来运行多个容器跑项目
由于公司业务复杂,不同的项目需要不同版本的 php 才能运行,故记录一下这次搭建开发环境的过程
这次准备要搭建的环境
nginx
php5.3
php7.0
php7.4
准备工作 1. 安装 docker
2. 安装 docker-compose
开始搭建
/var/project
就是本地机器和虚拟机的共享目录
web
是已经存在的项目目录
创建并进入 dev-compose
目录,用来存放容器相关配置
1. 编写 Dockerfile 文件
php7.4 的 dockfile 文件,命名为:Dockfile-php74
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 FROM php:7.4 -fpmLABEL maintainer="zhangjiawang" ENV TIME_ZONE Asia/ShanghaiRUN sed -i "s/archive.ubuntu./mirrors.aliyun./g" /etc/apt/sources.list RUN sed -i "s/deb.debian.org/mirrors.aliyun.com/g" /etc/apt/sources.list RUN sed -i "s/httpredir.debian.org/mirrors.aliyun.com\/debian-security/g" /etc/apt/sources.list RUN apt-get update && apt-get install -y \ libfreetype6-dev \ libjpeg62-turbo-dev \ libpng-dev \ libonig-dev \ wget RUN docker-php-ext-configure gd --with-freetype --with-jpeg RUN docker-php-ext-install -j$(nproc ) gd pdo pdo_mysql sockets RUN pecl install redis \ && docker-php-ext-enable redis RUN rm -rf /tmp/php-* && apt-get clean
php7.0 的 dockfile 文件,命名为:Dockfile-php70
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 FROM php:7.0 -fpmLABEL maintainer="zhangjiawang" ENV TIME_ZONE Asia/ShanghaiRUN sed -i "s/archive.ubuntu./mirrors.aliyun./g" /etc/apt/sources.list RUN sed -i "s/deb.debian.org/mirrors.aliyun.com/g" /etc/apt/sources.list RUN sed -i "s/httpredir.debian.org/mirrors.aliyun.com\/debian-security/g" /etc/apt/sources.list RUN apt-get update && apt-get install -y \ libfreetype6-dev \ libjpeg62-turbo-dev \ libpng-dev \ libonig-dev RUN docker-php-ext-configure gd --with-freetype --with-jpeg RUN docker-php-ext-install -j$(nproc ) gd pdo pdo_mysql sockets RUN rm -rf /tmp/php-* && apt-get clean
2. 编写 docker-compose.yaml 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 version: '3' services: nginx: image: nginx:1.18.0 container_name: nginx-dev ports: - "80:80" volumes: - ../web/:/var/www - ./conf/nginx/conf.d/:/etc/nginx/conf.d/ - ./log/nginx/:/var/log/nginx/ depends_on: - php7.4 - php7.0 - php5.3 php7.4: build: context: . dockerfile: Dockerfile-php74 container_name: php7.4-dev expose: - "9000" volumes: - ../web/:/var/www - ./conf/php7.4/php.ini:/usr/local/etc/php/php.ini php7.0: build: context: . dockerfile: Dockerfile-php70 container_name: php7.0-dev expose: - "9000" volumes: - ../web/:/var/www php5.3: image: magentoomitsis/php5.3 container_name: php5.3-dev expose: - "9000" volumes: - ../web/:/var/www:rw
3. docker-compose 运行服务
运行成功!
查看一下当前运行的容器:
测试 回到 /var/project
目录查看:
conf.d 目录下没有配置文件?
解决办法 :
手动在 conf.d 目录下创建 default.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 server { listen 80; listen [::]:80; server_name localhost; location / { root /var/www/html; index index.php index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /var/www/html; } location ~ \.php$ { fastcgi_pass php7.4-dev:9000; # 可以指定不同的php容器 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name; include fastcgi_params; } }
在 html 目录下创建 index.php
重新启动 nginx 服务
1 2 [root@localhost dev-compose] Restarting nginx-dev ... done
docker-compose
必须在包含 docker-compose.yaml
文件的目录下运行
php.ini 居然是个目录?
删除宿主机的 php.ini 目录,创建 php.ini 文件,然后重新启动 php7.4 服务的话,会报如下的错误:
原因是因为一开始时候在 docker-compose.yaml
文件中指定了 php7.4 服务的数据卷容器,所以在容器中已经生产了php.ini 这个目录,此时如果将 php.ini 目录改为文件的话,会造成冲突:check if the specified host path exists and is the expected type
解决办法 :
删除宿主机的 php.ini 目录,创建 php.ini 文件
停止并删除现在的 php7.4 服务,重新 build
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 [root@localhost dev-compose] Stopping php7.4-dev ... done [root@localhost dev-compose] Going to remove php7.4-dev Are you sure? [yN] y Removing php7.4-dev ... done [root@localhost dev-compose] Building php7.4 Step 1/11 : FROM php:7.4-fpm ---> 5b23307ceb6e Step 2/11 : LABEL maintainer="zhangjiawang" ---> Using cache ---> 67e1d992e905 Step 3/11 : ENV TIME_ZONE Asia/Shanghai ---> Using cache ---> 46a4320dcaf5 Step 4/11 : RUN sed -i "s/archive.ubuntu./mirrors.aliyun./g" /etc/apt/sources.list ---> Using cache ---> 3c2b675f31f2 Step 5/11 : RUN sed -i "s/deb.debian.org/mirrors.aliyun.com/g" /etc/apt/sources.list ---> Using cache ---> 3aebfa80a509 Step 6/11 : RUN sed -i "s/httpredir.debian.org/mirrors.aliyun.com\/debian-security/g" /etc/apt/sources.list ---> Using cache ---> 889b51d01560 Step 7/11 : RUN apt-get update && apt-get install -y libfreetype6-dev libjpeg62-turbo-dev libpng-dev libonig-dev wget ---> Using cache ---> dff4fb71ceb5 Step 8/11 : RUN docker-php-ext-configure gd --with-freetype --with-jpeg ---> Using cache ---> 4e152b3a429c Step 9/11 : RUN docker-php-ext-install -j$(nproc ) gd pdo pdo_mysql sockets ---> Using cache ---> e03869357250 Step 10/11 : RUN pecl install redis && docker-php-ext-enable redis ---> Using cache ---> 2f1b9b18cbd0 Step 11/11 : RUN rm -rf /tmp/php-* && apt-get clean ---> Using cache ---> a20fe520a3a0 Successfully built a20fe520a3a0 Successfully tagged dev-compose_php7.4:latest [root@localhost dev-compose] Creating php7.4-dev ... done [root@localhost dev-compose] root@ea8d8eff8a49:/var/www/html root@ea8d8eff8a49:/usr/local/etc/php total 144 drwxr-xr-x 1 root root 38 Jul 25 10:18 conf.d -rw-r--r-- 1 501 dialout 0 Jul 27 2020 php.ini -rw-r--r--. 1 root root 72278 Jun 11 18:42 php.ini-development -rw-r--r--. 1 root root 72582 Jun 11 18:42 php.ini-production root@ea8d8eff8a49:/usr/local/etc/php