在前一篇博文用Podman搭建LAMP开发环境的容器(二) – 让数据库保存在本地中,我在之前的MariaDB上实现了数据库保存到本地。虽然说已经是可以使用了,但是每次访问数据库都要进入容器,然后通过命令行方式的客户端进行访问。这样显然不是很方便,于是我想在这个容器上加一个phpMyAdmin。如果成功的话,就可以比较方便的操作数据库了。想到了就去做,马上到phpMyAdmin网站上是下载。
我看主页上推荐下载的版本是5.2.1的。看到版本我突然意识到:要看看这个版本的基本需求是什么,因为我安排的php的版本是7.4的,在现在说来应该算是比较就的版本。不知道是不是能够满足phpMyAdmin5.2.1的需求。
在phpMyAdmin 5.2.0的发行说明中,我看到需要PHP7.2或更高的版本,我装的是7.4的,看来是满足的。
不过应该可能还需要一些PHP扩展的支持。再详细看看:
果然,还是需要不是扩展支持的。看来要先修改Containerfile文件,安装所必须的PHP扩展。在apt install 增加安装这些扩展:
RUN apt update && \
apt install -y locales \
apache2 \
php7.4 \
php7.4-mysql \
php7.4-mbstring \
php7.4-zip \
php7.4-gd \
php7.4-xml \
php7.4-curl \
libapache2-mod-php7.4 \
mariadb-server && \
apt clean && \
rm -rf /var/lib/apt/lists/*
然后下载“phpMyAdmin-5.2.1-all-languages.tar.gz”文件到Containerfile文件所在的文件夹。在Containerfile中加入以下代码,把phpMyAdmin解压到/opt/php_my_admin目录下:
ADD phpMyAdmin-5.2.1-all-languages.tar.gz /opt/php_my_admin
再配置一个Apache站点监听8081端口,专门用于phpMyAdmin。新建一个010-phpmyadmin.conf文件,并输入以下内容:
Listen 8081
<VirtualHost *:8081>
ServerAdmin webmaster@localhost
DocumentRoot /opt/php_my_admin/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory "/opt/php_my_admin/">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
再修改Containerfile文件,最后修改成下面的样子:
FROM localhost/ubuntu:20.04
ARG DEBIAN_FRONTEND=noninteractive
ENV TZ=Asia/Shanghai
RUN apt update && \
apt install -y locales \
apache2 \
php7.4 \
php7.4-mysql \
php7.4-mbstring \
php7.4-zip \
php7.4-gd \
php7.4-xml \
php7.4-curl \
libapache2-mod-php7.4 \
mariadb-server && \
apt clean && \
rm -rf /var/lib/apt/lists/*
RUN localedef -i en_US -c -f UTF-8 -A /usr/share/locale/locale.alias en_US.UTF-8
RUN localedef -i zh_CN -c -f UTF-8 -A /usr/share/locale/locale.alias zh_CN.UTF-8
ENV LANG=zh_CN.utf8
ADD phpMyAdmin-5.2.1-all-languages.tar.gz /opt/php_my_admin
COPY entrypoint.sh /usr/local/bin
RUN chmod 755 /usr/local/bin/entrypoint.sh
COPY 000-default.conf /etc/apache2/sites-available/
RUN chmod 544 /etc/apache2/sites-available/000-default.conf
COPY 010-phpmyadmin.conf /etc/apache2/sites-available/
RUN chmod 544 /etc/apache2/sites-available/010-phpmyadmin.conf && \
ln -s /etc/apache2/sites-available/010-phpmyadmin.conf /etc/apache2/site-enabled/010-phpmyadmin.conf
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
EXPOSE 80
EXPOSE 8081
CMD ["/bin/bash"]
重新构建容器镜像:
E:\containers\lamp>podman build . -t lamp:ubuntu20.04
到测试项目文件夹运行容器看看:
E:\test>podman run --rm -it -v ./:/workspaces/myproject -v ./database:/var/lib/mysql -p 8080:80 -p 8081:8081 -e MY_DOCUM
ENT_ROOT=/workspaces/myprojects/src/public lamp:ubuntu20.04
AH00112: Warning: DocumentRoot [/workspaces/myprojects/src/public] does not exist
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 10.88.0.98. Set the 'ServerName' directive globally to suppress this message
* Starting MariaDB database server mysqld [ OK ]
root@e595d8db7038:/#
好像是启动成功了,在浏览器打开http://localhost:8081看看:
不对,phpMyAdmin解压后还有一级“phpMyAdmin-5.2.1-all-languages”目录,修改一下010-phpmyadmin.conf文件:
Listen 8081
<VirtualHost *:8081>
ServerAdmin webmaster@localhost
DocumentRoot /opt/php_my_admin/phpMyAdmin-5.2.1-all-languages
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory "/opt/php_my_admin/phpMyAdmin-5.2.1-all-languages">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
重新构建容器镜像、启动容器后再访问http://localhost:8081:
出来了,用户名输入:root,密码为空,登录试试:
看来用空密码不行。
经过一轮百度,终于找到了解决方案。先在Containerfile所在文件夹下新建一个phpmyadmin_config.inc.php文件,并输入以下内容:
<?php
$i = 0;
/* Server: localhost [1] */
$i++;
$cfg['Servers'][$i]['verbose'] = 'localhost';
$cfg['Servers'][$i]['host'] = 'localhost';
$cfg['Servers'][$i]['port'] = 3306;
$cfg['Servers'][$i]['socket'] = '/run/mysqld/mysqld.sock';
$cfg['Servers'][$i]['auth_type'] = 'cookie';
$cfg['Servers'][$i]['user'] = 'root';
$cfg['Servers'][$i]['password'] = '';
$cfg['Servers'][$i]['AllowNoPassword'] = true;
/* End of servers configuration */
$cfg['blowfish_secret'] = \sodium_hex2bin('abd9cf33be896124b3966e49510b5d917d25ec57dcca4fc4076aa9abc64efc1e');
$cfg['DefaultLang'] = 'zh_CN';
$cfg['ServerDefault'] = 1;
$cfg['UploadDir'] = '';
$cfg['SaveDir'] = '';
然后在Containerfile文件中加入下面两行:
COPY phpmyadmin_config.inc.php /opt/php_my_admin/phpMyAdmin-5.2.1-all-languages/config.inc.php
RUN chmod 544 /opt/php_my_admin/phpMyAdmin-5.2.1-all-languages/config.inc.php
重新构建容器镜像、启动容器后再访问http://localhost:8081,用户名输入:root,密码为空,再登录试试:
终于登录成功了!!!!