zhangli-s 发表于 2018-5-26 13:29:04

【系列6】使用Dockerfile创建带LAMP的Centos Docker镜像

  LAMP值的Linux (操作系统)、ApacheHTTP服务器、MySQL(有时也指MariaDB,数据库软件)和PHP(有时也是指Perl或Python)的组合方案,一般很适合用来建立Web服务器环境。
① 下载LAMP镜像:
   下面介绍如何使用Docker来搭建一个包含LAMP组件的容器。
# 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                  
docker.io   docker.io/reinblau/lamp         Dockerfile for PHP-Projects wi...   28               
......
# docker pull tutum/lamp
Using default tag: latest
Trying to pull repository docker.io/tutum/lamp ...
latest: Pulling from docker.io/tutum/lamp

8387d9ff0016: Pull complete
3b52deaaf0ed: Pull complete
4bd501fad6de: Pull complete
a3ed95caeb02: Pull complete
790f0e8363b9: Pull complete
11f87572ad81: Pull complete
341e06373981: Pull complete
709079cecfb8: Pull complete
55bf9bbb788a: Pull complete
b41f3cfd3d47: Pull complete
70789ae370c5: Pull complete
43f2fd9a6779: Pull complete
6a0b3a1558bd: Pull complete
934438c9af31: Pull complete
1cfba20318ab: Pull complete
de7f3e54c21c: Pull complete
596da16c3b16: Pull complete
e94007c4319f: Pull complete
3c013e645156: Pull complete
Digest: sha256:d332e7e97606ac6407b0ba9ae9e9383c89d7e04c6f4853332e98f7d326408329

② 使用默认方式启动LAMP容器:
利用下载的镜像启动一个容器,并映射容器的8080端口和3306端口:
# docker run -d -p 8080:80 -p 3306:3306 tutum/lamp
ec3c2c2b04ddda0110f6488ac4c2b958e5e834613d1c637bbaba8f628c6e461e
# docker ps
CONTAINER ID      IMAGE               COMMAND             CREATED             STATUS      PORTS                                          NAMES
ec3c2c2b04dd      tutum/lamp          "/run.sh"         4 seconds ago       Up 3 seconds      0.0.0.0:3306->3306/tcp, 0.0.0.0:8080->80/tcp   hungry_leavitt

使用curl命令测试,可以查看到默认的应用已经启动:
# curl http://127.0.0.1:8080
返回的内容如下:
<html>
<head>
      <title>Hello world!</title>
      <style>
      body {
                background-color: white;
                text-align: center;
                padding: 50px;
                font-family: &quot;Open Sans&quot;,&quot;Helvetica Neue&quot;,Helvetica,Arial,sans-serif;
      }

      #logo {
                margin-bottom: 40px;
      }
      </style>
</head>
<body>
      <img id=&quot;logo&quot; src=&quot;logo.png&quot; />
      <h1>Hello world!</h1>
                        <h2>MySQL Server version: 5.5.47-0ubuntu0.14.04.1</h2>
      </body>
http://blog.51cto.com/static/js/ueditor1.4.3/themes/default/images/spacer.gif

③ 部署自己的PHP应用
默认的容器启动了一个helloword应用。读者可以基于此镜像,编辑Dockerfile来创建自定义LAMP应用镜像。
在宿主主机上创建新的工作目录LAMP:
# mkdir LAMP
# cd LAMP
# touch Dockerfile
在php目录下里面创建Dockerfile文件,内容为
# vi Dockerfile
FROM tutum/lamp:latest
RUN rm -fr /app && git clone https://github.com/username/customapp.git /app
#这里将https://github.com/username/customapp.git /app替换/app里面的文件
EXPOSE 80 3306
CMD [&quot;/run.sh&quot;]
创建镜像,命名为dockerpool/my-lamp-app:
# docker build -t dockerpool/my-lamp-app .
利用新创建镜像启动容器,注意启动时候指定-d参数,让容器后台运行:
# docker run -d -p 8080:80 -p 3306:3306 dockerpool/my-lamp-app
在本地主机上使用curl看一下自己的应用程序是不是已经正确启动:
# curl http://127.0.0.1:8080/

④ 在PHP程序中连接数据库
1. 在容器中访问MySQL数据库
下载的tutum/lamp镜像中的MySQL数据库已带有默认的root用户,本地连接可以不使用密码,所以在代码中访问数据库的实现非常简单:
<?php
$mysql = new mysqli(&quot;localhost&quot;, &quot;root&quot;);
echo &quot;MySQL Server info: &quot;.$mysql->host_info;
?>

2. 在容器外访问MySQL数据库
默认的MySQL数据库不支持root用户远程登录,因此在容器外无法直接通过root用户访问MySQL数据库。
当第一次使用tutum/lamp镜像启动容器的时候,它会自动创建一个叫admin的MySQL用户,并生成一个随机密码,使用docker logs命令可以获取到这个密码:
# 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
========================================================================
页: [1]
查看完整版本: 【系列6】使用Dockerfile创建带LAMP的Centos Docker镜像