1. 程式人生 > >【系列6】使用Dockerfile創建帶LAMP的Centos Docker鏡像

【系列6】使用Dockerfile創建帶LAMP的Centos Docker鏡像

dockerfile創建帶lamp

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>
技術分享圖片

③ 部署自己的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
========================================================================

【系列6】使用Dockerfile創建帶LAMP的Centos Docker鏡像