1健爬,目的
在已經掌握docker安裝放仗、docker倉庫的基本使用、docker鏡像和容器的基本操作和相互轉化的基礎上匙铡,可嘗試通過docker搭建一個web服務器,便于分布式部署或快速移植web服務器碍粥。
通過本文的學習鳖眼,可以了解學習docker容器與宿主機的文件和端口映射,進一步熟練使用docker容器嚼摩。
2钦讳,修改容器,搭建簡單的web服務
安裝nginx
# apt-get install nginx
修改nginx配置文件
# vi /etc/nginx/conf.d/web.conf
# server的配置
server {
# 監(jiān)聽端口
listen 81;
# 項目的初始化頁面
location / {
root /home/visual/nginx_web/;
index index.html;
}
}
修改開機啟動項
# vi /etc/rc.local
####!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
service ssh start
service nginx start
/bin/bash
exit 0
3枕面,創(chuàng)建鏡像愿卒,便于再次啟動容器
通過commit操作創(chuàng)建docker鏡像文件,上篇文章已經講過潮秘,命令如下
kevin@apple:~/docker$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
568e5204fff3 ubuntu "/bin/sh -c 'while..." 40 hours ago Exited (137) 38 hours ago kind_khorana
00f561d97811 ubuntu "/bin/echo hello w..." 40 hours ago Exited (0) 40 hours ago nifty_mcnulty
93a1b9d39683 ubuntu "bash" 40 hours ago Exited (0) 5 seconds ago zealous_noether
abdc084f9821 hello-world "/hello" 41 hours ago Exited (0) 18 hours ago sleepy_clarke
kevin@apple:~/docker$ docker commit 93a1b9d39683 learn/nginx:v2
sha256:ab92edd21696770f1eb37e9229b6834094a8d3381e5b4e9edc620b7927004bb4
kevin@apple:~/docker$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
learn/nginx v2 ab92edd21696 5 seconds ago 370MB
learn/visual_init v1 56a4eab7dc5b 37 hours ago 321MB
registry.cn-beijing.aliyuncs.com/zhangsp/ai visual_init 56a4eab7dc5b 37 hours ago 321MB
ubuntu latest 14f60031763d 5 days ago 120MB
hello-world latest 1815c82652c0 5 weeks ago 1.84kB
4琼开,啟動新容器
使用新創(chuàng)建的鏡像learn/nginx:v2,啟動新容器
# docker run -it --name nginx_test -h docker-nginx -p 8001:81 -p 8000:80 -v /home/kevin/docker/nginx_web/:/home/visual/nginx_web/ learn/nginx:v2 /bin/sh /etc/rc.local
啟動容器的參數介紹
- -it枕荞,交互方式啟動
- --name nginx_test柜候,指定新容器的名稱是nginx_test
- -h docker-nginx搞动,指定新容器的主機名是docker-nginx
- -p 8001:81 -p 8000:80,指定宿主機與docker容器的端口映射渣刷,宿主機的8001對應docker容器的81鹦肿,宿主機的8000對應docker容器的80
- -v /home/kevin/docker/nginx_web/:/home/visual/nginx_web/,指定宿主機與docker容器的文件映射辅柴,宿主機的/home/kevin/docker/nginx_web/ 對應docker容器的 /home/visual/nginx_web/
- learn/nginx:v2狮惜,指定啟動容器對應的鏡像是learn/nginx:v2,可以是鏡像ID
- /bin/sh /etc/rc.local碌识,指定容器啟動后,執(zhí)行shell腳本是/etc/rc.local
查看docker容器虱而,容器nginx_test處于up狀態(tài)筏餐,說明啟動正常
kevin@apple:~/docker/nginx_web$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cbbbe7a5d47a learn/nginx:v2 "/bin/sh /etc/rc.l..." 25 minutes ago Up 24 minutes 0.0.0.0:8000->80/tcp, 0.0.0.0:8001->81/tcp nginx_test
966bd52b72da ubuntu "/bin/sh -c 'while..." 42 hours ago Exited (137) 40 hours ago stupefied_knuth
00f561d97811 ubuntu "/bin/echo hello w..." 42 hours ago Exited (0) 42 hours ago nifty_mcnulty
93a1b9d39683 ubuntu "bash" 43 hours ago Exited (0) About an hour ago zealous_noether
abdc084f9821 hello-world "/hello" 43 hours ago Exited (0) 20 hours ago sleepy_clarke
5,測試docker_nginx是否正常
通過瀏覽器測試8001端口
通過瀏覽器測試8001端口-1.png
通過瀏覽器測試8000端口
通過瀏覽器測試8000端口.png
修改宿主機的文件牡拇,并測試8001端口
- 修改宿主機的/home/kevin/docker/nginx_web/index.html文件
kevin@apple:~/docker/nginx_web$ vi index.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx! I am in docker!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx! I am in Docker!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a >nginx.org</a>.<br/>
Commercial support is available at
<a >nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
- 通過瀏覽器測試8001端口魁瞪,發(fā)現“Welcome to nginx! I am in docker!”,說明內容已經修改惠呼,使用docker做為web服務器的功能已經OK
通過瀏覽器測試8001端口-2.png