一 简介
LAMP 指的 Linux(操作系统)、ApacheHTTP 服务器,MySQL(有时也指MariaDB,数据库软件) 和 PHP(有时也是指 Perl 或 Python) 的第一个字母,一般用来建立 web 服务器。 虽然这些开放源代码程序本身并不是专门设计成同另几个程序一起工作的,但由于它们的免费和开源,这个组合开始流行(大多数Linux发行版本捆绑了这些软件)。当一起使用的时候,它们表现的像一个具有活力的解决方案包。下面介绍如何使用docker来搭建一个包含lamp组件的容器。
二 从网站上 pull 一个 LAMP 镜像
[root@localhost ~]# docker pull tutum/lamp
三 使用默认方式启动 LAMP 容器
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tutum/lamp latest 3d49e175ec00 21 months ago 427MB
[root@localhost ~]# docker run -d -p 8081:80 -p 3307:3306 3d
e921dbff538be427538f450631921bbd2c5f7ac9470e62d74edb8f9df8917ca7
[root@localhost ~]# curl http://192.168.0.107:8081
<html>
<head>
<title>Hello world!</title>
<style>
body {
background-color: white;
text-align: center;
padding: 50px;
font-family: "Open Sans","Helvetica Neue",Helvetica,Arial,sans-serif;
}
#logo {
margin-bottom: 40px;
}
</style>
</head>
<body>
<img id="logo" src="logo.png" />
<h1>Hello world!</h1>
<h2>MySQL Server version: 5.5.47-0ubuntu0.14.04.1</h2>
</body>
四 部署自己的PHP应用
默认的容器启动了一个 helloword 应用,我们可以使用 dockerfile 创建另外一个镜像来部署我们自己的应用程序。
core@localhost ~ $ mkdir php
core@localhost ~ $ cd php/
core@localhost ~/php $ touch Dockerfile
core@localhost ~/php $ vi Dockerfile
[root@localhost php]# curl 127.0.0.1:8082
This is index.html[root@localhost php]# cat Dockerfile
FROM tutum/lamp:latest
#RUN git clone https://github.com/cakin24/tata.git /app
#这里替换 https://github.com/username/customapp.git 地址为你自己的项目地址
EXPOSE 80 3306
CMD ["/run.sh"]
[root@localhost php]# docker build --no-cache -t dockerpool/my-lamp-app .
Sending build context to Docker daemon 121.9kB
Step 1/3 : FROM tutum/lamp:latest
---> 3d49e175ec00
Step 2/3 : EXPOSE 80 3306
---> Running in 6a59ef59f53f
---> 6b2cab096a4d
Removing intermediate container 6a59ef59f53f
Step 3/3 : CMD /run.sh
---> Running in dc8affce4715
---> 387b59df4876
Removing intermediate container dc8affce4715
Successfully built 387b59df4876
Successfully tagged dockerpool/my-lamp-app:latest
[root@localhost php]# git clone https://github.com/cakin24/tata.git
[root@localhost php]# docker run -d -p 8082:80 -p 3308:3306 -v /root/php/tata:/app dockerpool/my-lamp-app
6cbe46e323dbfa8455125f6ce85ed24fa2a16fbbe4f24df27739f29077ea773f
[root@localhost php]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6cbe46e323db dockerpool/my-lamp-app "/run.sh" 20 seconds ago Up 13 seconds 0.0.0.0:8082->80/tcp, 0.0.0.0:3308->3306/tcp stoic_ride
[root@localhost php]# curl 127.0.0.1:8082
This is index.html
五 在php中连接数据库
当我们第一次以 tutum/lamp 镜像启动容器的时候,它会自动创建一个叫 admin 的 mysql 用户,并生成一个随机密码,使用「docker logs +容器ID」可以获取到这个密码。
[root@localhost php]# docker logs 6cbe46e323db
=> An empty or uninitialized MySQL volume is detected in /var/lib/mysql
=> Installing MySQL ...
=> Done!
=> Waiting for confirmation of MySQL service startup
=> 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 -pepqjhdlTkpO0 -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
在tata目录中新建一个php文件
[root@localhost tata]# cat phpinfo.php
<?php
$mysql = new mysqli("localhost", "root");
echo "MySQL Server info: ".$mysql->host_info;
?>
如果访问这个页面: