【Dockerfile镜像实战】构建LNMP环境并运行Wordpress网站平台
- Nginx部署
- MySQL部署
- PHP部署
Nginx部署
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# docker pull centos:7
[root@localhost ~]# docker images
cd /opt
mkdir nginx
cd nginx
vim Dockerfile
vim nginx.conf
cd /opt/nginx
rz -E //将nginx的安装包导入进来
mkdir html
cd html
rz -E //将wordpress的安装包导入进来
tar xf wordpress-4.9.4-zh_CN.tar.gz //解压文件
rm -rf wordpress-4.9.4-zh_CN.tar.gz //删除压缩包
[root@localhost nginx]# docker build -t nginx:lnmp .
[root@localhost nginx]# docker images
docker run -d --name nginx -p 80:80 -v /opt/nginx/html:/usr/local/nginx/html -v /opt/nginx/nginx.conf:/usr/local/nginx/conf/nginx.conf --net lnmpnetwork --ip 172.18.0.10 nginx:lnmpdocker ps -a
cd /opt/nginx/html
vim index.html
//新建一个测试网页,查看nginx是否能正常运行
[root@localhost nginx]# docker exec -it nginx bash
[root@d94d0066c01a /]#
[root@d94d0066c01a /]# cd /usr/local/nginx/html/
[root@d94d0066c01a html]#
[root@d94d0066c01a html]# ls
wordpress
MySQL部署
[root@localhost nginx]# cd /opt
[root@localhost opt]#
[root@localhost opt]# ls
containerd nginx rh
[root@localhost opt]#
[root@localhost opt]# mkdir mysql php
[root@localhost opt]#
[root@localhost opt]# ls
containerd mysql nginx php rh
[root@localhost opt]#
[root@localhost opt]# cd mysql/
[root@localhost mysql]#
[root@localhost mysql]# ls
[root@localhost mysql]#
[root@localhost mysql]# rz -E
rz waiting to receive.
[root@localhost mysql]#
[root@localhost mysql]# touch Dockerfile
[root@localhost mysql]#
[root@localhost mysql]# ls
Dockerfile mysql-boost-5.7.41.tar.gz
[root@localhost mysql]# vim Dockerfile
[root@localhost mysql]# vim my.cnf
[root@localhost mysql]# docker build -t mysql:lnmp .
[root@localhost docker]# docker run --name mysql -d --privileged -v /usr/local/mysql --network lnmpnetwork --ip 172.18.0.20 mysql:lnmp
13f2bb048679a6d4042f5f235e794305ca6e8c2912d8b13b8b8d891c0911a34f
[root@localhost docker]#
[root@localhost docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
13f2bb048679 mysql:lnmp "/usr/local/mysql/bi…" 17 seconds ago Up 10 seconds 3306/tcp mysql
d94d0066c01a nginx:lnmp "/usr/local/nginx/sb…" 2 hours ago Up 46 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp, 443/tcp nginx
[root@localhost docker]#
[root@13f2bb048679 mysql]# mysqlmysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)mysql> quit;
Bye
[root@13f2bb048679 mysql]#
[root@13f2bb048679 mysql]# mysqladmin -u root -p password "abc123"
[root@13f2bb048679 mysql]# mysql -u root -pabc123
PHP部署
[root@localhost mysql]#
[root@localhost mysql]# cd ../php/
[root@localhost php]#
[root@localhost php]# ls
[root@localhost php]#
[root@localhost php]# rz -E
rz waiting to receive.
[root@localhost php]#
[root@localhost php]# ls
php-7.1.10.tar.bz2
[root@localhost php]#
[root@localhost php]# vim Dockerfile
[root@localhost php]#
[root@localhost php]#
[root@localhost php]# vim php-fpm.conf
[root@localhost php]#
[root@localhost php]# vim php.ini
[root@localhost php]#
[root@localhost php]# vim www.conf
[root@localhost php]#
[root@localhost php]# docker build -t php:lnmp .
[root@localhost php]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
php lnmp f969ee0fe328 2 minutes ago 421MB
mysql lnmp 03aa4c425566 3 hours ago 4.24GB
nginx lnmp 01356f501e0c 6 hours ago 208MB
centos 7 eeb6ee3f44bd 2 years ago 204MB
[root@localhost php]#
[root@localhost php]# docker run --name=php -d -p 9000:9000 --volumes-from mysql --volumes-from nginx --network lnmpnetwork --ip 172.18.0.30 php:lnmp
fc67bd7f644ba8b704398db992420fcf80662b50b0a1ab28a9200af6ff73f400
[root@localhost php]#
[root@localhost php]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fc67bd7f644b php:lnmp "/usr/local/php/sbin…" 12 seconds ago Up 11 seconds 0.0.0.0:9000->9000/tcp, :::9000->9000/tcp php
13f2bb048679 mysql:lnmp "/usr/local/mysql/bi…" 3 hours ago Up 3 hours 3306/tcp mysql
d94d0066c01a nginx:lnmp "/usr/local/nginx/sb…" 5 hours ago Up 4 hours 0.0.0.0:80->80/tcp, :::80->80/tcp, 443/tcp nginx
[root@localhost php]#
[root@localhost php]# docker exec -it nginx bash
[root@d94d0066c01a /]#
[root@d94d0066c01a /]# cd /usr/local/nginx/html/
[root@d94d0066c01a html]#
[root@d94d0066c01a html]# ll
total 4
drwxr-xr-x. 5 nobody 65534 4096 Feb 8 2018 wordpress
[root@d94d0066c01a html]#
[root@d94d0066c01a html]# chown -R nginx:nginx wordpress/
[root@d94d0066c01a html]#
[root@d94d0066c01a html]# ll
total 4
drwxr-xr-x. 5 nginx nginx 4096 Feb 8 2018 wordpress
[root@d94d0066c01a html]#
[root@13f2bb048679 /]# mysql -u root -p
Enter password:
mysql>
mysql> create database wordpress;
Query OK, 1 row affected (0.01 sec)mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| wordpress |
+--------------------+
5 rows in set (0.00 sec)mysql>
mysql> grant all privileges on wordpress.* to 'wordpress'@'%' identified by 'admin123';
Query OK, 0 rows affected, 1 warning (0.01 sec)mysql> grant all privileges on *.* to 'root'@'%' identified by 'admin123';
Query OK, 0 rows affected, 1 warning (0.00 sec)mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
浏览器访问:
http://192.168.190.120/wordpress/index.php