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

       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

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端口:
[root@docker1 ~]# docker run -d -p 8080:80 -p 3306:3306 tutum/lamp
ec3c2c2b04ddda0110f6488ac4c2b958e5e834613d1c637bbaba8f628c6e461e
[root@docker1 ~]# 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命令测试,可以查看到默认的应用已经启动:
[root@docker1 ~]# 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: "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>
file://C:\Users\Sheng\AppData\Local\Temp\ct_tmp/1.png

③ 部署自己的PHP应用
默认的容器启动了一个helloword应用。读者可以基于此镜像,编辑Dockerfile来创建自定义LAMP应用镜像。
在宿主主机上创建新的工作目录LAMP:
[root@docker1 ~]# mkdir LAMP
[root@docker1 ~]# cd LAMP
[root@docker1 LAMP]# touch Dockerfile
在php目录下里面创建Dockerfile文件,内容为
[root@docker1 LAMP]# 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 ["/run.sh"]
创建镜像,命名为dockerpool/my-lamp-app:
[root@docker1 LAMP]# docker build -t dockerpool/my-lamp-app .
利用新创建镜像启动容器,注意启动时候指定-d参数,让容器后台运行:
[root@docker1 LAMP]# docker run -d -p 8080:80 -p 3306:3306 dockerpool/my-lamp-app
在本地主机上使用curl看一下自己的应用程序是不是已经正确启动:
[root@docker1 LAMP]# curl http://127.0.0.1:8080/

④ 在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

========================================================================


本文转自 Mr_sheng 51CTO博客,原文链接:http://blog.51cto.com/sf1314/2056639


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值