设为首页 收藏本站
查看: 700|回复: 0

[经验分享] 【系列6】使用Dockerfile创建带LAMP的Centos Docker镜像

[复制链接]

尚未签到

发表于 2018-5-26 13:29:04 | 显示全部楼层 |阅读模式
  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: &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>


③ 部署自己的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 [&quot;/run.sh&quot;]
创建镜像,命名为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(&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命令可以获取到这个密码:
[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
========================================================================

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-481403-1-1.html 上篇帖子: Docker day2 Docker基本命令 下篇帖子: 在 docker 之间导出导入镜像的方法
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表