{"id":10006309,"date":"2022-08-24T15:53:50","date_gmt":"2022-08-24T07:53:50","guid":{"rendered":"https:\/\/ityet.com\/?p=10006309"},"modified":"2022-08-24T15:53:50","modified_gmt":"2022-08-24T07:53:50","slug":"docker%e9%83%a8%e7%bd%b2node%e5%ba%94%e7%94%a8%e7%ae%80%e5%8d%95%e5%ae%9e%e8%b7%b5","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006309","title":{"rendered":"Docker\u90e8\u7f72Node\u5e94\u7528\u7b80\u5355\u5b9e\u8df5"},"content":{"rendered":"<p>\u7b80\u4ecb\uff1a\u00a0\u672c\u6587\u5c06\u4ece\u96f6\u81f3\u4e00\uff0c\u4ecb\u7ecd\u5982\u4f55\u5728\u4e91\u670d\u52a1\u5668\u4e0a\u901a\u8fc7 Docker \u5bb9\u5668\u8fd0\u884c\u4e00\u4e2a\u7b80\u5355\u7684Node\u5e94\u7528\u3002\u524d\u8a00\u672c\u6587\u5c06\u4ece\u96f6\u81f3\u4e00\uff0c\u4ecb\u7ecd\u5982\u4f55\u5728\u4e91\u670d\u52a1\u5668\u4e0a\u901a\u8fc7 Docker \u5bb9\u5668\u8fd0\u884c\u4e00\u4e2a\u7b80\u5355\u7684Node\u5e94\u7528\u3002\u672c\u6587\u5047\u8bbe\u8bfb\u8005\u5df2\u7ecf\u638c\u63e1\u57fa\u672c\u7684Linux\uff0cDocker\uff0cNode\uff0cExpress \u77e5\u8bc6\u3002\u57fa\u672c\u6b65\u9aa4\u672c\u5730\u5199\u597dnodejs\u5e94\u7528\uff0c\u653e\u5230github\u3002\u4e91\u670d\u52a1\u5668\u5b89\u88c5docker\uff0c\u914d\u7f6e\u56fd\u5185\u955c\u50cf\u52a0\u901f\u3002\u62c9\u53d6\u4e00\u4e2anode\u955c\u50cf\u3002\u4ecegithub\u62c9\u53d6\u9879\u76ee\u4ee3\u7801\u3002\u7f16\u5199Dockerfile\u6587\u4ef6\uff0c\u6784\u5efa\u955c\u50cf\u3002\u6839\u636e\u955c\u50cf\u8fd0\u884c\u4e00\u4e2a\u5bb9\u5668\u3002\u672c\u5730\u8fdb\u884c\u8bbf\u95ee\u3002\u5177\u4f53\u6b65\u9aa41.\u7f16\u5199Node\u5e94\u7528\u65b0\u5efa\u9879\u76ee\u76ee\u5f55hello\uff0c\u8fdb\u5165\u76ee\u5f55\uff0c\u521d\u59cb\u5316\u9879\u76ee\uff1anpm  init\u5b89\u88c5express\u6a21\u5757npm install express &#8211;save\u9879\u76ee\u6839\u76ee\u5f55\u4e0b\u65b0\u5efa app.js \uff0c\u5f53\u8bbf\u95ee\u65f6\u8fd4\u56deHello,Docker!\uff0c\u76d1\u542c3000\u7aef\u53e3\u3002\/\/ app.js<br \/>const express = require(&#8216;express&#8217;);  <br \/>const app = express();<br \/>app.get(&#8216;\/&#8217;, function (req, res) {<br \/> res.send(&#8216;Hello,Docker!&#8217;);<br \/>});  <br \/>app.listen(3000);<br \/>console.log(&#8216;Server is running on <a href=\"https:\/\/link.segmentfault.com\/?enc=DUEUb6qkhAhbg9qNzMY%2BZA%3D%3D.uib0%2FSMdYWZWKrtmtqG1XWtQnxuivmWnEUANOwodHjE%3D\" rel=\"nofollow\">http:\/\/localhost:3000<\/a>&#8216;);2.\u5b89\u88c5Docker\u8fdc\u7a0b\u8fde\u63a5\u670d\u52a1\u5668\uff0c\u5b89\u88c5Docker\uff1ayum install -y docker\u5b89\u88c5\u6210\u529f\u4ee5\u540e\uff0c\u7f16\u8f91\/etc\/docker\/daemon.json\u00a0\u6587\u4ef6\uff0c\u914d\u7f6e\u56fd\u5185\u955c\u50cf\u52a0\u901f\u3002\u8fd9\u91cc\u7528\u5230\u7684\u662f\u963f\u91cc\u4e91\u63d0\u4f9b\u7684\u52a0\u901f\u670d\u52a1\uff0c\u9700\u8981\u767b\u5f55<a href=\"https:\/\/link.segmentfault.com\/?enc=W98AtL8doO9Ri3qfuynvQw%3D%3D.nFjO5vHQmgCCkDebgBhmR14hkBCA%2FHAlqXnVEtIPUjo%3D\" rel=\"nofollow\">https:\/\/dev.aliyun.com\/<\/a>\u6765\u83b7\u53d6\u4e2a\u4eba\u4e13\u5c5e\u7684\u52a0\u901f\u5730\u5740\u3002\u8fd9\u91cc\u5047\u8bbe\u52a0\u901f\u5730\u5740\u662f<a href=\"https:\/\/link.segmentfault.com\/?enc=YGP9uEbQyMqh7rJjcuACOQ%3D%3D.fMx8aunpMA47IqPNckUuyaKJaYwGaGyIDblHCd074MpmB87Qyi3wQQUTWxMqn9OI\" rel=\"nofollow\">https:\/\/4da6b0g8.mirror.aliyu&#8230;<\/a>\u3002vim \/etc\/docker\/daemon.json\u7f16\u5199\u4ee5\u4e0b\u5185\u5bb9\uff1a{<br \/>\u201cregistry-mirrors\u201d: [\u201c<a href=\"https:\/\/link.segmentfault.com\/?enc=bneSH2rcPN0HFnX06MddsA%3D%3D.T4IvaULs6NQSSBMZ%2F9%2B9S%2BLzv6bOtM4wrZWoEIwB5dCgcfG8Jvd9FWmWOpzllXZB\" rel=\"nofollow\">https:\/\/4da6b0g8.mirror.aliyu&#8230;<\/a>\u201c]<br \/>}\u4fee\u6539\u5b8c\u914d\u7f6e\u6587\u4ef6\u5e94\u8be5\u91cd\u542f\u670d\u52a1\u30023.\u62c9\u53d6Node\u955c\u50cf\u62c9\u53d6Node\u955c\u50cfdocker pull node:9.2.0\u7b49\u5f85\u955c\u50cf\u62c9\u53d6\u6210\u529f\uff0c\u53ef\u4ee5\u67e5\u770b\u4e0b\u6211\u4eec\u5df2\u7ecf\u6709\u7684\u955c\u50cf:docker imagesREPOSITORY          TAG                 IMAGE ID            CREATED             SIZE<br \/>docker.io\/node      9.2.0               c1d02ac1d9b4        3 weeks ago         675.6 MB<br \/>node                9.2.0               c1d02ac1d9b4        3 weeks ago         675.6 MB4.\u90e8\u7f72\u9879\u76ee\u4ee3\u7801\u8fdb\u5165\/home\u76ee\u5f55\uff0c\u4ecegithub\u4e0a\u62c9\u53d6\u9879\u76ee\u4ee3\u7801\uff1acd \/home<br \/>git clone <a href=\"https:\/\/link.segmentfault.com\/?enc=uCdn6WomTDuXJktBiIhYiA%3D%3D.iT7LjlhrfWaTIUATAyxwHG6wpCa9ZPFOAfdKsfWfZdc%3D\" rel=\"nofollow\">https:\/\/github.com\/hsyq\/hello<\/a>\u8fd9\u91cc\u7684github\u5730\u5740\u8981\u6362\u6210\u81ea\u5df1\u4ed3\u5e93\u7684\u5730\u5740\u30025.\u7f16\u5199Dockerfile\u6587\u4ef6Dockerfile\u6587\u4ef6\u91cc\u662f\u4e00\u6761\u6761\u80fd\u88abLinux\u6267\u884c\u7684\u547d\u4ee4\uff0cdocker\u80fd\u6839\u636e\u8be5\u6587\u4ef6\u6784\u5efa\u955c\u50cf\u3002\u8be5\u6587\u4ef6\u5fc5\u987b\u548c\u9879\u76ee\u6587\u4ef6\u653e\u5728\u4e00\u8d77\uff0c\u4e00\u822c\u653e\u5728\u9879\u76ee\u6839\u76ee\u5f55\u4e0b\uff0c\u672c\u4f8b\u4e2d\u5373\/home\/hello\u76ee\u5f55\u4e0b\u3002cd hello<br \/>vim Dockerfile# \u57fa\u7840\u955c\u50cf\u4e3anode\uff0c\u7248\u672c\u4e3av9.2.0<br \/>FROM node:9.2.0<\/p>\n<h2>\u521b\u5efa\u5bb9\u5668\u5185\u7684\u9879\u76ee\u5b58\u653e\u76ee\u5f55<\/h2>\n<p>RUN mkdir -p \/home\/nodeapp<br \/>WORKDIR \/home\/nodeapp<\/p>\n<h2>\u5c06Dockerfile\u5f53\u524d\u76ee\u5f55\u4e0b\u6240\u6709\u6587\u4ef6\u62f7\u8d1d\u81f3\u5bb9\u5668\u5185\u9879\u76ee\u76ee\u5f55\u5e76\u5b89\u88c5\u9879\u76ee\u4f9d\u8d56<\/h2>\n<p>COPY . \/home\/Service<br \/>RUN npm install<\/p>\n<h2>\u5bb9\u5668\u5bf9\u5916\u66b4\u9732\u7684\u7aef\u53e3\u53f7\uff0c\u8981\u548cnode\u9879\u76ee\u914d\u7f6e\u7684\u7aef\u53e3\u53f7\u4e00\u81f4<\/h2>\n<p>EXPOSE 3000<\/p>\n<h2>\u5bb9\u5668\u542f\u52a8\u65f6\u6267\u884c\u7684\u547d\u4ee4<\/h2>\n<p>CMD [ &#8220;node&#8221;, &#8220;app.js&#8221; ]6.\u6784\u5efa\u955c\u50cf\u5728\u9879\u76ee\u6839\u76ee\u5f55\u4e0b\u6267\u884c\u547d\u4ee4\u6784\u5efa\u5bf9\u8c61\uff1adocker build -t hello .\u5c4f\u5e55\u4f1a\u6309\u7167Dockerfile\u5185\u7684\u6307\u4ee4\u4e00\u6761\u6761\u6267\u884c\u5e76\u663e\u793a\u5728\u5c4f\u5e55\u4e0a\u3002\u6784\u5efa\u5b8c\u6210\uff0c\u67e5\u770b\u5df2\u6709\u7684\u955c\u50cf\u3002REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE<br \/>hello               latest              b753dcb8208d        4 hours ago         675.6 MB<br \/>docker.io\/node      9.2.0               c1d02ac1d9b4        3 weeks ago         675.6 MB<br \/>node                9.2.0               c1d02ac1d9b4        3 weeks ago         675.6 MB<br \/>7.\u8fd0\u884c\u5bb9\u5668\u901a\u8fc7\u955c\u50cfhello\u521b\u5efa\u4e00\u4e2a\u5bb9\u5668\u5e76\u8fd0\u884c\uff1adocker run -d &#8211;name hellocontainer -p 80:3000 hello-d\u8868\u793a\u5bb9\u5668\u4f1a\u5728\u540e\u53f0\u8fd0\u884c\uff1b&#8211;name \u662f\u6211\u4eec\u7ed9\u5bb9\u5668\u8d77\u7684\u540d\u5b57\uff0c\u8fd9\u4e2a\u540d\u5b57\u662f\u552f\u4e00\u7684\uff1b-p\u8868\u793a\u7aef\u53e3\u6620\u5c04\uff0c\u5373\u5c06\u5bb9\u5668\u5185\u76843000\u7aef\u53e3\u6620\u5c04\u5230\u5bbf\u4e3b\u673a\u5668\u768480\u7aef\u53e3\u4e0a\uff0c\u8fd9\u6837\u5916\u90e8\u5c31\u53ef\u4ee5\u901a\u8fc780\u7aef\u53e3\u6765\u8bbf\u95ee\u5bb9\u5668\u5185\u90e8\u8fd0\u884c\u7684\u5e94\u7528\u4e86\u3002\u672c\u6587\u7684\u6f14\u793a\u6ca1\u6709\u4f7f\u7528 Nginx \u505a\u7aef\u53e3\u8f6c\u53d1\uff0c\u53ea\u8981\u670d\u52a1\u5668\u5f00\u653e 80 \u7aef\u53e3\uff0c\u5c31\u80fd\u5c06 80 \u6620\u5c04\u5230 node\u9879\u76ee\u5bb9\u5668\u76843000\u7aef\u53e3\u4e0a\uff0c\u5bf9\u5b8c\u63d0\u4f9b\u8bbf\u95ee\u670d\u52a1\u3002\u6267\u884c\u547d\u4ee4\u67e5\u770b\u5bb9\u5668\u7684\u72b6\u6001\uff1adocker ps -aCONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                        PORTS                  NAMES<br \/>748be4cfccd9        hello               &#8220;node app.js&#8221;       25 seconds ago      Up 25 seconds                 0.0.0.0:80-&gt;3000\/tcp   hellocontainer<br \/>\u6b64\u65f6\uff0cnode\u5e94\u7528\u5df2\u7ecf\u5728\u5bb9\u5668\u5185\u8dd1\u8d77\u6765\u4e86\u3002\u6211\u4eec\u53ef\u4ee5\u8bbf\u95ee\u4e86\u30028.\u672c\u5730\u8bbf\u95ee\u6253\u5f00\u672c\u5730\u7684\u6d4f\u89c8\u5668\uff0c\u8f93\u5165\u4e91\u670d\u52a1\u5668\u7684\u516c\u7f51IP\u5373\u53ef\u8bbf\u95ee\uff1a<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042368214\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><br \/>\u539f\u6587\u94fe\u63a5\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=PNziF8nm6%2Bp8EJkETq9PBw%3D%3D.GE83Sk3jVnsHIy1dAWr8fKx9xyXH3tYhQ4zrOzyVQ2TeC9E8OBdGgyLprprrOEsb\" rel=\"nofollow\">https:\/\/click.aliyun.com\/m\/10&#8230;<\/a>\u672c\u6587\u4e3a\u963f\u91cc\u4e91\u539f\u521b\u5185\u5bb9\uff0c\u672a\u7ecf\u5141\u8bb8\u4e0d\u5f97\u8f6c\u8f7d\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042368212\">https:\/\/segmentfault.com\/a\/1190000042368212<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7b80\u4ecb\uff1a\u00a0\u672c\u6587\u5c06\u4ece\u96f6\u81f3\u4e00\uff0c\u4ecb\u7ecd\u5982\u4f55\u5728\u4e91\u670d\u52a1\u5668\u4e0a\u901a\u8fc7 Docker \u5bb9\u5668\u8fd0\u884c\u4e00\u4e2a\u7b80\u5355\u7684Node\u5e94\u7528\u3002\u524d\u8a00\u672c\u6587\u5c06\u4ece\u96f6 [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-10006309","post","type-post","status-publish","format-standard","hentry"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006309","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006309"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006309\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006309"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006309"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006309"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}