鏡像下載
PHP鏡像下載
docker pull php:7.4.8-fpm
Nginx鏡像下載
docker pull nginx
Mysql鏡像下載
docker pull mysql:5.7
驗證
docker images
結果如圖:
制作配置文件
創(chuàng)建 ~/nginx/conf/ 配置等目錄
mkdir -p ~/nginx/logs ~/nginx/conf
創(chuàng)建nginx的配置文件
vim ~/nginx/conf/nginx.conf
配置文件如下:
server {
listen 80;
server_name localhost;
location / {
root /webroot/;
index index.html index.htm index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /webroot/;
}
location ~ \.php$ {
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /www/$fastcgi_script_name;
include fastcgi_params;
}
}
配置文件說明:
- php:9000: 表示 php 服務的 URL娇妓。
- /webroot/: 是 php 容器中 php 文件的存儲路徑煎谍,映射到本地的 ~/Documents/code 目錄醉锄。
在 ~/Documents/code 目錄下創(chuàng)建index.php
<?php
phpinfo();
?>
運行Mysql容器
docker run -it -d -p8066:3306 -e MYSQL_ROOT_PASSWORD=123456 --name mysql57 -v ~/Documents/code/mysqldata:/usr/local/mysql mysql:5.7
運行PHP容器
docker run --name php7 -v ~/Documents/code:/webroot -d php:7.4.8-fpm
運行Nginx容器
docker run --name mynginx -p 80:80 -v ~/Documents/code:/webroot -v ~/nginx/conf:/etc/nginx/conf.d --link php7:php --link mysql57:mysql -d nginx
命令說明:
- --name mynginx : 將容器命名為 mynginx
- -v ~/Documents/code:/webroot : 將本地目錄~/Documents/code 掛載到容器的/webroot目錄下
驗證
瀏覽器訪問 localhost
配置虛擬域名
修改nginx的配置文件
vim ~/nginx/conf/nginx.conf
server {
listen 80;
server_name mytest.com;
location / {
root /webroot/test/;
index index.html index.htm index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /webroot/test/;
}
location ~ \.php$ {
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /webroot/test/$fastcgi_script_name;
include fastcgi_params;
}
}
重啟容器
docker restart mynginx
修改hosts文件
sudo vim /ets/hosts
增加
127.0.0.1 mytest.com
測試
在~/Documents/code下創(chuàng)建test文件夾灭贷,在該文件夾新建一個index.php
<?php
echo "hello world";
?>
驗證
瀏覽器訪問 mytest.com