sudo apt-get update
sudo apt-get install docker.io
#安装docker
sudo docker pull wordpress
#获取wordpress镜像
sudo docker pull mysql
#获取mysql镜像
sudo docker run –name mysql_1 -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 -d mysql
#运行mysql镜像,容器名称mysql_1,设置mysl密码123456,后台运行
sudo docker exec -it mysql_1 /bin/bash
#进入mysql容器
mysql -u root -p
123456
#登录mysql
create database wordpress
#创建database
exit
exit
#退出
sudo docker run –name wordpress_1 –link mysql_1:mysql -p 80:80 -d wordpress
#运行wordpress镜像,容器名称wordpress_wordpress, 关联mysql_wordpress容器作为mysql数据库, 将容器的80端口(第二个)映射到主机的80端口(第一个)
完成
然后遇到了这个错误:
error establishing a database connection
原因是:database host 应该是mysql容器名称
database host should use mysql container’s name(which here I am using mysql_1), not localhost.
原文链接:https://blog.csdn.net/weixin_44345526/article/details/122193505?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168994567316800226569088%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=168994567316800226569088&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~times_rank-10-122193505-null-null.268%5Ev1%5Ekoosearch&utm_term=docker%E3%80%81wordpress%E3%80%81wordpress%E5%BB%BA%E7%AB%99%E3%80%81wordpress%E4%B8%BB%E9%A2%98%E3%80%81%E5%AE%B9%E5%99%A8%E9%95%9C%E5%83%8F%E3%80%81