{"id":48793,"date":"2023-05-12T21:50:37","date_gmt":"2023-05-12T13:50:37","guid":{"rendered":"http:\/\/lrxjmw.cn\/?p=48793"},"modified":"2023-05-12T21:50:37","modified_gmt":"2023-05-12T13:50:37","slug":"docker-run-time","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/docker-run-time.html","title":{"rendered":"\u5982\u4f55\u4f7f\u7528Docker\u6784\u5efa\u8fd0\u884c\u65f6\u95f4\u8f83\u957f\u7684\u811a\u672c"},"content":{"rendered":"
\u5bfc\u8bfb<\/td>\n | \u6211\u60f3\u6211\u5df2\u7ecf\u627e\u5230\u4e86\u4e00\u4e2a\u975e\u5e38\u4e0d\u9519\u7684Docker\u4f7f\u7528\u6848\u4f8b\u3002\u4f60\u662f\u4e0d\u662f\u4f1a\u89c9\u5f97\u8fd9\u662f\u4e00\u7bc7\u5199Docker\u6709\u591a\u597d\u591a\u597d\u7684\u6587\u7ae0\uff0c\u5f00\u59cb\u4e4b\u524d\u6211\u60f3\u548c\u4f60\u786e\u8ba4\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4f1a\u4ecb\u7ecd\u5982\u4f55\u628a\u6587\u4ef6\u7cfb\u7edf\u4f5c\u4e3a\u6301\u4e45\u6027\u7684\u6570\u636e\u7ed3\u6784\u3002 \u56e0\u6b64\uff0c\u8fd9\u7bc7\u6587\u7ae0\u7684\u89c1\u89e3\u540c\u6837\u9002\u7528\u4e8e\u5176\u4ed6\u7684 copy-on-write\u6587\u4ef6\u7cfb\u7edf\uff0c\u5982BTRFS\u548cZFS\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n <\/p>\n \u95ee\u9898<\/strong><\/div>\n \u8ba9\u6211\u4eec\u4ece\u8fd9\u4e2a\u6211\u8bd5\u56fe\u89e3\u51b3\u7684\u95ee\u9898\u5f00\u59cb\u3002\u6211\u5f00\u53d1\u4e86\u4e00\u4e2a\u4f1a\u8fd0\u884c\u5f88\u957f\u65f6\u95f4\u7684\u6784\u5efa\u811a\u672c\uff0c\u8fd9\u4e2a\u811a\u672c\u4e2d\u5305\u542b\u4e86\u5f88\u591a\u7684\u6b65\u9aa4\u3002 \u8fd9\u4e2a\u811a\u672c\u4f1a\u8fd0\u884c1-2\u4e2a\u5c0f\u65f6\u3002 \u5b83\u4f1a\u4ece\u7f51\u7edc\u4e0b\u8f7d\u6bd4\u8f83\u5927\u7684\u6587\u4ef6\uff08\u8d85\u8fc7300M\uff09\u3002 \u540e\u9762\u7684\u6784\u5efa\u6b65\u9aa4\u4f9d\u8d56\u524d\u671f\u6784\u5efa\u7684\u5e93\u3002 \u4f46\u6700\u6700\u70e6\u4eba\u7684\u662f\uff0c\u8fd0\u884c\u8fd9\u4e2a\u811a\u672c\u771f\u7684\u9700\u8981\u82b1\u5f88\u957f\u7684\u65f6\u95f4\u3002<\/p>\n \u6587\u4ef6\u7cfb\u7edf\u662f\u56fa\u6709\u72b6\u6001<\/strong><\/div>\n \u6211\u4eec\u4e00\u822c\u662f\u901a\u8fc7\u4e00\u79cd\u6709\u72b6\u6001\u7684\u65b9\u5f0f\u4e0e\u6587\u4ef6\u7cfb\u7edf\u8fdb\u884c\u4ea4\u4e92\u7684\u3002\u6211\u4eec\u53ef\u4ee5\u6dfb\u52a0\u3001\u5220\u9664\u6216\u79fb\u52a8\u6587\u4ef6\u3002\u6211\u4eec\u53ef\u4ee5\u4fee\u6539\u6587\u4ef6\u7684 \u6743\u9650\u6216\u8005\u5b83\u7684\u8bbf\u95ee\u65f6\u95f4\u3002\u5927\u90e8\u5206\u72ec\u7acb\u7684\u64cd\u4f5c\u90fd\u53ef\u4ee5\u64a4\u9500\uff0c\u4f8b\u5982\u5c06\u6587\u4ef6\u79fb\u52a8\u5230\u5176\u5b83\u5730\u65b9\u540e\uff0c\u4f60\u53ef\u4ee5\u5c06\u6587\u4ef6\u6062\u590d\u5230\u539f\u6765\u7684\u4f4d\u7f6e\u3002\u4f46\u6211\u4eec\u4e0d\u4f1a\u901a\u8fc7\u5feb\u7167\u7684\u65b9\u5f0f\u6765\u5c06\u5b83\u6062\u590d\u5230 \u539f\u59cb\u72b6\u6001\u3002\u8fd9\u7bc7\u6587\u7ae0\u6211\u5c06\u4f1a\u4ecb\u7ecd\u5982\u4f55\u5728\u8017\u65f6\u8f83\u957f\u7684\u811a\u672c\u4e2d\u5145\u5206\u5229\u7528\u5feb\u7167\u8fd9\u4e00\u7279\u6027\u3002<\/p>\n \u4f7f\u7528\u8054\u5408\u6587\u4ef6\u7cfb\u7edf\u7684\u5feb\u7167<\/strong><\/div>\n Docker\u4f7f\u7528\u7684\u662f\u8054\u5408\u6587\u4ef6\u7cfb\u7edf\u53eb\u505aAUFS\uff08\u8bd1\u8005\u6ce8\uff1a\u7b80\u5355\u6765\u8bf4\u5c31\u662f\u652f\u6301\u5c06\u4e0d\u540c\u76ee\u5f55\u6302\u8f7d\u5230\u540c\u4e00\u4e2a\u865a\u62df\u6587\u4ef6\u7cfb\u7edf\u4e0b\u7684\u6587\u4ef6\u7cfb\u7edf\uff09\u3002\u8054\u5408\u6587\u4ef6\u7cfb\u7edf\u5b9e\u73b0\u4e86Union mount\u3002\u987e\u540d\u601d\u4e49\uff0c\u4e5f\u5c31\u662f\u8bf4\u4e0d\u540c\u7684\u6587\u4ef6\u7cfb\u7edf\u7684\u6587\u4ef6\u548c\u76ee\u5f55\u53ef\u4ee5\u5206\u5c42\u53e0\u52a0\u5728\u5355\u4e2a\u8fde\u8d2f\u6587\u4ef6\u7cfb\u7edf\u4e4b\u4e0a\u3002\u8fd9\u662f\u901a\u8fc7\u5206\u5c42\u7684\u65b9\u5f0f\u5b8c\u6210\u7684\u3002\u5982\u679c\u4e00\u4e2a\u6587\u4ef6\u51fa\u73b0\u5728\u4e24\u4e2a\u6587\u4ef6\u7cfb\u7edf\uff0c\u90a3\u6700\u9ad8\u5c42\u7ea7\u7684\u6587\u4ef6\u624d\u4f1a\u663e\u793a\uff08\u8be5\u6587\u4ef6\u5176\u5b83\u7248\u672c\u4e5f\u662f\u5b58\u5728\u4e8e\u5c42\u7ea7\u4e2d\u7684\uff0c\u4e0d\u4f1a\u6539\u53d8\uff0c\u53ea\u662f\u770b\u4e0d\u5230\u7684\uff09\u3002<\/p>\n \u5728Docker\u4e2d\uff0c\u6bcf\u4e00\u4e2a\u5728Union mount\u8f6c\u54e6\u7ed9\u4f60\u7684\u6587\u4ef6\u7cfb\u7edf\u90fd\u88ab\u79f0\u4e3alayers\uff08\u5c42\uff09\u3002\u4f7f\u7528\u8fd9\u79cd\u6280\u672f\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u5feb\u7167\uff0c\u6bcf\u4e2a\u5feb\u7167\u90fd\u662f\u6240\u6709\u5c42\u7684\u4e00\u4e2aUnion mount\u3002\n<\/p>\n \u751f\u6210\u811a\u672c\u7684\u5feb\u7167<\/strong><\/div>\n \u4f7f\u7528\u5feb\u7167\u53ef\u4ee5\u5e2e\u52a9\u6784\u5efa\u4e00\u4e2a\u957f\u65f6\u8fd0\u884c\u7684\u811a\u672c\u3002\u603b\u7684\u60f3\u6cd5\u662f\uff0c\u5c06\u4e00\u4e2a\u5927\u7684\u811a\u672c\u5206\u89e3\u4e3a\u8bb8\u591a\u5c0f\u7684\u811a\u672c\uff08\u6211\u559c\u6b22\u79f0\u4e4b\u4e3a scriptlets\uff09\uff0c\u5e76\u5355\u72ec\u8fd0\u884c\u8fd9\u4e9b\u5c0f\u7684\u811a\u672c\uff0c\u811a\u672c\u8fd0\u884c\u540e\u4e3a\u5176\u6587\u4ef6\u7cfb\u7edf\u6253\u4e00\u4e2a\u5feb\u7167 \uff08Docker\u4f1a\u81ea\u52a8\u6267\u884c\u6b64\u64cd\u4f5c\uff09\u3002\u5982\u679c\u4f60\u53d1\u73b0\u4e00\u4e2ascriptlet\u8fd0\u884c\u5931\u8d25\uff0c\u4f60\u53ef\u4ee5\u5feb\u901f\u56de\u9000\u5230\u4e0a\u6b21\u7684\u5feb\u7167\uff0c\u7136\u540e\u518d\u8bd5\u4e00\u6b21\u3002\u4e00\u65e6\u4f60\u5b8c\u6210\u811a\u672c\u7684\u6784\u5efa\uff0c\u5e76\u4e14 \u53ef\u4ee5\u4fdd\u8bc1\u811a\u672c\u80fd\u6b63\u5e38\u5de5\u4f5c\uff0c\u90a3\u4f60\u5c31\u53ef\u4ee5\u5c06\u5b83\u5206\u914d\u7ed9\u5176\u5b83\u4e3b\u673a\u3002<\/p>\n \u56de\u8fc7\u5934\u6765\u518d\u5bf9\u6bd4\u4e0b\uff0c\u5982\u679c\u4f60\u6ca1\u6709\u4f7f\u7528\u5feb\u7167\u529f\u80fd\u4e86\uff1f\u5f53\u4f60\u8f9b\u8f9b\u82e6\u82e6\u7b49\u5f85\u4e86\u4e00\u4e2a\u534a\u5c0f\u65f6\u540e\uff0c\u811a\u672c\u5374\u6784\u5efa\u5931\u8d25\u4e86\uff0c\u6211\u60f3\u9664\u4e86\u5c11\u90e8\u5206\u6709\u8010\u5fc3\u7684\u4eba\u5916\uff0c\u5f88\u591a\u4eba\u662f\u4e0d\u60f3\u518d\u6765\u4e00\u6b21\u4e86\uff0c\u5f53\u7136\uff0c\u4f60\u4e5f\u4f1a\u5c3d\u6700\u5927\u52aa\u529b\u628a\u7cfb\u7edf\u6062\u590d\u5230\u5931\u8d25\u524d\u7684\u72b6\u6001\uff0c\u6bd4\u5982\u53ef\u4ee5\u5220\u9664\u4e00\u4e2a\u76ee\u5f55\u6216\u8fd0\u884cmake clean\u3002<\/p>\n \u4f46\u662f\uff0c\u6211\u4eec\u53ef\u80fd\u6ca1\u6709\u771f\u6b63\u5730\u7406\u89e3\u6211\u4eec\u6b63\u5728\u6784\u5efa\u7684\u7ec4\u4ef6\u3002\u5b83\u53ef\u80fd\u6709\u590d\u6742\u7684Makefile\uff0c\u5b83\u4f1a\u628a\u628a\u6587\u4ef6\u653e\u5230\u6587\u4ef6\u7cfb\u7edf\u4e2d\u6211\u4eec\u4e0d\u77e5\u9053\u7684\u5730\u65b9\uff0c\u552f\u4e00\u771f\u6b63\u786e\u5b9a\u7684\u9014\u5f84\u662f\u6062\u590d\u5230\u5feb\u7167\u3002\n<\/p>\n \u4f7f\u7528\u5feb\u7167\u6784\u5efa\u811a\u672c\u7684Docker<\/strong><\/div>\n \u5728\u672c\u8282\u4e2d\uff0c\u6211\u5c06\u4ecb\u7ecd\u6211\u662f\u5982\u4f55\u4f7f\u7528Docker\u5b9e\u73b0GHC7.8.3 ARM\u4ea4\u53c9\u7f16\u8bd1\u5668\u7684\u6784\u5efa\u811a\u672c\u3002Docker\u975e\u5e38\u9002\u5408\u505a\u8fd9\u4ef6\u4e8b\uff0c\u4f46\u5e76\u975e\u5b8c\u7f8e\u3002\u6211\u505a\u4e86\u5f88\u591a\u770b\u8d77\u6765\u6ca1\u7528\u7684\u6216\u8005\u4e0d\u96c5\u7684\u4e8b\u60c5\uff0c\u4f46\u90fd\u662f\u5fc5\u8981\u7684\uff0c\u8fd9\u90fd\u662f\u4e3a\u4e86\u4fdd\u8bc1\u5c06\u5f00\u53d1\u811a\u672c\u7684\u603b\u65f6\u95f4\u964d\u5230\u6700\u4f4e\u9650\u5ea6\u3002\u6784\u5efa\u811a\u672c\u53ef\u4ee5\u5728\u8fd9\u91cc\u627e\u5230\u3002<\/p>\n \u7528Dockerfile\u6784\u5efa<\/strong><\/div>\n Docker\u901a\u8fc7\u8bfb\u53d6Dockerfile\u6765\u6784\u5efa\u955c\u50cf\u3002Dockerfile\u4f1a\u901a\u8fc7\u4e00\u4e9b\u547d\u4ee4\u6765\u5177\u4f53\u6307\u5b9a\u5e94\u8be5\u6267\u884c\u54ea\u4e9b\u52a8\u4f5c\u3002\u5177\u4f53\u4f7f\u7528\u8bf4\u660e\u53ef\u4ee5\u53c2\u8003\u8fd9\u7bc7\u6587\u7ae0\u3002\u5728\u6211\u7684\u811a\u672c\u4e2d\u4e3b\u8981\u7528\u5230WORKDIR\u3001ADD\u548cRUN\u3002ADD\u547d\u4ee4\u975e\u5e38\u6709\u7528\u56e0\u4e3a\u5b83\u53ef\u4ee5\u8ba9\u4f60\u5728\u8fd0\u884c\u4e4b\u524d\u5c06\u5916\u90e8\u6587\u4ef6\u6dfb\u52a0\u5230\u5f53\u524dDocker\u955c\u50cf\u4e2d\u7136\u540e\u8f6c\u6362\u6210\u955c\u50cf\u7684\u6587\u4ef6\u7cfb\u7edf\u3002\u4f60\u53ef\u4ee5\u5728\u8fd9\u91cc\u770b\u5230\u5f88\u591ascriptlets\u6784\u6210\u7684\u6784\u5efa\u811a\u672c\u3002<\/p>\n \u8bbe\u8ba1<\/strong><\/div>\n 1. \u5728RUN\u4e4b\u524dADD scriptlets<\/strong><\/span><\/div>\n \u5982\u679c\u4f60\u5f88\u65e9\u5c31\u5c06\u6240\u6709\u7684scriptletsADD\u5728Dockerfile\uff0c\u60a8\u53ef\u80fd\u4f1a\u9047\u5230\u4ee5\u4e0b\u95ee\u9898\uff1a\u5982\u679c\u4f60\u7684\u811a\u672c\u6784\u5efa\u5931\u8d25\uff0c\u4f60\u56de\u53bb\u4fee\u6539scriptlet\u5e76\u518d\u6b21\u8fd0\u884cdocker build\u3002\u4f46\u662f\u4f60\u53d1\u73b0\uff0cDocker\u5f00\u59cb\u5728\u9996\u6b21\u52a0\u5165scriptlets\u7684\u5730\u65b9\u6784\u5efa\uff01\u8fd9\u6837\u505a\u4f1a\u6d6a\u8d39\u4e86\u5927\u91cf\u7684\u65f6\u95f4\u5e76\u4e14\u8fdd\u80cc\u4e86\u4f7f\u7528\u5feb\u7167\u7684\u76ee\u7684\u3002<\/p>\n \u51fa\u73b0\u8fd9\u79cd\u60c5\u51b5\u7684\u539f\u56e0\u662f\u7531\u4e8eDocker\u5904\u7406\u5b83\u7684\u4e2d\u95f4\u955c\u50cf\uff08\u5feb\u7167\uff09\u7684\u65b9\u5f0f\u3002\u5f53Docker\u901a\u8fc7Dockerfile\u6784\u5efa\u955c\u50cf\u65f6\uff0c\u5b83\u4f1a\u4e0e\u4e2d\u95f4\u955c\u50cf\u6bd4\u8f83\u5f53\u524d\u547d\u4ee4\u662f\u5426\u4e00\u81f4\u3002\u7136\u800c\uff0c\u5728ADD\u547d\u4ee4\u7684\u60c5\u51b5\u4e0b\u88ab\u88c5\u8fdb\u955c\u50cf\u7684\u6587\u4ef6\u91cc\u7684\u5185\u5bb9\u4e5f\u4f1a\u88ab\u68c0\u67e5\u3002\u5982\u679c\u76f8\u5bf9\u4e8e\u73b0\u6709\u7684\u4e2d\u95f4\u955c\u50cf\uff0c\u6587\u4ef6\u5df2\u7ecf\u6539\u53d8\uff0c\u90a3\u4e48Docker\u4e5f\u522b\u65e0\u9009\u62e9\uff0c\u53ea\u80fd\u4ece\u8fd9\u70b9\u5f00\u59cb\u5efa\u7acb\u4e00\u4e2a\u65b0\u7684\u955c\u50cf\u3002\u56e0\u4e3aDocker\u4e0d\u77e5\u9053\u8fd9\u4e9b\u53d8\u5316\u4f1a\u4e0d\u4f1a\u5f71\u54cd\u5230\u6784\u5efa\u3002<\/p>\n \u6b64\u5916\uff0c\u4f7f\u7528RUN\u547d\u4ee4\u8981\u6ce8\u610f\uff0c\u6bcf\u6b21\u8fd0\u884c\u65f6\u5b83\u90fd\u4f1a\u5bfc\u81f4\u6587\u4ef6\u7cfb\u7edf\u6709\u4e0d\u540c\u7684\u66f4\u6539\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0cDocker\u4f1a\u53d1\u73b0\u4e2d\u95f4\u955c\u50cf\u5e76\u4f7f\u7528\u5b83\uff0c\u4f46\u662f\u8fd9\u5c06\u662f\u9519\u8bef\u7684\u3002RUN\u547d\u4ee4\u6bcf\u6b21\u8fd0\u884c\u65f6\u4f1a\u9020\u6210\u6587\u4ef6\u7cfb\u7edf\u76f8\u540c\u7684\u6539\u53d8\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u6211\u786e\u4fdd\u5728\u6211\u7684scriptlets\u6211\u603b\u662f\u4e0b\u8f7d\u4e86\u4e00\u4e2a\u5df2\u77e5\u7248\u672c\u7684\u6587\u4ef6\u4e0e\u4e00\u4e2a\u7279\u5b9aMD5\u6821\u9a8c\u3002<\/p>\n \u5bf9Docker \u6784\u5efa\u7f13\u5b58\u66f4\u8be6\u7ec6\u7684\u89e3\u91ca\u53ef\u4ee5\u5728\u8fd9\u91cc\"https:\/\/docs.docker.com\/articles\/dockerfile_best-practices\/#build-cache\"\u627e\u5230\u3002\n<\/p>\n 2.\u4e0d\u8981\u4f7f\u7528ENV\u547d\u4ee4\u6765\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\uff0c\u8bf7\u4f7f\u7528scriptlet\u3002<\/strong><\/span><\/div>\n \u5b83\u4f3c\u4e4e\u770b\u8d77\u6765\u5f88\u6709\u8bf1\u60d1\u529b\uff1a\u4f7f\u7528ENV\u547d\u4ee4\u6765\u8bbe\u7f6e\u6240\u6709\u6784\u5efa\u811a\u672c\u9700\u8981\u7684\u73af\u5883\u53d8\u91cf\u3002\u4f46\u662f\uff0c\u5b83\u4e0d\u652f\u6301\u53d8\u91cf\u66ff\u6362\u7684\u65b9\u5f0f\uff0c\u4f8b\u5982 ENV BASE=$HOME\/base \u5c06\u8bbe\u7f6eBASE\u7684\u503c\u4e3a$HOME\/base\u7740\u5f88\u53ef\u80fd\u4e0d\u662f\u4f60\u60f3\u8981\u7684\u3002<\/p>\n \u76f8\u53cd\uff0c\u6211\u7528ADD\u547d\u4ee4\u6dfb\u52a0\u4e00\u4e2a\u540d\u4e3aset-env.sh\u6587\u4ef6\u3002\u6b64\u6587\u4ef6\u4f1a\u5305\u542b\u5728\u540e\u7eed\u7684scriptlet\u4e2d\uff1a\n<\/p>\n 1.THIS_DIR=\"$( cd \"$( dirname \"${BASH_SOURCE[0]}\" )\" && pwd )\"\r\n2.source $THIS_DIR\/set-env-1.sh\r\n<\/pre>\n |