{"id":897,"date":"2020-11-28T09:44:03","date_gmt":"2020-11-28T01:44:03","guid":{"rendered":"https:\/\/www.linuxdevops.cn\/?p=897"},"modified":"2023-03-28T11:19:04","modified_gmt":"2023-03-28T03:19:04","slug":"detailed-explanation-of-dockerfile-instruction","status":"publish","type":"post","link":"https:\/\/www.linuxdevops.cn\/2020\/11\/detailed-explanation-of-dockerfile-instruction\/","title":{"rendered":"Dockerfile \u6307\u4ee4\u8be6\u89e3"},"content":{"rendered":"
Dockerfile \u662f\u4e00\u4e2a\u7528\u6765\u6784\u5efa\u955c\u50cf\u7684\u6587\u672c\u6587\u4ef6\uff0c\u6587\u672c\u5185\u5bb9\u5305\u542b\u4e86\u4e00\u6761\u6761\u6784\u5efa\u955c\u50cf\u6240\u9700\u7684\u6307\u4ee4\u548c\u8bf4\u660e\u3002<\/p>\n
FROM\uff1a\u5b9a\u5236\u7684\u955c\u50cf\u90fd\u662f\u57fa\u4e8e FROM \u7684\u955c\u50cf\uff0c\u8fd9\u91cc\u7684 nginx \u5c31\u662f\u5b9a\u5236\u9700\u8981\u7684\u57fa\u7840\u955c\u50cf\u3002<\/p>\n
RUN\uff1a\u7528\u4e8e\u6267\u884c\u540e\u9762\u8ddf\u7740\u7684\u547d\u4ee4\u884c\u547d\u4ee4\u3002\u6709\u4ee5\u4e0b\u4fe9\u79cd\u683c\u5f0f\uff1a<\/p>\n
shell \u683c\u5f0f\uff1a<\/p>\n
RUN <\u547d\u4ee4\u884c\u547d\u4ee4>\n# <\u547d\u4ee4\u884c\u547d\u4ee4> \u7b49\u540c\u4e8e\uff0c\u5728\u7ec8\u7aef\u64cd\u4f5c\u7684 shell \u547d\u4ee4\u3002\n<\/code><\/pre>\n <\/p>\n
exec \u683c\u5f0f\uff1a<\/p>\n
RUN ["\u53ef\u6267\u884c\u6587\u4ef6", "\u53c2\u65701", "\u53c2\u65702"]\n# \u4f8b\u5982\uff1a\n# RUN [".\/test.php", "dev", "offline"] \u7b49\u4ef7\u4e8e RUN .\/test.php dev offline\n<\/code><\/pre>\n <\/p>\n
\u6ce8\u610f\uff1aDockerfile \u7684\u6307\u4ee4\u6bcf\u6267\u884c\u4e00\u6b21\u90fd\u4f1a\u5728 docker \u4e0a\u65b0\u5efa\u4e00\u5c42\u3002\u6240\u4ee5\u8fc7\u591a\u65e0\u610f\u4e49\u7684\u5c42\uff0c\u4f1a\u9020\u6210\u955c\u50cf\u81a8\u80c0\u8fc7\u5927\u3002\u4f8b\u5982\uff1a\n\nFROM centos\nRUN yum install wget\nRUN wget -O redis.tar.gz "http:\/\/download.redis.io\/releases\/redis-5.0.3.tar.gz"\nRUN tar -xvf redis.tar.gz\n\u4ee5\u4e0a\u6267\u884c\u4f1a\u521b\u5efa 3 \u5c42\u955c\u50cf\u3002\u53ef\u7b80\u5316\u4e3a\u4ee5\u4e0b\u683c\u5f0f\uff1a\nFROM centos\nRUN yum install wget \\\n && wget -O redis.tar.gz "http:\/\/download.redis.io\/releases\/redis-5.0.3.tar.gz" \\\n && tar -xvf redis.tar.gz\n\u5982\u4e0a\uff0c\u4ee5 && \u7b26\u53f7\u8fde\u63a5\u547d\u4ee4\uff0c\u8fd9\u6837\u6267\u884c\u540e\uff0c\u53ea\u4f1a\u521b\u5efa 1 \u5c42\u955c\u50cf\u3002\n<\/code><\/pre>\n\u6784\u5efa\u955c\u50cf<\/h3>\n$ docker build -t nginx:test .\n<\/code><\/pre>\n\u4e0a\u4e0b\u6587\u8def\u5f84<\/h3>\n
\u6307\u4ee4\u6700\u540e\u4e00\u4e2a . \u662f\u4e0a\u4e0b\u6587\u8def\u5f84\uff0c\u90a3\u4e48\u4ec0\u4e48\u662f\u4e0a\u4e0b\u6587\u8def\u5f84\u5462\uff1f<\/p>\n
\u4e0a\u4e0b\u6587\u8def\u5f84\uff0c\u662f\u6307 docker \u5728\u6784\u5efa\u955c\u50cf\uff0c\u6709\u65f6\u5019\u60f3\u8981\u4f7f\u7528\u5230\u672c\u673a\u7684\u6587\u4ef6\uff08\u6bd4\u5982\u590d\u5236\uff09\uff0cdocker build \u547d\u4ee4\u5f97\u77e5\u8fd9\u4e2a\u8def\u5f84\u540e\uff0c\u4f1a\u5c06\u8def\u5f84\u4e0b\u7684\u6240\u6709\u5185\u5bb9\u6253\u5305\u3002<\/p>\n
\u89e3\u6790\uff1a\u7531\u4e8e docker \u7684\u8fd0\u884c\u6a21\u5f0f\u662f C\/S\u3002\u6211\u4eec\u672c\u673a\u662f C\uff0cdocker \u5f15\u64ce\u662f S\u3002\u5b9e\u9645\u7684\u6784\u5efa\u8fc7\u7a0b\u662f\u5728 docker \u5f15\u64ce\u4e0b\u5b8c\u6210\u7684\uff0c\u6240\u4ee5\u8fd9\u4e2a\u65f6\u5019\u65e0\u6cd5\u7528\u5230\u6211\u4eec\u672c\u673a\u7684\u6587\u4ef6\u3002\u8fd9\u5c31\u9700\u8981\u628a\u6211\u4eec\u672c\u673a\u7684\u6307\u5b9a\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6\u4e00\u8d77\u6253\u5305\u63d0\u4f9b\u7ed9 docker \u5f15\u64ce\u4f7f\u7528\u3002<\/p>\n
\u5982\u679c\u672a\u8bf4\u660e\u6700\u540e\u4e00\u4e2a\u53c2\u6570\uff0c\u90a3\u4e48\u9ed8\u8ba4\u4e0a\u4e0b\u6587\u8def\u5f84\u5c31\u662f Dockerfile \u6240\u5728\u7684\u4f4d\u7f6e\u3002<\/p>\n
\u6ce8\u610f\uff1a\u4e0a\u4e0b\u6587\u8def\u5f84\u4e0b\u4e0d\u8981\u653e\u65e0\u7528\u7684\u6587\u4ef6\uff0c\u56e0\u4e3a\u4f1a\u4e00\u8d77\u6253\u5305\u53d1\u9001\u7ed9 docker \u5f15\u64ce\uff0c\u5982\u679c\u6587\u4ef6\u8fc7\u591a\u4f1a\u9020\u6210\u8fc7\u7a0b\u7f13\u6162\u3002<\/p>\n
<\/p>\n
\u6307\u4ee4\u8be6\u89e3<\/h2>\nCOPY<\/h3>\n
\u590d\u5236\u6307\u4ee4\uff0c\u4ece\u4e0a\u4e0b\u6587\u76ee\u5f55\u4e2d\u590d\u5236\u6587\u4ef6\u6216\u8005\u76ee\u5f55\u5230\u5bb9\u5668\u91cc\u6307\u5b9a\u8def\u5f84\u3002<\/p>\n
\u683c\u5f0f\uff1a<\/p>\n
COPY [--chown=<user>:<group>] <\u6e90\u8def\u5f841>... <\u76ee\u6807\u8def\u5f84>\nCOPY [--chown=<user>:<group>] ["<\u6e90\u8def\u5f841>",... "<\u76ee\u6807\u8def\u5f84>"]\n<\/code><\/pre>\n[--chown=:]\uff1a\u53ef\u9009\u53c2\u6570\uff0c\u7528\u6237\u6539\u53d8\u590d\u5236\u5230\u5bb9\u5668\u5185\u6587\u4ef6\u7684\u62e5\u6709\u8005\u548c\u5c5e\u7ec4\u3002<\/p>\n<\u6e90\u8def\u5f84>\uff1a\u6e90\u6587\u4ef6\u6216\u8005\u6e90\u76ee\u5f55\uff0c\u8fd9\u91cc\u53ef\u4ee5\u662f\u901a\u914d\u7b26\u8868\u8fbe\u5f0f\uff0c\u5176\u901a\u914d\u7b26\u89c4\u5219\u8981\u6ee1\u8db3 Go \u7684 filepath.Match \u89c4\u5219\u3002\u4f8b\u5982\uff1a<\/p>\n
COPY hom* \/mydir\/\nCOPY hom?.txt \/mydir\/\n<\/code><\/pre>\n<\u76ee\u6807\u8def\u5f84>\uff1a\u5bb9\u5668\u5185\u7684\u6307\u5b9a\u8def\u5f84\uff0c\u8be5\u8def\u5f84\u4e0d\u7528\u4e8b\u5148\u5efa\u597d\uff0c\u8def\u5f84\u4e0d\u5b58\u5728\u7684\u8bdd\uff0c\u4f1a\u81ea\u52a8\u521b\u5efa\u3002<\/p>\n
ADD<\/h3>\n
ADD \u6307\u4ee4\u548c COPY \u7684\u4f7f\u7528\u683c\u5f0f\u4e00\u81f4\uff08\u540c\u6837\u9700\u6c42\u4e0b\uff0c\u5b98\u65b9\u63a8\u8350\u4f7f\u7528 COPY\uff09\u3002\u529f\u80fd\u4e5f\u7c7b\u4f3c\uff0c\u4e0d\u540c\u4e4b\u5904\u5982\u4e0b\uff1a<\/p>\n
\n- ADD \u7684\u4f18\u70b9\uff1a\u5728\u6267\u884c <\u6e90\u6587\u4ef6> \u4e3a tar \u538b\u7f29\u6587\u4ef6\u7684\u8bdd\uff0c\u538b\u7f29\u683c\u5f0f\u4e3a gzip, bzip2 \u4ee5\u53ca xz \u7684\u60c5\u51b5\u4e0b\uff0c\u4f1a\u81ea\u52a8\u590d\u5236\u5e76\u89e3\u538b\u5230 <\u76ee\u6807\u8def\u5f84>\u3002<\/li>\n
- ADD \u7684\u7f3a\u70b9\uff1a\u5728\u4e0d\u89e3\u538b\u7684\u524d\u63d0\u4e0b\uff0c\u65e0\u6cd5\u590d\u5236 tar \u538b\u7f29\u6587\u4ef6\u3002\u4f1a\u4ee4\u955c\u50cf\u6784\u5efa\u7f13\u5b58\u5931\u6548\uff0c\u4ece\u800c\u53ef\u80fd\u4f1a\u4ee4\u955c\u50cf\u6784\u5efa\u53d8\u5f97\u6bd4\u8f83\u7f13\u6162\u3002\u5177\u4f53\u662f\u5426\u4f7f\u7528\uff0c\u53ef\u4ee5\u6839\u636e\u662f\u5426\u9700\u8981\u81ea\u52a8\u89e3\u538b\u6765\u51b3\u5b9a\u3002<\/li>\n<\/ul>\n
CMD<\/h3>\n
\u7c7b\u4f3c\u4e8e RUN \u6307\u4ee4\uff0c\u7528\u4e8e\u8fd0\u884c\u7a0b\u5e8f\uff0c\u4f46\u4e8c\u8005\u8fd0\u884c\u7684\u65f6\u95f4\u70b9\u4e0d\u540c:<\/p>\n
\n- CMD \u5728docker run \u65f6\u8fd0\u884c\u3002<\/li>\n
- RUN \u662f\u5728 docker build\u3002<\/li>\n<\/ul>\n
\u4f5c\u7528\uff1a\u4e3a\u542f\u52a8\u7684\u5bb9\u5668\u6307\u5b9a\u9ed8\u8ba4\u8981\u8fd0\u884c\u7684\u7a0b\u5e8f\uff0c\u7a0b\u5e8f\u8fd0\u884c\u7ed3\u675f\uff0c\u5bb9\u5668\u4e5f\u5c31\u7ed3\u675f\u3002CMD \u6307\u4ee4\u6307\u5b9a\u7684\u7a0b\u5e8f\u53ef\u88ab docker run \u547d\u4ee4\u884c\u53c2\u6570\u4e2d\u6307\u5b9a\u8981\u8fd0\u884c\u7684\u7a0b\u5e8f\u6240\u8986\u76d6\u3002<\/p>\n
\u6ce8\u610f\uff1a\u5982\u679c Dockerfile \u4e2d\u5982\u679c\u5b58\u5728\u591a\u4e2a CMD \u6307\u4ee4\uff0c\u4ec5\u6700\u540e\u4e00\u4e2a\u751f\u6548\u3002<\/p>\n
\u683c\u5f0f\uff1a<\/p>\n
CMD <shell \u547d\u4ee4> \nCMD ["<\u53ef\u6267\u884c\u6587\u4ef6\u6216\u547d\u4ee4>","<param1>","<param2>",...] \nCMD ["<param1>","<param2>",...] # \u8be5\u5199\u6cd5\u662f\u4e3a ENTRYPOINT \u6307\u4ee4\u6307\u5b9a\u7684\u7a0b\u5e8f\u63d0\u4f9b\u9ed8\u8ba4\u53c2\u6570\n<\/code><\/pre>\n\u63a8\u8350\u4f7f\u7528\u7b2c\u4e8c\u79cd\u683c\u5f0f\uff0c\u6267\u884c\u8fc7\u7a0b\u6bd4\u8f83\u660e\u786e\u3002\u7b2c\u4e00\u79cd\u683c\u5f0f\u5b9e\u9645\u4e0a\u5728\u8fd0\u884c\u7684\u8fc7\u7a0b\u4e2d\u4e5f\u4f1a\u81ea\u52a8\u8f6c\u6362\u6210\u7b2c\u4e8c\u79cd\u683c\u5f0f\u8fd0\u884c\uff0c\u5e76\u4e14\u9ed8\u8ba4\u53ef\u6267\u884c\u6587\u4ef6\u662f sh\u3002<\/p>\n<\/p>\n
ENTRYPOINT<\/h3>\n
\u7c7b\u4f3c\u4e8e CMD \u6307\u4ee4\uff0c\u4f46\u5176\u4e0d\u4f1a\u88ab docker run \u7684\u547d\u4ee4\u884c\u53c2\u6570\u6307\u5b9a\u7684\u6307\u4ee4\u6240\u8986\u76d6\uff0c\u800c\u4e14\u8fd9\u4e9b\u547d\u4ee4\u884c\u53c2\u6570\u4f1a\u88ab\u5f53\u4f5c\u53c2\u6570\u9001\u7ed9 ENTRYPOINT \u6307\u4ee4\u6307\u5b9a\u7684\u7a0b\u5e8f\u3002<\/p>\n
\u4f46\u662f, \u5982\u679c\u8fd0\u884c docker run \u65f6\u4f7f\u7528\u4e86 --entrypoint \u9009\u9879\uff0c\u6b64\u9009\u9879\u7684\u53c2\u6570\u53ef\u5f53\u4f5c\u8981\u8fd0\u884c\u7684\u7a0b\u5e8f\u8986\u76d6 ENTRYPOINT \u6307\u4ee4\u6307\u5b9a\u7684\u7a0b\u5e8f\u3002<\/p>\n
\u4f18\u70b9\uff1a\u5728\u6267\u884c docker run \u7684\u65f6\u5019\u53ef\u4ee5\u6307\u5b9a ENTRYPOINT \u8fd0\u884c\u6240\u9700\u7684\u53c2\u6570\u3002<\/p>\n
\u6ce8\u610f\uff1a\u5982\u679c Dockerfile \u4e2d\u5982\u679c\u5b58\u5728\u591a\u4e2a ENTRYPOINT \u6307\u4ee4\uff0c\u4ec5\u6700\u540e\u4e00\u4e2a\u751f\u6548\u3002<\/p>\n
\u683c\u5f0f\uff1a<\/p>\n
ENTRYPOINT ["<executeable>","<param1>","<param2>",...]\n<\/code><\/pre>\n\u53ef\u4ee5\u642d\u914d CMD \u547d\u4ee4\u4f7f\u7528\uff1a\u4e00\u822c\u662f\u53d8\u53c2\u624d\u4f1a\u4f7f\u7528 CMD \uff0c\u8fd9\u91cc\u7684 CMD \u7b49\u4e8e\u662f\u5728\u7ed9 ENTRYPOINT \u4f20\u53c2\uff0c\u4ee5\u4e0b\u793a\u4f8b\u4f1a\u63d0\u5230\u3002<\/p>\n
\u793a\u4f8b\uff1a<\/p>\n
\u5047\u8bbe\u5df2\u901a\u8fc7 Dockerfile \u6784\u5efa\u4e86 nginx:test \u955c\u50cf\uff1a<\/p>\n
FROM nginx\n\nENTRYPOINT ["nginx", "-c"] # \u5b9a\u53c2\nCMD ["\/etc\/nginx\/nginx.conf"] # \u53d8\u53c2 \n<\/code><\/pre>\n