启动一个临时docker实例,进入docker可以查看php.ini的配置文件 在/usr/local/etc/php/目录.
使用 docker cp tmpphp:/usr/local/etc/php/php.ini-development ./ 将php.ini配置文件copy下来.
/www目录是nginx重应该指定的路径.
容器互联:
1、创建容器 test1
docker run -d --name test1 nginx
2、创建容器 test2并 link 到 test1(现在推荐使用 --network)。
docker run -d --name test2 --link test1 nginx
3、进入test2,并 ping test1,发现是可以 ping 通的
docker exec -it test2 /bin/sh
ping test1
test2 link 到 test1,其实就是修改了 test2自己的 host (/etc/hosts) 文件和设置了环境变量而已,所以test2能 ping 通 test1,但是 test1无法 ping通 test2.
docker 安装 ping 命令:
apt-get update
apt install iputils-ping
# apt install net-tools # 安装 ifconfig
使用–network搭建网络连接:
docker network create mynetwork # 创建自定义网络。
docker run -d --name test1 --network mynetwork nginx
docker run -d --name test2 --network mynetwork nginx
这个时候 无论在test1,还是test2 都可以相互ping通,或者ping通自己(容器名,或127.0.0.1)
docker network inspect mynetwork # 查看使用mynetwork网络的容器列表:
复制临时 docker的配置:
sudo docker cp tmpphp:/usr/local/etc ./
使用容器互联的好处是:
- docker启动会重新分配IP,使用docker互联,可以使得容器名连接。
# 注意如果不想手写配置,挂载的配置目录和文件需要从临时实例中copy一份.
docker run --name mymysql --restart=always -v /Users/mfw/docker/mysql/etc:/etc/mysql --network=mynetwork -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql
# 注意如果不想手写配置,挂载的配置目录和文件需要从临时实例中copy一份.
docker run --name myphpfpm --restart=always -v /Users/mfw/docker/php/www:/www -v /Users/mfw/docker/php/etc:/usr/local/etc --network=mynetwork -p 9000:9000 -d php:7.2-fpm
# 注意如果不想手写配置,挂载的配置目录和文件需要从临时实例中copy一份.
docker run --name=mynginx --network=mynetwork -p 8080:80 \
--restart=always \
-v /Users/mfw/docker/nginx/nginx.conf:/etc/nginx/nginx.conf \
-v /Users/mfw/docker/nginx/conf.d:/etc/nginx/conf.d \
-v /Users/mfw/docker/nginx/www:/usr/share/nginx/html \
-v /Users/mfw/docker/nginx/logs:/var/log/nginx \
-d nginx
删除default.conf,重写 webphp.conf 内容:
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_pass myphpfpm:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /www$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
需要配置 mac的hosts文件:120.0.0.1 localhost
phpinfo(); 可以看到PDO默认不支持mysql数据库。 需要 安装pdo_msyql扩展。
docker exec -it myphpfpm /bin/bash
cd /usr/local/bin
./docker-php-ext-install pdo_mysql
<?php
echo "test. ";
$servername = "mymysql"; // 可以使用docker名直接连
$username = "root";
$password = "123456";
try {
$conn = new PDO("mysql:host=$servername;", $username, $password);
echo "连接成功";
}
catch(PDOException $e)
{
echo $e->getMessage();
}
运行结果如下:
网上找了一堆说明,没有几个是正确的,最后:
docker exec -it mymysql /bin/bash
mysql -uroot -p123456
use mysql
ALTER USER root IDENTIFIED WITH mysql_native_password BY '123456'; # ALTER USER root IDENTIFIED WITH mysql_native_password BY 'PASSWORD';
成功解决问题