LAMP介紹
LAMP 指的 Linux(操作系統)、ApacheHTTP 服務器,MySQL(有時也指MariaDB,數據庫軟件) 和 PHP(有時也是指 Perl 或 Python) 的第一個字母,一般用來建立 web 服務器。 雖然這些開放源代碼程序本身并不是專門設計成同另幾個程序一起工作的,但由于它們的免費和開源,這個組合開始流行(大多數Linux發行版本***了這些軟件)。當一起使用的時候,它們表現的像一個具有活力的解決方案包。
下面介紹如何使用docker來搭建一個包含lamp組件的容器:
從網站上 pull 一個 lamp 鏡像
官方的倉里沒有標 OFFICIAL 的 lamp 的鏡像,不過 「tutum」的鏡像做的非常好,我們可以直接 pull 他們的鏡像來完成我們的操作。
1
|
2
3
4
|
"lang-bash" >core@localhost ~ /base $ docker pull tutum /lamp Pulling repository tutum /lamp b32789c7d66: Download complete ... |
使用默認方式啟動 lamp 容器
1
|
2
3
4
5
6
7
8
9
10
|
"lang-bash" >core@localhost ~ /base $ docker run "hljs-operator" >-d -p : -p : tutum /lamp ee00c97a5cdefb985baf826c16723f333aa5edddee4072a5107c724ad09f10d core@localhost ~ /base $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ee00c97a5 "hljs-built_in" > cd tutum /lamp :latest "/run.sh" seconds ago Up seconds 0.0.0.0:-> /tcp , 0.0.0.0:-> /tcp lonely_davinci e3c136d76b44 tutum /tomcat : "hljs-number" >8.0 "hljs-string" > "/run.sh" minutes ago Up minutes "hljs-number" >0.0. "hljs-number" >0.0:-> /tcp tomcat001 fe9e65aaf58c dl.dockerpool.com: /mysql : "hljs-number" >5.7 "hljs-string" >" /entrypoint .sh mysq 51 minutes ago Up 51 minutes 3306 /tcp db001,tomcat001 /tomysql core@localhost ~ /base $ curl http: //127 .0.0.1:8080 |
#使用curl可以查看到默認的應用已經啟動
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
< html > < head > < title >Hello world!</ title > < style > body { background-color: white; text-align: center; padding: 50px; font-family: "Open Sans "hljs-string">","Helvetica Neue "hljs-string">",Helvetica,Arial,sans-serif; } #logo { margin-bottom: 40px; } </ style > </ head > < body > < img id = "logo " hljs-string">" src=" http://upload.server110.com/image/20141116/205R55010-0.png " /> < h1 >Hello world!</ h1 > < h2 >MySQL Server version: 5.5.38-0ubuntu0.14.04.1</ h2 > </ body > </ html > |
部署自己的 PHP 應用
默認的容器啟動了一個 helloword 應用,我們可以使用 dockerfile 創建另外一個鏡像來部署我們自己的應用程序,dockerfile 的詳細語法將在后面章節介紹。
1
|
2
3
4
5
|
core@localhost ~ $ mkdir php core@localhost ~ $ cd php/ core@localhost ~ /php $ touch Dockerfile core@localhost ~ /php $ vi Dockerfile core@localhost ~ /php $ docker build -t dockerpool /my-lamp-app . |
Dockerfile 內容如下:
1
|
2
3
4
5
|
FROM tutum /lamp :latest RUN rm -fr /app && git clone https: //github .com /username/customapp .git /app #這里替換 https://github.com/username/customapp.git 地址為你自己的項目地址 EXPOSE 80 3306 CMD [ "/run.sh" ] |
再次啟動自己的容器就完成部署了
1
|
2
3
4
5
|
"lang-bash" >core@localhost ~ /php $ docker stop ee ee core@localhost ~ /php $ docker rm ee ee core@localhost ~ /php $ docker run "hljs-operator" >-d -p : -p : dockerpool /my-lamp-app |
使用 curl看下自己的應用程序是不是已經正確啟動了吧!
1
|
|
curl http: //localhost/ |
在 php 程序中連接數據庫 在容器中訪問 mysql 數據庫
這個鏡像的 mysql 數據庫有個默認的 root 用戶,本地連接時可以不用密碼,所以在代碼訪問非常簡單。
1
|
2
3
4
|
"hljs-preprocessor" ><?php $mysql = "hljs-keyword" >new mysqli( "hljs-string" > "localhost" , "hljs-string" > "root" ); echo "hljs-string" > "MySQL Server info: " . "hljs-variable" >$mysql "hljs-variable" >->host_info; ?> |
在容器外部訪問 mysql 數據庫
當我們第一次以 tutum/lamp 鏡像啟動容器的時候,它會自動創建一個叫 admin 的 mysql 用戶,并生成一個隨機密碼,使用「docker logs +容器ID」可以獲取到這個密碼。
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
"lang-bash" >core@localhost ~ /php $ docker logs cb => An empty or uninitialized MySQL volume is detected in /var/lib/mysql => Installing MySQL ... => Done! => Waiting "hljs-keyword" > 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 -p2Ijg6gvmM0N3 -h<host> -P<port> Please remember to change the above password as soon as possible! MySQL user "hljs-string" > 'root' has no password but only allows local connections ======================================================================== |
默認的 root 用戶無法遠程登陸,所以要使用 admin 用戶,它同樣具有 root 權限。
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家用docker搭建LAMP能有所幫助,如果有疑問大家可以留言交流。