使用Docker快速搭建一个WordPress
2022/11/23 17:17 投稿
MYSQL 部分
[root@vms91 opt]#docker pull mysql //拉取mysql
[root@vms91 opt]# docker run -d -v /opt/database/:/var/lib/mysql --name database-wp -e MYSQL_ROOT_PASSWORD=redhat -e MYSQL_DATABASE=wordpress mysql   //启动一个容器,后台运行,挂载卷,容器名为database-wp;设置root密码;创建数据库;从mysql image启动
[root@vms91 opt]# docker inspect database-wp | grep -i ipaddress   //查看数据库的内部IP
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.2",
"IPAddress": "172.17.0.2",
[root@vms91 opt]#yum install mysql //本地安装mysql作为client 尝试去连接db
[root@vms91 opt]# mysql -uroot -p -h 172.17.0.2 //报错如下,因为新版本的特性,我们需要做一些配置修改
Enter password
ERROR 2059 (HY000): Authentication plugin 'caching_sha2_password' cannot be loaded: /usr/lib64/mysql/plugin/caching_sha2_password.so: cannot open shared object file: No such file or directory
[root@vms91 opt]# docker exec -it dababase-wp bash  //通过交互进入容器
root@6f5c6c58f20f:/# mysql -u root -p  //容器内登陆mysql
Enter password:
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'redhat'; //执行相应命令,将用户的加密方式改为mysql_native_password
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges; //刷新权限
Query OK, 0 rows affected (0.00 sec)
[root@vms91 opt]# mysql -uroot -p -h 172.17.0.2 //返回本机登陆
Enter password:
MySQL [(none)]> show databases; //成功进入数据库,并看到了我们之前创建的数据库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| wordpress          |
+--------------------+
5 rows in set (0.00 sec)
WORDPRESS部分
[root@vms91 opt]#docker pull wordpress //拉取wordpress
[root@vms91 opt]# docker run -d --name wordpress -e WORDPRESS_DB_HOST=172.17.0.2 -e WORDPRESS_DB_USER=root -e WORDPRESS_DB_PASSWORD=redhat -e WORDPRESS_DB_NAME=wordpress -v /opt/wordpress/:/var/www/html/ -p 80:80 wordpress
//后台运行,命名为wordpress,指定数据库IP,数据库登录名,数据库密码,数据库名,挂载卷,端口映射,镜像名
[root@vms91 opt]# docker inspect wordpress | grep -i ipaddress //查看IP
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.3",
"IPAddress": "172.17.0.3",
通过docker宿主机的ip查看,因为我们将宿主机的80映射到了容器的80

创建完成!

[root@vms91 opt]# mysql -uroot -p -h 172.17.0.2 //我们可以返回数据库查看数据表,发现wordpress的需要的数据表已自动创建
Enter password:
MySQL [(none)]> use wordpress;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MySQL [wordpress]> show tables;
+-----------------------+
| Tables_in_wordpress   |
+-----------------------+
| wp_commentmeta        |
| wp_comments           |
| wp_links              |
| wp_options            |
| wp_postmeta           |
| wp_posts              |
| wp_term_relationships |
| wp_term_taxonomy      |
| wp_termmeta           |
| wp_terms              |
| wp_usermeta           |
| wp_users              |
+-----------------------+
12 rows in set (0.00 sec)