LAMP值的Linux (操作系统)、ApacheHTTP服务器、MySQL(有时也指MariaDB,数据库软件)和PHP(有时也是指Perl或Python)的组合方案,一般很适合用来建立Web服务器环境。
① 下载LAMP镜像:
下面介绍如何使用Docker来搭建一个包含LAMP组件的容器。
[root@docker1 ~]# docker search -s 10 lamp
Flag --stars has been deprecated, use --filter=stars=3 instead
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/linode/lamp LAMP on Ubuntu 14.04.1 LTS Container 121
docker.io docker.io/tutum/lamp Out-of-the-box LAMP image (PHP+MySQL) 68
docker.io docker.io/greyltc/lamp a super secure, up-to-date and lightweight... 65 [OK]
docker.io docker.io/reinblau/lamp [Deprecated]Dockerfile for PHP-Projects wi... 28 [OK]
......
[root@docker1 ~]# docker pull tutum/lamp
Using default tag: latest
Trying to pull repository docker.io/tutum/lamp ...
latest: Pulling from docker.io/tutum/lamp
④ 在PHP程序中连接数据库
1. 在容器中访问MySQL数据库
下载的tutum/lamp镜像中的MySQL数据库已带有默认的root用户,本地连接可以不使用密码,所以在代码中访问数据库的实现非常简单:
<?php
$mysql = new mysqli("localhost", "root");
echo "MySQL Server info: ".$mysql->host_info;
?>
2. 在容器外访问MySQL数据库
默认的MySQL数据库不支持root用户远程登录,因此在容器外无法直接通过root用户访问MySQL数据库。
当第一次使用tutum/lamp镜像启动容器的时候,它会自动创建一个叫admin的MySQL用户,并生成一个随机密码,使用docker logs命令可以获取到这个密码:
[root@docker1 LAMP]# docker logs ec3c2c2b04dd
=> An empty or uninitialized MySQL volume is detected in /var/lib/mysql
=> Installing MySQL ...
=> Done!
=> Waiting for confirmation of MySQL service startup
=> Creating MySQL admin user with random password
=> Done!
========================================================================
You can now connect to this MySQL Server using:
mysql -uadmin -p8fMyJd458mqd -h<host> -P<port>
Please remember to change the above password as soon as possible!
MySQL user 'root' has no password but only allows local connections
========================================================================