天天看點

虛拟化技術—docker容器—管理篇

一、我們怎麼更友善的進去已經start的容器呢?

nsenter:可以通過這個指令進去docker的images裡

首先擷取一個容器的id:

docker inspect --format " ``.`State`.`Pid`" centos_with_nettools 擷取一個id

10592

<a href="http://s3.51cto.com/wyfs02/M02/88/87/wKioL1f7MrWTIklOAAAM17doeFY415.png-wh_500x0-wm_3-wmp_4-s_3472879000.png" target="_blank"></a>

nsenter --target 10592 --mount --yts --ipc --net

<a href="http://s3.51cto.com/wyfs02/M01/88/87/wKioL1f7M06yfJqqAAAKWYZpiRU430.png-wh_500x0-wm_3-wmp_4-s_606943617.png" target="_blank"></a>

這樣就進去了,當然這樣很麻煩,是以可以寫一個腳本:

cat in.sh

CNAME=$1

CPID=$(docker inspect --format " ``.`State`.`Pid`" $CNAME)

nsenter --target $CPID --mount --uts --ipc --net

設定權限:

chmod +x in.sh

使用:./in.sh centos_with_nettools

二、檢視docker網絡

brctl show

檢視docker的網橋

<a href="http://s5.51cto.com/wyfs02/M00/88/8B/wKiom1f7NGmy_OClAAASSnrdxsU553.png-wh_500x0-wm_3-wmp_4-s_258254966.png" target="_blank"></a>

iptables -t nat -L -n

<a href="http://s4.51cto.com/wyfs02/M00/88/87/wKioL1f7NLvSbFDpAAA4UQEy1V4461.png-wh_500x0-wm_3-wmp_4-s_2288764409.png" target="_blank"></a>

三、容器建立好了,怎麼對外提供服務呢?

容器都是獨立的,資源隔離的,一個容器最常見的是隻跑一個服務,但凡服務都都是有端口的,那麼我們就要把容器内的端口映射到本機的端口上,然後對外提供服務

第一種:随機映射

docker run -P -d --name nginx1 nginx

第二種:指定映射

docker run -d -p 91:80 --name nginx2 nginx

四、資料卷

什麼是資料卷?

    當然是存放資料的,那麼我們用來做什麼?打個比方,一個網站後端不可能隻有一台伺服器,那麼多台伺服器怎麼保證一些需要的資料一緻呢?這裡就要用到資料卷了,一個容器隻用來儲存資料,其他背景伺服器都到這個容器裡取資料,這樣就可以保證資料一緻性了。

先建立一個容器:

docker run -it --name volume-test1 -h nginx -v /data nginx

docker ps -l檢視一下:

<a href="http://s3.51cto.com/wyfs02/M00/88/87/wKioL1f7NzqAAYf7AAAZ8yP6mk8252.png-wh_500x0-wm_3-wmp_4-s_3579008631.png" target="_blank"></a>

docker inspect -f "``.`Volumes`" volume-test1檢視卷:

<a href="http://s3.51cto.com/wyfs02/M01/88/8B/wKiom1f7N1-Cx26MAAAWxwklSKg028.png-wh_500x0-wm_3-wmp_4-s_499824730.png" target="_blank"></a>

<a href="http://s2.51cto.com/wyfs02/M01/88/87/wKioL1f7N2ihhLKgAAAv-aghLFk784.png-wh_500x0-wm_3-wmp_4-s_3658928441.png" target="_blank"></a>

挂載:

docker run -it --name volume-test2 -h nginx1 -v /opt:/opt centos

注意:/opt後面不能有/号

docker run -it --name volume-test2 -h nginx1 -v /opt:/opt:ro centos

ro:表示在容器裡不能寫

這時建立新容器時就可以指定了

建立容器,資料卷從volume-test1上取

docker run -it --name volume-test34 --volumes-from volume-test1 centos

本文轉自 sykmiao 51CTO部落格,原文連結:http://blog.51cto.com/syklinux/1860219,如需轉載請自行聯系原作者

繼續閱讀