前面讲到了创建MariaDB,这次在前面的基础上搭建phpMyAdmin服务,以便友好的管理数据库MariaDB。MariaDB的docker独立出来,这样方便管理,易于扩展。这次我们基于Dockerfile方式创建剩余的服务,Dockerfile也是官方推荐的创建镜像方法。
一、创建Apche+PHP+phpMyAdmin镜像
首先启动MariaDB的容器。在上一节中我们知道数据库的用户名是root,密码是123456,但是我们还缺少数据库的ip地址,我们使用如下方法获取docker实例的ip地址,是172.17.0.3
[root@test01 ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES2e78c466919e liuxin/centos-mariadb:v1 "/root/run.sh" 11 hours ago Up 3 hours 0.0.0.0:13306->3306/tcp dreamy_bell [root@test01 ~]# docker inspect --format '{{ .NetworkSettings.IPAddress }}' 2e78c466919e172.17.0.3[root@test01 ~]#
首先创建目录和文件
[root@test01 ~]# mkdir phpMyAdmin_dockerfile && cd phpMyAdmin_dockerfile[root@test01 phpMyAdmin_dockerfile]# lsconfig.inc.php Dockerfile httpd_init.sh phptest.php run.sh
编辑Dockerfile文件,内容如下。具体每行的意思已经添加到注释中了
[root@test01 phpMyAdmin_dockerfile]# cat Dockerfile ##使用的基础镜像FROM centos:7.4.1708#添加作者信息MAINTAINER liuxin 842887233@qq.com#工作目录WORKDIR /root/#安装httpd和php,并忽略错误RUN yum -y install httpd RUN yum -y install php#安装mariadb客户端和php-mysqlndRUN yum -y install mariadb RUN yum -y install php-mysqlnd RUN yum -y install php-mbstring#安装phpmyadminRUN yum -y install epel-release RUN yum -y install phpmyadmin#设置环境变量,便于管理ENV MARIADB_ADDR 172.17.0.3ENV MARIADB_PORT 3306ENV MARIADB_USER root ENV MARIADB_PASS 123456#让容器支持中文ENV LC_ALL en_US.UTF-8#配置phpmyadmin#ADD phpMyAdmin.conf /etc/httpd/conf.d/phpMyAdmin.confADD config.inc.php /etc/phpMyAdmin/config.inc.php RUN chown root:apache /etc/phpMyAdmin/config.inc.php RUN chmod 640 /etc/phpMyAdmin/config.inc.php#添加php测试界面ADD phptest.php /var/www/html/phptest.php#初始化httpd服务ADD httpd_init.sh /root/httpd_init.sh RUN chmod 775 /root/httpd_init.sh RUN /root/httpd_init.sh#导出端口EXPOSE 80#添加启动文件ADD run.sh /root/run.sh RUN chmod 775 /root/run.sh#设置默认启动命令CMD ["/root/run.sh"]
配置phpmyadmin的配置文件,这里我直接将写好的配置文件上传,然后修改文件权限。也可以通过脚本来配置,不过比较麻烦,不如这个简单。改动的配置如下
设置phpmyadmin的登录密码
$cfg['blowfish_secret'] = '123456'; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */
设置连接数据库的账号、密码、ip和端口
$cfg['Servers'][$i]['host'] = '172.17.0.3'; // MySQL hostname or IP address $cfg['Servers'][$i]['port'] = ''; // MySQL port - leave blank for default port $cfg['Servers'][$i]['user'] = 'root'; // MySQL user $cfg['Servers'][$i]['password'] = '123456'; // MySQL password (only needed
使用脚本配置httpd
[root@test01 phpMyAdmin_dockerfile]# cat httpd_init.sh #!/bin/shsed -i 's/#ServerName www.example.com:80/ServerName localhost:80/g' /etc/httpd/conf/httpd.conf sed -i '/Require ip/d' /etc/httpd/conf.d/phpMyAdmin.conf sed -i '/<RequireAny>/a Require all granted' /etc/httpd/conf.d/phpMyAdmin.conf
添加一个php测试界面
[root@test01 phpMyAdmin_dockerfile]# cat phptest.php <?php phpinfo();?>
启动脚本
[root@test01 phpMyAdmin_dockerfile]# cat run.sh #!/bin/shhttpdwhile true;do sleep 1000;done
创建镜像
[root@test01 phpMyAdmin_dockerfile]# docker build -t liuxin/centos-phpmyadmin:v1 ./Sending build context to Docker daemon 14.34 kB Step 1 : FROM centos:7.4.1708 ---> 3afd47092a0e . . 省略... . . Step 26 : CMD /root/run.sh ---> Using cache ---> 19e52b9aceab Successfully built 19e52b9aceab [root@test01 phpMyAdmin_dockerfile]#
二、创建并启动容器
映射,物理机端口8080到容器的80端口
[root@test01 phpMyAdmin_dockerfile]# docker run -d -p 8080:80 liuxin/centos-phpmyadmin:v1 /root/run.sha5a54528879ea95e9bf98a7ba3fbf6236b26f3a54a5305738a4ee770164f105b [root@test01 phpMyAdmin_dockerfile]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a5a54528879e liuxin/centos-phpmyadmin:v1 "/root/run.sh" 56 seconds ago Up 54 seconds 0.0.0.0:8080->80/tcp sick_ride2e78c466919e liuxin/centos-mariadb:v1 "/root/run.sh" 12 hours ago Up 4 hours 0.0.0.0:13306->3306/tcp dreamy_bell [root@test01 phpMyAdmin_dockerfile]#
三、验证
打开web浏览器,输入测试的网页
php测试界面
输入phpmyadmin的地址,并输入账号密码,看能否登录
phpMyAdmin的登录界面
登录成功,大功告成
phpMyAdmin主界面
作者:小小运维
链接:https://www.jianshu.com/p/daaf43e55d4f