Docker映射詳解,沒問題了!

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"小夥伴兒們,大家好!上一篇我們瞭解到了Docker容器的操作命令——"},{"type":"link","attrs":{"href":"http://mp.weixin.qq.com/s?__biz=MzIzNzE1ODExNw==&mid=2247485291&idx=1&sn=a094a81338ddacd50974caf8765f0ec4&chksm=e8cdace1dfba25f735e5a63dee51da8f930f9898460e9d4ad41635db5b1b7ca1565ad42d12b4&scene=21#wechat_redirect","title":null},"content":[{"type":"text","text":"久等了!Docker容器常用命令"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"今天來學習Docker端口映射!持續更新,敬請期待!"}]}]},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":"思維導圖:"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/85/859583d79031aeb42436e165de370d38.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"1,Docker映射是什麼?"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/92/92003e9b66e1891a59f0a899d76a6f0c.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"容器裏面運行着應用,外部需要訪問交互,涉及到容器裏面的端口和宿主機之間的映射;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"通過端口映射,我們就可以從"},{"type":"text","marks":[{"type":"strong"}],"text":"外部訪問宿主機的指定端口"},{"type":"text","text":"來訪問到"},{"type":"text","marks":[{"type":"strong"}],"text":"容器的應用"},{"type":"text","text":";"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"2,如何實現Docker映射?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這裏我們以Tomcat爲例子,我們看看如何在外部通過Docker端口映射來訪問宿主主機裏面的Tomca。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"2.1,下載tomcat容器鏡像"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下載鏡像:"},{"type":"codeinline","content":[{"type":"text","text":"docker pull tomcat:tag"}]},{"type":"text","text":",不加版本號的話默認是下載最新版本"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/2c/2c94aa131d0fa85b15dd115694021d4f.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"查看所有鏡像:"},{"type":"codeinline","content":[{"type":"text","text":"docker images"}]},{"type":"text","text":",我這裏是已經下載了三個版本的tomcat"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/b1/b1a3111a7d4786efc925a5a5c545d5ad.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"2.2,創建並啓動tomcat容器"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"創建啓動tomcat容器:"},{"type":"codeinline","content":[{"type":"text","text":"docker run --name tomcat1 -d tomcat :latest"}]},{"type":"text","text":", 其中"},{"type":"codeinline","content":[{"type":"text","text":"-d"}]},{"type":"text","text":"是表示後臺運行容器,"},{"type":"codeinline","content":[{"type":"text","text":"tomcat1"}]},{"type":"text","text":"是容器名稱,"},{"type":"codeinline","content":[{"type":"text","text":"tomcat :latest"}]},{"type":"text","text":"是註明版本號"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/4c/4c2a210274a6e7e23aba6b015b98da77.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"查看正在運行的容器:"},{"type":"codeinline","content":[{"type":"text","text":"docker ps"}]}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/fc/fc579c8c8e20f5a91a2e24a7c5555449.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"2.3,開放端口映射命令"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"映射命令:"},{"type":"codeinline","content":[{"type":"text","text":"docker run --name 容器名稱 -d -p (服務器端口):(Docker端口) image-name"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"--name"}]},{"type":"text","text":":自定義容器名,不指定時,docker會自動生成一個名稱"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"-d"}]},{"type":"text","text":":表示後臺運行容器"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"服務器端口"}]},{"type":"text","text":":這裏的tomcat端口是8080"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"Docker端口"}]},{"type":"text","text":":我們任意設置一個端口號,但是前提是這個端口號不能被佔用"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"image-name"}]},{"type":"text","text":":指定運行的鏡像名稱以及Tag,這裏也可以用鏡像ID都可以的"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/a0/a020ac18a166fe49e15f80b9a1bb7b25.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這個時候我們的端口以及搭建好了,我們在瀏覽器中輸入服務器ip地址(查看命令:"},{"type":"codeinline","content":[{"type":"text","text":"ip addr show"}]},{"type":"text","text":")再加上Docker端口就可以訪問了。"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/be/beaf67999da380b4b93af85f67cece46.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"結果,就這??"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/64/643cc1efc3f53c5c45387d116d5578e7.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"2.4,關於訪問映射端口出現404問題"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這個問題相信剛開始搭建的時候應該都遇到過,我查了一下原因如下:"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/50/50fe54e4f4b1dc78af0d3464e1e1d658.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"解決辦法:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們先進入到tomcat的目錄:"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/f6/f66f33bd57164afe44b8bad363051c4a.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"進入之後,修改"},{"type":"codeinline","content":[{"type":"text","text":"webapps"}]},{"type":"text","text":"爲"},{"type":"codeinline","content":[{"type":"text","text":"webapps2"}]},{"type":"text","text":"或者修改"},{"type":"codeinline","content":[{"type":"text","text":"webapps.dist"}]},{"type":"text","text":"爲"},{"type":"codeinline","content":[{"type":"text","text":"webapps"}]},{"type":"text","text":"就可以了"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/b2/b267d628a73d1000b297ef45ff29d280.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/25/25ae80dce76fc195f55322b5d2899950.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們先來看看效果:"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/00/00989253e2d68eafe02549679f5b41e3.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"3,提交運行容器成爲鏡像"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上述運行成功之後,也存在一定問題,如果不退當前容器的情況下,運行端口端口映射的tomcat是可以訪問到首頁的;但是,我們只是在這個容器中修改了 命名,我們重啓了這個容器,修改的命名就會恢復。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"爲了防止恢復問題,我們自己把修改後的鏡像映射成新的鏡像"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"命令:"},{"type":"codeinline","content":[{"type":"text","text":"docker commit -a=“作者名” -m=“備註” 運行時容器ID 新鏡像名稱"}]}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/49/49ef6bc37cdd5fd9d7b5af40f596b845.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"然後我們就可以用新鏡像來重新訪問映射端口的應用了"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/9f/9f598208c521da61bdd09357a1f4eecc.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"非常方便!"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule"},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"好了,今天就先分享到這裏了,下期繼續給大家帶來Docker的使用後續內容!更多幹貨、優質文章,歡迎關注我的原創技術公衆號~"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/e6/e657572474a8c418a7ca95500b97e192.jpeg","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章