1. 程式人生 > >Docker多埠對映和映象遷移多份部署

Docker多埠對映和映象遷移多份部署

閱讀此文章前請以上篇博文為基礎:http://blog.csdn.net/lishirong/article/details/72677860

本片博文主要解決以下幾個問題:

1.在一個docker中對映多個埠

2.一個映象在同一主機上的多份部署

3.映象遷移到其它linux主機進行部署
開始吧!

一、單docker多埠對映

1.首先得到現有的映象

#docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
sdzn.com/tomcat     0.1                 af2454df9380        10 seconds ago      514.6 MB
docker.io/centos    7.2.1511            feac5e0dfdb2        8 months ago        194.6 MB
2.將18080和28080均對映到容器的8080埠

#docker run -d -p 18080:8080 -p 28080:8080 --name tomcat1 sdzn.com/tomcat:0.1 /root/run.sh

測試訪問均能成功http://127.0.0.1:18080和http://127.0.0.1:28080

3.檢視當前正在執行的映象

#docker ps

CONTAINER ID        IMAGE                 COMMAND             CREATED             STATUS              PORTS                                              NAMES
75626d3c702b        sdzn.com/tomcat:0.1   "/root/run.sh"      2 minutes ago       Up 2 minutes        0.0.0.0:18080->8080/tcp, 0.0.0.0:28080->8080/tcp   tomcat1
3.額外增加一個正在執行的映象

#docker run -d -p 38080:8080 -p 48080:8080 --name tomcat2 sdzn.com/tomcat:0.1 /root/run.sh
#docker ps

CONTAINER ID        IMAGE                 COMMAND             CREATED             STATUS              PORTS                                              NAMES
6ccc5564b93e        sdzn.com/tomcat:0.1   "/root/run.sh"      14 seconds ago      Up 4 seconds        0.0.0.0:38080->8080/tcp, 0.0.0.0:48080->8080/tcp   tomcat2
75626d3c702b        sdzn.com/tomcat:0.1   "/root/run.sh"      6 minutes ago       Up 6 minutes        0.0.0.0:18080->8080/tcp, 0.0.0.0:28080->8080/tcp   tomcat1

4.停止一個容器和啟動一個容器

#docker stop tomcat2

#docker start tomcat2

#docker restart tomcat2

這樣可以重啟一個容器的服務,達到暫時減小記憶體佔用,恢復初始狀態的作用,但相應的容器的增量資料會被銷燬。

5.看下我1G記憶體的虛擬機器跑了5個docker容器例項

CONTAINER ID        IMAGE                 COMMAND             CREATED              STATUS              PORTS                                              NAMES
4c4b3643d6a8        sdzn.com/tomcat:0.1   "/root/run.sh"      About a minute ago   Up About a minute   0.0.0.0:8085->8080/tcp, 0.0.0.0:8086->8080/tcp     tomcat5
125574dfe0fc        sdzn.com/tomcat:0.1   "/root/run.sh"      2 minutes ago        Up 2 minutes        0.0.0.0:8083->8080/tcp, 0.0.0.0:8084->8080/tcp     tomcat4
79f89f9df907        sdzn.com/tomcat:0.1   "/root/run.sh"      3 minutes ago        Up 3 minutes        0.0.0.0:8081->8080/tcp, 0.0.0.0:8082->8080/tcp     tomcat3
6ccc5564b93e        sdzn.com/tomcat:0.1   "/root/run.sh"      21 minutes ago       Up 12 minutes       0.0.0.0:38080->8080/tcp, 0.0.0.0:48080->8080/tcp   tomcat2
75626d3c702b        sdzn.com/tomcat:0.1   "/root/run.sh"      28 minutes ago       Up 27 minutes       0.0.0.0:18080->8080/tcp, 0.0.0.0:28080->8080/tcp   tomcat1
6.給指定容器額外增加一個埠對映

百度上並沒有找到相關資訊,大多數建議是退出並另存為一個容器重新啟動。

二、單映象單機多份部署

在“一”中已經有提到,單映象可以理解是靜態的,可以從映象釋出為多個容器來服務。

三、映象遷移其它linux部署
1.映象匯出,只能講容器ID進行匯出

#docker ps -a

CONTAINER ID        IMAGE                 COMMAND             CREATED             STATUS                         PORTS                                            NAMES
4c4b3643d6a8        sdzn.com/tomcat:0.1   "/root/run.sh"      56 minutes ago      Up 56 minutes                  0.0.0.0:8085->8080/tcp, 0.0.0.0:8086->8080/tcp   tomcat5
125574dfe0fc        sdzn.com/tomcat:0.1   "/root/run.sh"      57 minutes ago      Exited (137) 22 minutes ago                                                     tomcat4
79f89f9df907        sdzn.com/tomcat:0.1   "/root/run.sh"      58 minutes ago      Exited (137) 22 minutes ago                                                     tomcat3
6ccc5564b93e        sdzn.com/tomcat:0.1   "/root/run.sh"      About an hour ago   Exited (137) 23 minutes ago                                                     tomcat2
75626d3c702b        sdzn.com/tomcat:0.1   "/root/run.sh"      About an hour ago   Exited (137) 23 minutes ago                                                     tomcat1
3dd730d024a3        feac                  "/bin/bash"         About an hour ago   Exited (0) About an hour ago                                                    zen_hypatia

#docker export 75626d3c702b>tomcat1.tar

或者儲存映象檔案也可

#docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
sdzn.com/tomcat     0.2                 3d37243864ed        37 seconds ago      509 MB
sdzn.com/tomcat     0.1                 c4370254990a        6 hours ago         518 MB
docker.io/tomcat    latest              3695a0fe8320        3 days ago          334.3 MB
docker.io/centos    7.2.1511            feac5e0dfdb2        8 months ago        194.6 MB

#docker save 3d3>tomcat.tar

#ls

Desktop           tomcat1.tar  模板  圖片  下載  桌面
install_agent.sh  公共         視訊  文件  音樂
2.將tomcat1.tar匯入到另一個ubuntu14的系統下

#ls

tomcat1.tar
3.匯入tar包到映象中

#cat tomcat1.tar | docker import - sdzn.com/tomcat:0.1

#docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
sdzn.com/tomcat     0.1                 2c26a93be79d        23 seconds ago      514.7 MB
4.啟動映象

# docker run -d -p 8080:8080 --name tomcat1 sdzn.com/tomcat:0.1 /root/run.sh

#docker ps

CONTAINER ID        IMAGE                 COMMAND             CREATED             STATUS              PORTS                    NAMES
9d760c4a36aa        sdzn.com/tomcat:0.1   "/root/run.sh"      30 seconds ago      Up 27 seconds       0.0.0.0:8080->8080/tcp   tomcat1

5.訪問測試:


成功!至此完成了docker從CentOs7的匯出和向Ubuntu的匯入。


文獻參考:

1.http://blog.csdn.net/meegomeego/article/details/50707621

2.http://blog.csdn.net/pipisorry/article/details/51330126