09-23 17:56 nginx mysql docker php 技巧 docker 安装nginx+php+mysql #### 安装nginx 创建www目录 `mkdir /www /www/wwwroot` 拉取官方镜像 `docker pull nginx` 创建容器先简单的让nginx运行起来 `docker run -d --name nginx -p 80:80 -p 443:443 nginx` 将容器配置文件以及日志拷贝出来 ```shell docker cp nginx:/etc/nginx /www/server/nginx/config docker cp nginx:/var/log/ /www/server/nginx/log ``` 删除以创建nginx容器,重新创建并挂载 ```shell docker stop nginx docker rm nginx docker run -d --name nginx -p 80:80 -p 443:443 -v /www/wwwroot:/usr/share/nginx/html -v /www/server/nginx/log:/var/log/nginx -v /www/server/nginx/config:/etc/nginx nginx ``` #### 安装php7.2 拉取php7.2镜像 `docker pull php:7.2-fpm` 创建容器 `docker run -d --name phpfpm7.2 :9000 -v /www/wwwroot:/usr/share/nginx/html` 获取phpfpm7.2这个容器的ip ```shell docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' phpfpm7.2 172.18.0.3 ``` 最后将这个内网ip:172.18.0.3配置到/www/server/nginx/config/default.conf ```shell location ~ \.php$ { root /usr/share/nginx/html; fastcgi_pass 172.18.0.3:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } ``` 重启nginx 即可访问www/wwwroot 目录 #### 安装mysql 下载镜像mysql8 `docker pull mysql:8` 创建容器 `docker run -d --name mysql8 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql:8` 将容器相关配置文件以及日志拷贝出来 ```shell docker cp mysql8:/etc/mysql /www/server/mysql8/config dcoker cp mysql8:/var/log /www/server/mysql8/log docker cp mysql8:/var/lib/mysql /www/server/mysql/data ``` 删除以创建mysql容器,重新创建并挂载 ```shell docker stop mysql8 docker rm mysql8 docker run -d -p 3306:3306 -v /www/server/mysql8/config:/etc/mysql -v /www/server/mysql8/log/:/var/log -v /www/server/mysql8/data/:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:8 ```