分类: LINUX
2019-06-28 11:11:08
导读 | 前面讲到了创建MariaDB,这次在前面的基础上搭建phpMyAdmin服务,以便友好的管理数据库MariaDB。MariaDB的docker独立出来,这样方便管理,易于扩展。这次我们基于Dockerfile方式创建剩余的服务,Dockerfile也是官方推荐的创建镜像方法。 |
在上一节中我们知道数据库的用户名是root,密码是123456,但是我们还缺少数据库的ip地址,我们使用如下方法获取docker实例的ip地址,是172.17.0.3
[root@test01 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2e78c466919e 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 }}' 2e78c466919e
172.17.0.3
[root@test01 ~]#
[root@test01 ~]# mkdir phpMyAdmin_dockerfile && cd phpMyAdmin_dockerfile [root@test01 phpMyAdmin_dockerfile]# ls config.inc.php Dockerfile httpd_init.sh phptest.php run.sh
内容如下。具体每行的意思已经添加到注释中了
[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-mysqlnd RUN yum -y install mariadb RUN yum -y install php-mysqlnd RUN yum -y install php-mbstring #安装phpmyadmin RUN yum -y install epel-release RUN yum -y install phpmyadmin #设置环境变量,便于管理 ENV MARIADB_ADDR 172.17.0.3 ENV MARIADB_PORT 3306 ENV MARIADB_USER root ENV MARIADB_PASS 123456 #让容器支持中文 ENV LC_ALL en_US.UTF-8 #配置phpmyadmin #ADD phpMyAdmin.conf /etc/httpd/conf.d/phpMyAdmin.conf ADD 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的登录密码
$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
[root@test01 phpMyAdmin_dockerfile]# cat httpd_init.sh #!/bin/sh sed -i 's/#ServerName localhost:80/g' /etc/httpd/conf/httpd.conf sed -i '/Require ip/d' /etc/httpd/conf.d/phpMyAdmin.conf sed -i '//a Require all granted' /etc/httpd/conf.d/phpMyAdmin.conf
[root@test01 phpMyAdmin_dockerfile]# cat phptest.php < ?php phpinfo(); ?>
[root@test01 phpMyAdmin_dockerfile]# cat run.sh #!/bin/sh httpd while 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.sh a5a54528879ea95e9bf98a7ba3fbf6236b26f3a54a5305738a4ee770164f105b [root@test01 phpMyAdmin_dockerfile]# docker ps CONTAINER 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_ride 2e78c466919e 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]#