Apache+PHP+MySQL架构
架构使用
使用centos:centos7.5.1804镜像为基础镜像,构建Apache+PHP+MySQL架构的网页访问服务php:latest数据库使用本机安装MariaDB环境,相关权限请自行授权;编辑Dockerfile所需要的文件build_table.sh、run.sh、test.php
Dockerfile编写要求为:
(1)使用提供的centos:latest基础镜像,作者为xianidan。
(2)删除系统yum源文件,采用ftp.repo作为系统默认yum源文件,设置 /opt/为工作目录。
(3)安装httpd、php、php-mysql、mysql软件包,在容器内新建/var/log/httpd /var/www/html目录。
(4)设置环境变量MYSQL_ADDR、MYSQL_USER、MYSQL_PASS,系统编码采用en_US.UTF-8格式。 A
(5)添加test.php到/var/www/html目录,run.sh到/opt目录,build_table.sh 到/opt目录,设置run.sh、build_table.sh脚本具有可执行权限。
(6)执行/opt/build_table.sh脚本,如果/bin/bash不能直接调用,可使用绝对路径。
(7)暴露80端口,开机运行run.sh脚本。
保证master可以上外网,安装docker-ce的环境,利用Dockerfile编排完成容器的web小页面。
本机安装Docker-ce的环境
安装需要的软件包 $ yum install -y yum-utils device-mapper-persistent-data lvm2
使用阿里的Docker-ce源 $ yum-config-manager --add-repo http://mirrors. aliyun. com/docker-ce/linux/centos/docker-ce.repo
安装DOCKER CE $ yum -y install docker-ce
配置镜像加速 $ vi /etc/docker/daemon.json { "registry-mirrors": ["https://ably8t50.mirror.aliyuncs.com"] }
重启服务 $ systemctl daemon-reload $ systemctl restart docker
|
查看当前的镜像
[root@localhost test]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE |
拉取一个centos:centos7.5.1804镜像做实验
[root@localhost test]# docker pull centos:centos7.5.1804 centos7.5.1804: Pulling from library/centos Digest: sha256:7a45e4a1efbaafc1d9aa89925b6fdb33288a96d35ea0581412316e2f0ad3720a Status: Image is up to date for centos:centos7.5.1804 docker.io/library/centos:centos7.5.1804
[root@localhost test]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos centos7.5.1804 cf49811e3cdb 2 years ago 200MB
|
然后使用本地的yum源,1804的iso加上Docker的源。(不适用外源)
[root@localhost test]# mount /dev/sr0 /opt/ [root@localhost test]# mkdir /opt/centos [root@localhost test]# umonut /mnt [root@localhost test]# cat /etc/yum.repos.d/local.repo [Docker] name=Docker baseurl=file:///opt/Docker gpgcheck=0 enabled=1 [centos] name=centos baseurl=file:///opt/centos gpgcheck=0 enabled=1
配置vsftpd共享ftp源,配置本地数据库,让容器的镜像使用本机的数据库
[root@localhost test]# yum install -y vsftpd [root@localhost test]# echo anon_root=/opt >> /etc/vsftpd/vsftpd.conf [root@localhost test]# systemctl restart vsftpd [root@localhost test]# yum install -y mariadb-server [root@localhost test]# systemctl start mairdb [root@localhost test]# mysqladmin -uroot password 000000 [root@localhost test]# mysql -uroot MariaDB [(none)]> grant all privileges on *.* to root@'%' identified by "000000"; Query OK, 0 rows affected (0.01 sec) MariaDB [(none)]> grant replication slave on *.* to 'root'@'localhost' identified by '000000'; Query OK, 0 rows affected (0.00 sec)
|
编写Dockerfile文件
[root@localhost ]# mkdir test && cd test [root@localhost test]# cat Dockerfile
FROM centos:centos7.5.1804 MAINTAINER xiandian RUN rm -rvf /etc/yum.repos.d/* ADD ftp.repo /etc/yum.repos.d/ WORKDIR /opt/ RUN yum install httpd php mariadb-server php-mysql -y RUN sed -i "s/#ServerName.*/ServerName localhost/" /etc/httpd/conf/httpd.conf RUN mkdir -p /var/log/httpd RUN mkdir -p /var/www/html ENV MYSQL_ADDR 192.168.100.20 ENV MYSQL_USER root ENV MYSQL_PASS 000000 ENV LC_ALL en_US.UTF-8 ADD test.php /var/www/html/test.php ADD run.sh /opt/run.sh ADD build_table.sh /opt/build_table.sh RUN chmod +x /opt/run.sh RUN chmod +x /opt/build_table.sh RUN /bin/bash /opt/build_table.sh EXPOSE 80 CMD /opt/run.sh
|
编写ftp.repo源
[root@localhost test]# vi ftp.repo
[Docker] name=Docker baseurl=ftp://192.168.137.130/Docker gpgcheck=0 enabled=1 [centos] name=centos baseurl=ftp://192.168.137.130/centos gpgcheck=0 enabled=1
|
使用提供的php文件
[root@localhost test]# cat test.php <table border='1'> <tr> <th>NAME</th> <th>YEARS</th> </tr><tr><td>xiandian</td><td>2018</td></tr><tr><td>xiandian</td><td>2019</td></tr></table> |
编写build_table.sh的shell脚本
[root@localhost test]# vi build_table.sh
#!/bin/bash mysql -h$MYSQL_ADDR -u$MYSQL_USER -p$MYSQL_PASS -e "create database chinaskills;" mysql -h$MYSQL_ADDR -u$MYSQL_USER -p$MYSQL_PASS -e "create table chinaskills.company(name char(20) not null,years int not null)DEFAULT CHARSET=utf8;" mysql -h$MYSQL_ADDR -u$MYSQL_USER -p$MYSQL_PASS -e "insert into chinaskills.company values('xiandian',2018),('xiandian',20 19);"
|
编写run.sh的shell脚本
[root@localhost test]# vi run.sh
#!/bin/bash httpd while true do sleep 1000 done
|
构建镜像
[root@localhost test]# docker build -t php .
|
使用Dockerfile文件构建镜像,然后使用该镜像运行容器,端口设置为80:80, 使用curl命令访问该容器首页http://IP/test.php
[root@localhost test]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE php latest 324e99cb9105 33 minutes ago 449MB centos centos7.5.1804 cf49811e3cdb 2 years ago 200MB
[root@loaclhost test]# docker run -itd --name php -p 80:80 php:latest 9c4f119350e23d1902e7bd9d008de079496437a297306217ae71d51853725d47 [root@master ~]# curl http://192.168.100.20:80/test.php <table border='1'> <tr> <th>NAME</th>
|
