{"id":177947,"date":"2020-05-05T08:49:45","date_gmt":"2020-05-05T00:49:45","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=177947"},"modified":"2020-04-23T10:54:33","modified_gmt":"2020-04-23T02:54:33","slug":"docker-compose-ports","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/docker-compose-ports.html","title":{"rendered":"docker-compose ports \u4e0e expose \u7684\u533a\u522b"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86docker-compose ports\u548cexpose\u7684\u533a\u522b\u8be6\u89e3\uff0c\u6587\u4e2d\u901a\u8fc7\u793a\u4f8b\u4ee3\u7801\u4ecb\u7ecd\u7684\u975e\u5e38\u8be6\u7ec6\uff0c\u5bf9\u5927\u5bb6\u7684\u5b66\u4e60\u6216\u8005\u5de5\u4f5c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u5b66\u4e60\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u4eec\u4e0b\u9762\u968f\u7740\u5c0f\u7f16\u6765\u4e00\u8d77\u5b66\u4e60\u5b66\u4e60\u5427\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

docker-compose\u4e2d\u6709\u4e24\u79cd\u65b9\u5f0f\u53ef\u4ee5\u66b4\u9732\u5bb9\u5668\u7684\u7aef\u53e3\uff1aports\u548cexpose\u3002<\/p>\n

ports<\/strong><\/div>\n

ports\u66b4\u9732\u5bb9\u5668\u7aef\u53e3\u5230\u4e3b\u673a\u7684\u4efb\u610f\u7aef\u53e3\u6216\u6307\u5b9a\u7aef\u53e3\uff0c\u7528\u6cd5\uff1a<\/p>\n

\r\nports:\r\n  \r\n- \"80:80\" # \u7ed1\u5b9a\u5bb9\u5668\u768480\u7aef\u53e3\u5230\u4e3b\u673a\u768480\u7aef\u53e3\r\n  \r\n- \"9000:8080\" # \u7ed1\u5b9a\u5bb9\u5668\u76848080\u7aef\u53e3\u5230\u4e3b\u673a\u76849000\u7aef\u53e3\r\n  \r\n- \"443\" # \u7ed1\u5b9a\u5bb9\u5668\u7684443\u7aef\u53e3\u5230\u4e3b\u673a\u7684\u4efb\u610f\u7aef\u53e3\uff0c\u5bb9\u5668\u542f\u52a8\u65f6\u968f\u673a\u5206\u914d\u7ed1\u5b9a\u7684\u4e3b\u673a\u7aef\u53e3\u53f7\r\n<\/pre>\n

\u4e0d\u7ba1\u662f\u5426\u6307\u5b9a\u4e3b\u673a\u7aef\u53e3\uff0c\u4f7f\u7528ports\u90fd\u4f1a\u5c06\u7aef\u53e3\u66b4\u9732\u7ed9\u4e3b\u673a\u3002<\/p>\n

\u5bb9\u5668\u4e2d\u53ef\u4ee5\u8fd0\u884c\u4e00\u4e9b\u7f51\u7edc\u5e94\u7528\uff0c\u8981\u8ba9\u5916\u90e8\u4e5f\u53ef\u4ee5\u8bbf\u95ee\u8fd9\u4e9b\u5e94\u7528\uff0c\u53ef\u4ee5\u901a\u8fc7 -P\uff08\u5927\u5199\uff09 \u6216 -p \uff08\u5c0f\u5199\uff09 \u53c2\u6570\u6765\u6307\u5b9a\u7aef\u53e3\u6620\u5c04\u3002<\/p>\n

(1) \u5f53\u4f7f\u7528-P\u6807\u8bb0\u65f6\uff0cDocker \u4f1a\u968f\u673a\u6620\u5c04\u4e00\u4e2a49000~49900\u7684\u7aef\u53e3\u5230\u5185\u90e8\u5bb9\u5668\u5f00\u653e\u7684\u7f51\u7edc\u7aef\u53e3\u3002<\/p>\n

\u4f7f\u7528docker ps\u53ef\u4ee5\u770b\u5230\uff0c\u672c\u5730\u4e3b\u673a\u7684 49155 \u88ab\u6620\u5c04\u5230\u4e86\u5bb9\u5668\u7684 5000 \u7aef\u53e3\u3002\u6b64\u65f6\u8bbf\u95ee\u672c\u673a\u7684 49155 \u7aef\u53e3\u5373\u53ef\u8bbf\u95ee\u5bb9\u5668\u5185 web \u5e94\u7528\u63d0\u4f9b\u7684\u754c\u9762\u3002<\/p>\n

\r\n$ sudo docker run -d -P training\/webapp python app.py\r\n  \r\n$ sudo docker ps -l\r\n  \r\nCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES\r\n  \r\nbc533791f3f5 training\/webapp:latest python app.py 5 seconds ago Up 2 seconds 0.0.0.0:49155->5000\/tcp nostalgic_morse\r\n<\/pre>\n

\u540c\u6837\u7684\uff0c\u53ef\u4ee5\u901a\u8fc7docker logs\u547d\u4ee4\u6765\u67e5\u770b\u5e94\u7528\u7684\u4fe1\u606f\u3002<\/p>\n

\r\n$ sudo docker logs -f nostalgic_morse\r\n  \r\n* Running on http:\/\/0.0.0.0:5000\/\r\n  \r\n10.0.2.2 - - [23\/May\/2014 20:16:31] \"GET \/ HTTP\/1.1\" 200 -\r\n  \r\n10.0.2.2 - - [23\/May\/2014 20:16:31] \"GET \/favicon.ico HTTP\/1.1\" 404 - \r\n<\/pre>\n

(2) -p\uff08\u5c0f\u5199\uff09\u5219\u53ef\u4ee5\u6307\u5b9a\u8981\u6620\u5c04\u7684IP\u548c\u7aef\u53e3\uff0c\u4f46\u662f\u5728\u4e00\u4e2a\u6307\u5b9a\u7aef\u53e3\u4e0a\u53ea\u53ef\u4ee5\u7ed1\u5b9a\u4e00\u4e2a\u5bb9\u5668\u3002\u652f\u6301\u7684\u683c\u5f0f\u6709 hostPort:containerPort\u3001ip:hostPort:containerPort\u3001ip::containerPort\u3002<\/p>\n

expose<\/strong><\/div>\n

expose\u66b4\u9732\u5bb9\u5668\u7ed9link\u5230\u5f53\u524d\u5bb9\u5668\u7684\u5bb9\u5668\uff0c\u7528\u6cd5\uff1a<\/p>\n

\r\nexpose:\r\n- \"3000\"\r\n- \"8000\"\r\n<\/pre>\n

\u4ee5\u4e0a\u6307\u4ee4\u5c06\u5f53\u524d\u5bb9\u5668\u7684\u7aef\u53e33000\u548c8000\u66b4\u9732\u7ed9link\u5230\u672c\u5bb9\u5668\u7684\u5bb9\u5668\u3002<\/p>\n

\u548cports\u7684\u533a\u522b\u662f\uff0cexpose\u4e0d\u4f1a\u5c06\u7aef\u53e3\u66b4\u9732\u7ed9\u4e3b\u673a\u3002<\/p>\n

\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff0c\u4e5f\u5e0c\u671b\u5927\u5bb6\u591a\u591a\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

docker-compose\u4e2d\u6709\u4e24\u79cd\u65b9\u5f0f\u53ef\u4ee5\u66b4\u9732\u5bb9\u5668\u7684\u7aef\u53e3\uff1aports\u548cexpose\u3002 ports\u66b4\u9732\u5bb9\u5668\u7aef\u53e3 […]<\/p>\n","protected":false},"author":63,"featured_media":177948,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-177947","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/177947","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=177947"}],"version-history":[{"count":1,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/177947\/revisions"}],"predecessor-version":[{"id":177949,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/177947\/revisions\/177949"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/177948"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=177947"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=177947"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=177947"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}