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 4 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 6 : EXPOSE 80
---> Running in cd581ad95fb0
---> 368d9fc0b1ce
Removing intermediate container cd581ad95fb0
Step 7 : 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 2 minutes ago 268 MB
docker.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 NAMES
7d5c54ba0cf1 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 web
80/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 = 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
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 images
REPOSITORY TAG IMAGE ID CREATED SIZE
mariadb 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 NAMES
c0988d8c8eb2 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 20002
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:83:03:48:1e:51:77:25:37:20: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 3
Server version: 5.5.44-MariaDB MariaDB Server
Copyright (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 NAMES
c0988d8c8eb2 mariadb:centos7 "/mysql.sh" 2 minutes ago Up 2 minutes 3306/tcp, 0.0.0.0:20002->22/tcp db
7d5c54ba0cf1 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 web
web
[root@node1 mysql-Dockerfile]# docker rm web
web
测试是否能够访问到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 db
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 5.5.44-MariaDB MariaDB Server
Copyright (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 NAMES
4a36ee3781da web:centos7 "/usr/sbin/httpd -f /" 8 seconds ago Up 7 seconds 0.0.0.0:80->80/tcp web
50dba9d34f51 mariadb:centos7 "/mysql.sh" 9 minutes ago Up 9 minutes 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 20002
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 6
Server version: 5.5.44-MariaDB MariaDB Server
Copyright (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 实现负载或者冗余。
分类: Docker