docker多版本php的搭建_cangqiong_xiamen的博客-CSDN博客_docker 多版本php
CentOS 7中Docker搭建LNMP环境的方法_CentOS_资源库
sudo docker pull mariadb:10.3
sudo docker pull php:7.2-fpm
sudo docker pull nginx:1.14
sudo docker pull php:8.0-fpm
运行 mariadb10.3 容器的 shell 脚本? (特别要注意的是,docker command line is order sensitive! docker 命令的选项不能随意交换次序,参考 Docker ERROR: mysqld failed while attempting to check config_虚心前行-CSDN博客),docker 容器运行出错,可以 sudo? docker logs? <container name> 查看日志
#!/bin/bash
echo docker 准备运行 mariadb10.3 容器
echo -e "目录映射 host \e[1;34m/home/sjg/docker_data/mariadb10.3\e[0m -> container \e[1;35m/data\e[0m"
echo -e "端口映射 host \e[1;34m3306\e[0m -> container \e[1;35m3306\e[0m"
echo -e "内部 root 密码 \e[1;31mjsjxjf\e[0m"
already_started=`sudo docker ps | grep 'mariadb10.3'`
if [ ! -z "$already_started" ]; then
echo mariadb10.3 already started
else
has_container=`sudo docker ps -a | grep 'mariadb10.3'`
if [ ! -z "$has_container" ]; then
echo find image mariadb10.3, try to start it
sudo docker start mariadb10.3
else
echo no image mariadb10.3, try to create and run
sudo docker run -d -v /home/sjg/docker_data/mariadb10.3:/data -p 3306:3306 -e MYSQL_ROOT_PASSWORD=jsjxjf --name mariadb10.3 mariadb:10.3
fi
started=`sudo docker ps | grep 'mariadb10.3'`
if [ -z "$started" ]; then
echo FAILED to start mariadb10.3
exit
fi
fi
if [ $# -eq 1 -a "$1" = "bash" ]; then
echo 运行容器内部伪终端
sudo docker exec -ti mariadb10.3 /bin/bash
fi
运行效果如下 (可以在里面运行 mysql 和 mysqldump 命令) (已经改了,默认不进入虚拟终端,要进入虚拟终端,加参数 bash,如 ./start_mariadb.sh? bash)
停止该容器的脚本
#!/bin/bash
started=`sudo docker ps | grep 'mariadb10.3'`
if [ ! -z "$started" ]; then
sudo docker stop mariadb10.3
fi
check=`sudo docker ps | grep 'mariadb10.3'`
if [ -z "$check" ]; then
echo mariadb10.3 stopped!
else
echo FAILED to stop mariadb10.3
fi
类似处理 php-7.2fpm,启动和关闭脚本 (容器联系? ?外部容器名:内部容器名,内部容器名mariadb是php-7.2fpm容器引用mariadb10.3时使用的名字,当然,也可以直接用IP来引用)
#!/bin/bash
mariadb_started=`sudo docker ps | grep 'mariadb10.3'`
if [ -z "$mariadb_started" ]; then
echo container mariadb10.3 must be started first! exit
exit
fi
cname='php7.2fpm'
echo docker 准备运行 $cname 容器
echo -e "目录映射 host \e[1;34m/home/sjg/docker_data/gxlq_pig\e[0m -> container \e[1;35m/gxlq_pig\e[0m"
echo -e "端口映射 host \e[1;34m9000\e[0m -> container \e[1;35m9000\e[0m"
echo -e "容器联系 \e[1;31mmariadb10.3:mariadb\e[0m"
already_started=`sudo docker ps | grep $cname`
if [ ! -z "$already_started" ]; then
echo $cname already started
else
has_container=`sudo docker ps -a | grep $cname`
if [ ! -z "$has_container" ]; then
echo find container $cname, try to start it
sudo docker start $cname
else
echo no container $cname, try to create and run
sudo docker run -d -v /home/sjg/docker_data/gxlq_pig:/gxlq_pig -p 9000:9000 --link mariadb10.3:mariadb --name $cname php:7.2-fpm
fi
started=`sudo docker ps | grep $cname`
if [ -z "$started" ]; then
echo FAILED to start $cname
exit
fi
fi
if [ $# -eq 1 -a "$1" = "bash" ]; then
echo 运行容器内部伪终端
sudo docker exec -ti $cname /bin/bash
fi
#!/bin/bash
cname='php7.2fpm'
started=`sudo docker ps | grep $cname`
if [ ! -z "$started" ]; then
sudo docker stop $cname
fi
check=`sudo docker ps | grep $cname`
if [ -z "$check" ]; then
echo $cname stopped!
else
echo FAILED to stop $cname
fi
处理里面项目,参考?简介 | Composer 中文文档 | Composer 中文网 ?安装 composer
composer update 安装项目 vendor 目录下的依赖包
出现少png.h,apt install? libpng-dev?
对于缺少PHP扩展,类似 docker-php-ext-install? gd? zip 安装
类似处理 nginx1.14,我们希望不同的项目不同的组合,所以,这里建立 start_gxlq_pig.sh 为例(启动该脚本会先启动mariadb10.3 和 php7.2fpm)
#!/bin/bash
./start_mariadb.sh
./start_php7.2fpm.sh
mariadb_started=`sudo docker ps | grep 'mariadb10.3'`
php72fpm_started=`sudo docker ps | grep 'php7.2fpm'`
if [ -z "$mariadb_started" -o -z "$php72fpm_started" ]; then
echo container mariadb10.3 and php7.2fpm must be started first! exit
exit
fi
cname='gxlq_pig'
echo docker 准备运行 $cname 容器
echo -e "目录映射 host \e[1;34m/home/sjg/docker_data/gxlq_pig\e[0m -> container \e[1;35m/gxlq_pig\e[0m"
echo -e "端口映射 host \e[1;34m8066\e[0m -> container \e[1;35m80\e[0m"
echo -e "容器联系 \e[1;31mphp7.2fpm:phpfpm\e[0m"
already_started=`sudo docker ps | grep $cname`
if [ ! -z "$already_started" ]; then
echo $cname already started
else
has_container=`sudo docker ps -a | grep $cname`
if [ ! -z "$has_container" ]; then
echo find container $cname, try to start it
sudo docker start $cname
else
echo no container $cname, try to create and run
sudo docker run -d -v /home/sjg/docker_data/gxlq_pig:/gxlq_pig -p 8066:80 --link php7.2fpm:phpfpm --name $cname nginx:1.14
fi
started=`sudo docker ps | grep $cname`
if [ -z "$started" ]; then
echo FAILED to start $cname
exit
fi
fi
if [ $# -eq 1 -a "$1" = "bash" ]; then
echo 运行容器内部伪终端
sudo docker exec -ti $cname /bin/bash
fi
#!/bin/bash
cname='gxlq_pig'
started=`sudo docker ps | grep $cname`
if [ ! -z "$started" ]; then
sudo docker stop $cname
fi
check=`sudo docker ps | grep $cname`
if [ -z "$check" ]; then
echo $cname stopped!
else
echo FAILED to stop $cname
fi
apt update,? apt? install? vim procps, 用 vim 修改 /etc/nginx/conf.d/default.conf,和生产环境相比,开发环境去掉 SSL,phpfpm 改成 端口方式 (如果原来是 unix socket 方式), ps -aux 查看nginx工作进程的用户为nginx,对于开发环境,我们简单让nginx用户对项目目录都是可写的(或者 chmod 0777 -R)
对于php7.2fpm容器添加 xdebug 以便可以调试,可以部分? 参考?docker 配置xdebug成功案例【极其简单】_Lzq_1010的专栏-CSDN博客_docker xdebug
Xdebug: Documentation ? Step Debugging
apt update, apt install wget,?cd /usr/src/php/ext/ wget http://xdebug.org/files/xdebug-3.0.3.tgz && tar zxvf xdebug-3.0.3.tgz && mv xdebug-3.0.3 xdebug && rm -f xdebug-3.0.3.tgz
docker-php-ext-install xdebug,??docker-php-ext-enable xdebug apt update && apt install vim,? vim?/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini 修改配置 (容器内重启 php-fpm服务可以 kill? -USR2? 1? ,因为 php-fpm 主进程 id 为 1)
zend_extension=xdebug.so xdebug.mode=debug xdebug.remote_port=9001 xdebug.idekey=PHPSTORM xdebug.remote_log=/tmp/xdebug_remote.log xdebug.discover_client_host=1
PHPSTORM配置:可以在 settings-PHP-Debug 中按 Pre-configuration 提示操作,因为谷歌商店被墙,我们就用GET参数 XDEBUG_SESSION_START=PHPSTORM来激发调试,Xdebug port 9003,settings-PHP-servers设置好服务器的路径映射,本地源码目录和容器内绝对路径映射(项目根和web根)
php-fpm 开启日志可以参考??开启php-fpm日志记录 – 行星带
容器内配置改坏了导致容器启动不了恢复办法可以参考?Docker容器无法启动,里面的配置文件如何修改 - 知乎
docker 的 php7.2fpm 的扩展和linux的不太一样,gd扩展少 free-type jpeg 等支持
apt? install? libjpeg-dev (针对 error: jpeglib.h not found.)
apt install libfreetype-dev (针对?fatal error: ft2build.h: No such file or directory )
docker-php-ext-configure gd --with-freetype-dir=/usr --with-jpeg-dir=/usr --with-png-dir=/usr \ ? && docker-php-ext-install -j "$(nproc)" gd
php -r "print_r(gd_info());"? 查看是否已经支持 docker-php-ext-install pdo pdo_mysql? (针对访问 mariadb) apt update && apt install openssh-client? (针对没有 scp,如果容器内需要 ssh 和 scp 命令的话 ) ?
从 php-fpm 容器编写测试连接数据库:(注意,对容器来说 localhost 都是自己,所以,连接时 host 需要使用容器名或者宿主的IP地址,找不到连不上会报错 Error!: SQLSTATE[HY000] [2002] No such file or directory)
<?php
$dbms="mysql";
$host="mariadb10.3"; // 注意:必须用 容器名 或者 宿主IP
$dbport = "3306";
$dbName="gxlq_pig";
$user="gxlq_pig";
$pass="××××××";
$dsn="$dbms:host=$host;port=$dbport;dbname=$dbName";
try {
$dbh = new PDO($dsn, $user, $pass);
echo "successful<br/>";
$dbh = null;
} catch (PDOException $e) {
die ("Error!: " . $e->getMessage() . "<br/>");
}
|