基于Docker 搭建 wordpress

在Docker中,一般遵循一个Docker只运行一个应用,这样方便维护。

首先需要将centos 镜像pull到本地,并搭建本地yum仓库

yum仓库地址:http://192.168.2.11:8000  这里使用阿里yum和163yum都可以。

[root@node1 test]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/centos latest 904d6c400333 weeks ago 196.7 MB

编写Dockerfile文件

[root@node1 web-Dockerfile]# vim Dockerfile 

FROM centos
MAINTAINER hukey
RUN rm -rf /etc/yum.repos.d/*
RUN echo -e "[yum]\nname = yum\ngpgcheck = 0\nbaseurl = http://192.168.2.11:8000" > /etc/yum.repos.d/yum.repo
RUN yum install httpd php php-mysql php-mbstring -y && yum clean all
EXPOSE 80
CMD ["/usr/sbin/httpd","-f","/etc/httpd/conf/httpd.conf","-DFOREGROUND"]

尝试执行Dockerfile文件

[root@node1 web-Dockerfile]# docker build -f Dockerfile -t web:centos7 .
...
...
Step : EXPOSE
---> Running in cd581ad95fb0
---> 368d9fc0b1ce
Removing intermediate container cd581ad95fb0
Step : CMD /usr/sbin/httpd -f /etc/httpd/conf/httpd.conf -DFOREGROUND
---> Running in 34e0a903d8b5
---> 07d76f84111c
Removing intermediate container 34e0a903d8b5
Successfully built 07d76f84111c

docker每执行完成一行RUN 语句就会生成一个镜像文件,如果还有RUN指令执行,就会删除原来的镜像文件,并创建一个新的镜像文件,因此在编写Dockerfile时,要简练。

通过生成的镜像文件运行一个容器

[root@node1 web-Dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
web centos7 07d76f84111c minutes ago MB
docker.io/centos latest 904d6c400333 weeks ago 196.7 MB
[root@node1 web-Dockerfile]# docker run -d -p : -v /myweb/:/var/www/html/ --name web web:centos7   # -v 本机目录/myweb 挂载到容器内的/var/www/html目录

通过查看,我们已经成功的创建了一个web容器。

[root@node1 web-Dockerfile]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7d5c54ba0cf1 web:centos7 "/usr/sbin/httpd -f /" seconds ago Up seconds 0.0.0.0:->/tcp web
[root@node1 web-Dockerfile]# docker port web
/tcp -> 0.0.0.0:

测试访问:
http://192.168.2.11

基于Docker 搭建 wordpress

web 服务已经搭建完毕,接下来是MySQL

编写Dockerfile文件,CMD运行的mysql.sh脚本,需要我们手动编写。这里开通ssh服务,方便对于数据库的管理工作

[root@node1 mysql-Dockerfile]# vim Dockerfile 

   FROM centos
MAINTAINER hukey
RUN rm -rf /etc/yum.repos.d/* && echo -e "[yum]\nname = yum\ngpgcheck = 0\nbaseurl = http://192.168.2.11:8000" > /etc/yum.repos.d/yum.repo
4 RUN yum install mariadb-server openssh-server -y && yum clean all
5 RUN mysql_install_db && chown -R mysql:mysql /var/lib/mysql/
6 VOLUME /var/lib/mysql/
7 ADD mysql.sh /mysql.sh
8 RUN chmod 755 /mysql.sh
9 EXPOSE 22
10 EXPOSE 3306
11 CMD ["/mysql.sh"]

mysql.sh 脚本内容

[root@node1 mysql-Dockerfile]# vim mysql.sh 

   #!/bin/bash
# Author:hukey
mysqld_safe &
sleep
mysqladmin -uroot password ''
mysql -uroot -p123456 -e "GRANT ALL ON *.* TO 'root'@'%' IDENTIFIED BY '123456';FLUSH PRIVILEGES;"
sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config && ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
echo | passwd --stdin root
/usr/sbin/sshd -D

尝试生成镜像

[root@node1 mysql-Dockerfile]# docker build -f Dockerfile -t mariadb:centos7 .
[root@node1 mysql-Dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mariadb centos7 49b88fcfb709 seconds ago 367.8 MB

生成完成,尝试启动容器

[root@node1 mysql-Dockerfile]# docker run -d -p : --name db mariadb:centos7
[root@node1 mysql-Dockerfile]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c0988d8c8eb2 mariadb:centos7 "/mysql.sh" seconds ago Up seconds /tcp, 0.0.0.0:->/tcp db

通过ssh服务连接到容器

[root@node1 mysql-Dockerfile]# ssh 192.168.2.11 -p
The authenticity of host '[192.168.2.11]:20002 ([192.168.2.11]:20002)' can't be established.
RSA key fingerprint is 8c:c0:c0:a5:1e:0b::::1e::::::bc.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[192.168.2.11]:20002' (RSA) to the list of known hosts.
root@192.168.2.11's password:
[root@c0988d8c8eb2 ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is
Server version: 5.5.-MariaDB MariaDB Server Copyright (c) , , Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
rows in set (0.00 sec)

现在web容器和mariadb容器已经搭建完毕,接下来就需要将它们关联在起来

[root@node1 mysql-Dockerfile]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c0988d8c8eb2 mariadb:centos7 "/mysql.sh" minutes ago Up minutes /tcp, 0.0.0.0:->/tcp db
7d5c54ba0cf1 web:centos7 "/usr/sbin/httpd -f /" minutes ago Up minutes 0.0.0.0:->/tcp web
[root@node1 mysql-Dockerfile]# docker stop web
web
[root@node1 mysql-Dockerfile]# docker rm web
web

测试是否能够访问到mysql容器

[root@node1 mysql-Dockerfile]# docker run -it -p : --link=db --name web web:centos7 /bin/bash

[root@f310c805bdfc /]# yum install mariadb -y
[root@f310c805bdfc /]# mysql -uroot -p123456 -h db
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is
Server version: 5.5.-MariaDB MariaDB Server Copyright (c) , , Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
rows in set (0.00 sec)

连接mysql容器成功。

再次创建后台运行的web容器

[root@node1 web-Dockerfile]# docker run -d -p : -v /myweb/:/var/www/html/ --link=db:todb --name=web web:centos7
[root@node1 web-Dockerfile]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4a36ee3781da web:centos7 "/usr/sbin/httpd -f /" seconds ago Up seconds 0.0.0.0:->/tcp web
50dba9d34f51 mariadb:centos7 "/mysql.sh" minutes ago Up minutes /tcp, 0.0.0.0:->/tcp db

安装wordpress web程序

[root@node1 myweb]# cp -a /root/wordpress/* /myweb/

浏览器访问

基于Docker 搭建 wordpress

登录数据库容器,创建wordpress所需数据库。

[root@node1 myweb]# ssh 192.168.2.11 -p
root@192.168.2.11's password:
[root@50dba9d34f51 ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is
Server version: 5.5.-MariaDB MariaDB Server Copyright (c) , , Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> CREATE DATABASE wordpress;
Query OK, row affected (0.09 sec) MariaDB [(none)]> GRANT ALL ON wordpress.* TO 'wordpress'@'%' IDENTIFIED BY 'wordpress';
Query OK, rows affected (0.03 sec) MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, rows affected (0.02 sec) MariaDB [(none)]> Bye

基于Docker 搭建 wordpress

基于Docker 搭建 wordpress

到此,基于Docker lamp平台的wordpress搭建成功。

如果是在生产环境中使用docker,不太建议将数据库程序存放与docker内执行。建议MySQL 实现负载或者冗余。

上一篇:利用TEA算法进行数据加密


下一篇:网络编程socket