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

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

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

[root@node1 test]# docker imagesREPOSITORY          TAG                 IMAGE ID            CREATED             SIZEdocker.io/centos    latest              904d6c400333        4 weeks ago         196.7 MB

编写 Dockerfile 文件

[root@node1 web-Dockerfile]# vim Dockerfile FROM centosMAINTAINER hukeyRUN rm -rf /etc/yum.repos.d/*RUN echo -e "[yum]nname = yumngpgcheck = 0nbaseurl = http://192.168.2.11:8000" > /etc/yum.repos.d/yum.repoRUN yum install httpd php php-mysql php-mbstring -y && yum clean allEXPOSE 80CMD ["/usr/sbin/httpd","-f","/etc/httpd/conf/httpd.conf","-DFOREGROUND"]

尝试执行 Dockerfile 文件

[root@node1 web-Dockerfile]# docker build -f Dockerfile -t web:centos7 .......Step 6 : EXPOSE 80 ---> Running in cd581ad95fb0 ---> 368d9fc0b1ceRemoving intermediate container cd581ad95fb0Step 7 : CMD /usr/sbin/httpd -f /etc/httpd/conf/httpd.conf -DFOREGROUND ---> Running in 34e0a903d8b5 ---> 07d76f84111cRemoving intermediate container 34e0a903d8b5Successfully built 07d76f84111c

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

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

[root@node1 web-Dockerfile]# docker imagesREPOSITORY          TAG                 IMAGE ID            CREATED             SIZEweb                 centos7             07d76f84111c        2 minutes ago       268 MBdocker.io/centos    latest              904d6c400333        4 weeks ago         196.7 MB[root@node1 web-Dockerfile]# docker run -d -p 80:80 -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                NAMES7d5c54ba0cf1        web:centos7         "/usr/sbin/httpd -f /"   11 seconds ago      Up 10 seconds       0.0.0.0:80->80/tcp   web[root@node1 web-Dockerfile]# docker port web80/tcp -> 0.0.0.0:80

测试访问:
http://192.168.2.11

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

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

[root@node1 mysql-Dockerfile]# vim Dockerfile   1 FROM centos  2 MAINTAINER hukey  3 RUN rm -rf  /etc/yum.repos.d/*  && echo -e "[yum]nname = yumngpgcheck = 0nbaseurl = 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   1 #!/bin/bash  2 # Author:hukey  3 mysqld_safe &  4 sleep 5  5 mysqladmin -uroot password '123456'  6 mysql -uroot -p123456 -e "GRANT ALL ON *.* TO 'root'@'%' IDENTIFIED BY '123456';FLUSH PRIVILEGES;"  7 sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config && ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key  8 echo 123456 | passwd --stdin root  9 /usr/sbin/sshd -D

尝试生成镜像

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

生成完成,尝试启动容器

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

通过 ssh 服务连接到容器

[root@node1 mysql-Dockerfile]# ssh 192.168.2.11 -p 20002The 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:83:03:48:1e:51:77:25:37:20:bc.Are you sure you want to continue connecting (yes/no)? yesWarning: 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 -p123456Welcome to the MariaDB monitor.  Commands end with ; or g.Your MariaDB connection id is 3Server version: 5.5.44-MariaDB MariaDB ServerCopyright (c) 2000, 2015, 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               |+--------------------+4 rows in set (0.00 sec)

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

[root@node1 mysql-Dockerfile]# docker ps CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                             NAMESc0988d8c8eb2        mariadb:centos7     "/mysql.sh"              2 minutes ago       Up 2 minutes        3306/tcp, 0.0.0.0:20002->22/tcp   db7d5c54ba0cf1        web:centos7         "/usr/sbin/httpd -f /"   58 minutes ago      Up 58 minutes       0.0.0.0:80->80/tcp                web[root@node1 mysql-Dockerfile]# docker stop webweb[root@node1 mysql-Dockerfile]# docker rm webweb

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

[root@node1 mysql-Dockerfile]# docker run -it -p 80:80 --link=db --name web web:centos7 /bin/bash[root@f310c805bdfc /]# yum install mariadb -y[root@f310c805bdfc /]# mysql -uroot -p123456 -h dbWelcome to the MariaDB monitor.  Commands end with ; or g.Your MariaDB connection id is 5Server version: 5.5.44-MariaDB MariaDB ServerCopyright (c) 2000, 2015, 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               |+--------------------+4 rows in set (0.00 sec)

连接 mysql 容器成功。

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

[root@node1 web-Dockerfile]# docker run -d -p 80:80 -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                             NAMES4a36ee3781da        web:centos7         "/usr/sbin/httpd -f /"   8 seconds ago       Up 7 seconds        0.0.0.0:80->80/tcp                web50dba9d34f51        mariadb:centos7     "/mysql.sh"              9 minutes ago       U
p 9 mi
nutes        3306/tcp, 0.0.0.0:20002->22/tcp   db

安装 WordPress web 程序

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

浏览器访问

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

复制代码

[root@node1 myweb]# ssh 192.168.2.11 -p 20002root@192.168.2.11's password: [root@50dba9d34f51 ~]# mysql -uroot -p123456Welcome to the MariaDB monitor.  Commands end with ; or g.Your MariaDB connection id is 6Server version: 5.5.44-MariaDB MariaDB ServerCopyright (c) 2000, 2015, 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, 1 row affected (0.09 sec)MariaDB [(none)]> GRANT ALL ON WordPress.* TO 'WordPress'@'%' IDENTIFIED BY 'WordPress';Query OK, 0 rows affected (0.03 sec)MariaDB [(none)]> FLUSH PRIVILEGES;Query OK, 0 rows affected (0.02 sec)MariaDB [(none)]> Bye

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

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