Docker安装Apache与运行简单的web服务——httpd helloworld
Docker運行簡單的web服務——httpd helloworld目錄【閱讀時間:約5分鐘】
- 一、Docker簡介
 - 二、Docker的安裝與配置【CentOS環境】
 - 三、Docker運行簡單的web服務——httpd helloworld
 - 四、References
 
一、Docker簡介
Docker 是一個開源的應用容器引擎,讓開發者可以打包他們的應用以及依賴包到一個可移植的鏡像中,然后發布到任何流行的 Linux或Windows 機器上,也可以實現虛擬化。容器是完全使用沙箱機制,相互之間不會有任何接口。
 觀察Docker圖標,其實很形象的解釋了什么是Docker。在沒有使用集裝箱的情況下,我們需要考慮不同形狀、尺寸的貨物怎么安放,貨物與貨物之間是否能堆疊,這無疑是很繁瑣的事情。現在有了集裝箱(容器),我們就能將不同的貨物放入集裝箱之內這樣就能以同樣的方式來存放貨物,實現了標準化。
Docker采用了集裝箱原理,我們將程序放入到容器中,實現“一次封裝,到處運行”,只要開發環境能跑,在其他任何流行的機器上都能運行。并且將程序丟入到容器中,很好的實現了程序與程序之間的隔離,避免了類似Java開發的程序和.net開發的程序安裝在一個服務器上需要很多調試,有時還會產生沖突的情況。
Docker的應用場景:
Web 應用的自動化打包和發布。
自動化測試和持續集成、發布。
在服務型環境中部署和調整數據庫或其他的后臺應用。
從頭編譯或者擴展現有的 OpenShift 或 Cloud Foundry 平臺來搭建自己的 PaaS 環境。
二、Docker的安裝與配置【CentOS環境】
基本安裝過程,請參考我之前的一篇博客:博客
由于docker pull會出現timeout error的情況,一般需要更換docker鏡像源為國內的鏡像源,請參考我之前的一篇博客:博客
 
三、Docker運行簡單的web服務——httpd helloworld
在之前我們曾經使用Docker運行過一個最簡單的helloworld映像:
[root@localhost henryhzy]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
Digest: sha256:1a523af650137b8accdaed439c17d684df61ee4d74feac151b5b337bd29e7eec
Status: Downloaded newer image for hello-world:latestHello from Docker!
This message shows that your installation appears to be working correctly.To generate this message, Docker took the following steps:1. The Docker client contacted the Docker daemon.2. The Docker daemon pulled the "hello-world" image from the Docker Hub.(amd64)3. The Docker daemon created a new container from that image which runs theexecutable that produces the output you are currently reading.4. The Docker daemon streamed that output to the Docker client, which sent itto your terminal.To try something more ambitious, you can run an Ubuntu container with:$ docker run -it ubuntu bashShare images, automate workflows, and more with a free Docker ID:https://hub.docker.com/For more examples and ideas, visit:https://docs.docker.com/get-started/
 
而最近在學習web和docker的相關操作,因此想順便嘗試一下簡單的web服務,這里我使用docker Apache與httpd。
首先安裝Apache與httpd:
[root@localhost henryhzy]# docker pull httpd
Using default tag: latest
latest: Pulling from library/httpd
6ec7b7d162b2: Already exists 
17e233bac21e: Pulling fs layer 
130aad5bf43a: Pulling fs layer 
81d0a34533d4: Pulling fs layer 
da240d12a8a4: Waiting 
latest: Pulling from library/httpd
6ec7b7d162b2: Already exists 
17e233bac21e: Pull complete 
130aad5bf43a: Pull complete 
81d0a34533d4: Pull complete 
da240d12a8a4: Pull complete 
Digest: sha256:a3a2886ec250194804974932eaf4a4ba2b77c4e7d551ddb63b01068bf70f4120
Status: Downloaded newer image for httpd:latest
docker.io/library/httpd:latest
 
查看鏡像版本:
[root@localhost henryhzy]# docker images httpd
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
httpd        latest    dd85cdbb9987   10 days ago   138MB
[root@localhost henryhzy]# 
 
運行鏡像如下:
docker run -itd \
--name test_web_server \
-p 8085:80 \
-v /home/user/website/:/usr/local/apache2/htdocs/ \
httpd:latest
 
-p 8085:80,意思是端口號的映射,<host_port>:<容器內apache的port>
-v /home/user/website/:/usr/local/apache2/htdocs/, 這是docker重要的volume的概念。<host_路徑>:<容器內的路徑>。
查看docker容器確認httpd已啟動:
docker ps
 
 
此時訪問 http://<YOUR_IP>:8085即可到達一個web page
 
在apache的folder里面放一個編寫好的網頁,之后就可以通過這個端口8085來訪問,由此便完成了安裝Apache與運行簡單的web服務的流程。
下面給出一個具體的helloworld網頁的例子,在你的host,到/home/user/website/文件夾下,建立一個helloworld.html網頁,這個文件會自動映射到容器內的/usr/local/apache2/htdocs/下,被apache發布出來。
cd /home/user/website
vi helloworld.html
 
helloworld.html內容如下:
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>hello world web page</title>
</head>
<body><h1>Hello from HenryHZY 18342026!</h1>
</body>
</html> 
然后保存,訪問 http://<YOUR_IP>:8085/helloworld.html:
 
大體步驟如上,just enjoy it~
四、References
- Docker 安裝 Apache
 - Docker Apache建一個hello world的Web
 
總結
以上是生活随笔為你收集整理的Docker安装Apache与运行简单的web服务——httpd helloworld的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: Docker的安装、镜像源更换与简单应用
 - 下一篇: 【阶段小结】协同开发——这学期的Git使