1. 拉取镜像
[root@docker01 ~]# docker pull mysql:5.7
[root@docker01 ~]# docker pull nginx
[root@docker01 html]# docker pull alvinos/php:v2-fpm-mysql
2. 下载wordpress
[root@docker01 ~]# wget https://cn.wordpress.org/latest-zh_CN.tar.gz
3. 创建挂载目录,将wordpress解压到挂载目录
[root@docker01 ~]# mkdir /tmp/html
[root@docker01 ~]# tar -xf latest-zh_CN.tar.gz -C /tmp/
[root@docker01 ~]# cd /tmp/html
[root@docker01 html]# mv /tmp/wordpress/* .
4. 创建nana网桥
[root@docker01 html]# docker network create nana
[root@docker01 html]# docker network ls
NETWORK ID NAME DRIVER SCOPE
988b1280d96e bridge bridge local
e185b787c28e host host local
985d31a41d37 nana bridge local
01c19cbebe06 none null local
[root@docker01 html]# ip a
...
5: br-985d31a41d37: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
link/ether 02:42:9a:e8:9c:31 brd ff:ff:ff:ff:ff:ff
inet 172.18.0.1/16 brd 172.18.255.255 scope global br-985d31a41d37
valid_lft forever preferred_lft forever
5. 创建并运行数据库容器(mysql5.7)
# MYSQL_DATABASE: 创建数据库wordpress --network: 网络指定为自定义网桥nana
[root@docker01 html]# docker run -d -e MYSQL_ROOT_PASSWORD=123 -e MYSQL_DATABASE=wordpress --network nana --name mysql mysql:5.7
# 查看mysql数据库容器是否正常运行
[root@docker01 html]# docker ps | grep mysql
d1a7737ad91a mysql:5.7 "docker-entrypoint.s…" 18 minutes ago Up 18 minutes 3306/tcp, 33060/tcp mysql
6. 创建并运行php容器
[root@docker01 html]# docker run -d --network nana -v /tmp/html:/usr/share/nginx/html --name php alvinos/php:v2-fpm-mysql
# 查看php容器是否正常运行
[root@docker01 html]# docker ps | grep php
8210f15c03c1 alvinos/php:v2-fpm-mysql "php-fpm -F" 34 seconds ago Up 33 seconds 9000/tcp php
7. 创建并运行nginx容器
[root@docker01 html]# docker run -d -p 80:80 -v /tmp/html:/usr/share/nginx/html --name nginx --network nana nginx
# 查看nginx容器是否正常运行
[root@docker01 html]# docker ps | grep nginx
6e9d178296f8 nginx "/docker-entrypoint.…" 23 seconds ago Up 23 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp nginx
8. 进入nginx容器,创建nginx用户www
[root@docker01 html]# docker exec -it nginx bash
root@6e9d178296f8:/# useradd www
root@6e9d178296f8:/# id www
uid=1000(www) gid=1000(www) groups=1000(www)
root@6e9d178296f8:/# exit
exit
9. 修改容器内的nginx主配置文件
[root@docker01 html]# cd /opt
# 复制Nginx主配置文件
# 将容器内部文件复制到宿主主机
[root@docker01 opt]# docker cp nginx:/etc/nginx/nginx.conf .
# 修改nginx主配置文件的用户信息
[root@docker01 opt]# sed -i "/^user /c user www;" nginx.conf
# 覆盖docker容器里面的原文件
[root@docker01 opt]# docker cp nginx.conf nginx:/etc/nginx/nginx.conf
10. 修改容器内nginx的server文件
# 将容器内部文件复制到宿主主机
[root@docker01 opt]# docker cp nginx:/etc/nginx/conf.d/default.conf .
[root@docker01 opt]# vim default.conf
server {
listen 80;
listen [::]:80;
server_name localhost;
# wordpress(*.php文件)默认的查找路径
root /usr/share/nginx/html;
location / {
index index.php index.html index.htm;
}
location ~ \.php$ {
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
# 覆盖docker容器里面的原文件
[root@docker01 opt]# docker cp default.conf nginx:/etc/nginx/conf.d/default.conf
11. 检查修改好的nginx配置文件
[root@docker01 opt]# docker exec -it nginx bash
root@6e9d178296f8:/# cat /etc/nginx/conf.d/default.conf
root@6e9d178296f8:/# cat /etc/nginx/nginx.conf
root@6e9d178296f8:/# exit
12. 重启nginx容器
[root@docker01 opt]# docker restart nginx
[root@docker01 opt]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6e9d178296f8 nginx "/docker-entrypoint.…" 15 minutes ago Up 55 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp nginx
8210f15c03c1 alvinos/php:v2-fpm-mysql "php-fpm -F" 19 minutes ago Up 19 minutes 9000/tcp php
d1a7737ad91a mysql:5.7 "docker-entrypoint.s…" 45 minutes ago Up 45 minutes 3306/tcp, 33060/tcp mysql
13. 打开浏览器输入ip: 192.168.15.80
步骤如图所示
14. 创建浏览器指定的wp-config.php文件
[root@docker01 opt]# cd /tmp/html/
# 将浏览器生成的配置文件内容复制到wp-conf.php文件中
[root@docker01 html]# vim wp-config.php