{"id":178020,"date":"2020-05-07T10:27:21","date_gmt":"2020-05-07T02:27:21","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=178020"},"modified":"2020-04-24T10:30:13","modified_gmt":"2020-04-24T02:30:13","slug":"docker-file","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/docker-file.html","title":{"rendered":"\u7b80\u5355\u4ecb\u7ecdDocker\u6784\u5efa\u955c\u50cf\u7684\u4e09\u79cd\u65b9\u5f0f"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u672c\u7bc7\u6587\u7ae0\u5c06\u8bb2\u8ff0\u6709\u5173docker\u6784\u5efa\u955c\u50cf\u7684\u5177\u4f53\u64cd\u4f5c\u3002\u672c\u6587\u5c06\u4ecedocker\u6784\u5efa\u955c\u50cf\u7684\u65b9\u5f0f\u5165\u624b\uff0c\u9010\u4e00\u8bb2\u8ff0\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u6784\u5efa\u955c\u50cf\u7684\u4e09\u79cd\u65b9\u5f0f:<\/p>\n

    \n
  • Dockerfile<\/li>\n
  • \u57fa\u4e8e\u5df2\u6709\u7684\u955c\u50cf\u5bb9\u5668\u8fdb\u884c\u521b\u5efa<\/li>\n
  • \u57fa\u4e8e\u672c\u5730\u6a21\u677f\u521b\u5efa<\/li>\n<\/ul>\n

    \u9996\u5148\uff0c\u6211\u4eec\u4e3a\u4ec0\u4e48\u9700\u8981\u6784\u5efadocker\u955c\u50cf\uff1f\u53ea\u6709\u660e\u767d\u4e86\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u624d\u77e5\u9053\u6784\u5efadocker\u955c\u50cf\u7684\u610f\u4e49\u6216\u8005\u8bf4\u4ef7\u503c\u662f\u4ec0\u4e48\u3002<\/p>\n

    \u7b54\u6848\u5176\u5b9e\u5f88\u7b80\u5355\uff1adocker\u955c\u50cf\u662fdocker\u7684\u4e09\u5927\u6838\u5fc3\u4e4b\u4e00\uff0c\u4e5f\u662f\u5e94\u7528\u53d1\u5e03\u7684\u6807\u51c6\u683c\u5f0f\uff0c\u4e00\u4e2a\u5b8c\u6574\u7684docker\u955c\u50cf\u53ef\u4ee5\u652f\u6301\u4e00\u4e2adocker\u5bb9\u5668\u7684\u8fd0\u884c\u3002\u6211\u4eec\u5728\u5bb9\u5668\u8fdb\u884c\u76f8\u5173\u7684\u64cd\u4f5c\uff0c\u4f8b\u5982\u5b89\u88c5\u5e94\u7528\u670d\u52a1\uff0c\u5047\u8bbe\u67d0\u4e2a\u4e1a\u52a1\u7684\u9700\u6c42\u6070\u597d\u9700\u8981\u65b9\u624d\u5b89\u88c5\u914d\u7f6e\u597d\u7684\u5e94\u7528\u670d\u52a1\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5c06\u73af\u5883\u4ee5\u53ca\u642d\u5efa\u7684\u670d\u52a1\u751f\u6210\u65b0\u7684\u955c\u50cf\u63d0\u4f9b\u51fa\u53bb\u3002<\/p>\n

    \u4e0b\u9762\u6211\u4eec\u6765\u5177\u4f53\u770b\u770b\u8fd9\u4e09\u79cd\u65b9\u5f0f\u7684\u6784\u5efa\u624b\u6cd5\u3002<\/p>\n

    \u57fa\u4e8eDockerfile\u6784\u5efa\u955c\u50cf<\/strong><\/div>\n
    \u4ec0\u4e48\u662fDockerfile\uff1f<\/strong><\/span><\/div>\n

    \u200b Dockerfile\u6784\u5efa\u955c\u50cf\u7684\u65b9\u5f0f\u5c31\u76ee\u524d\u800c\u8a00\u662f\u4f7f\u7528\u6700\u4e3a\u5e7f\u6cdb\u7684\uff0c\u8fd9\u662f\u4e00\u79cd\u53ef\u4ee5\u81ea\u52a8\u5316\u751f\u6210\u955c\u50cf\u7684\u4e00\u79cd\u65b9\u5f0f\uff0c\u5c31\u7c7b\u4f3cshell\u811a\u672c\u4e00\u6837\uff0c\u4e00\u4e2a\u811a\u672c\u6267\u884c\u5b8c\u5c31\u53ef\u4ee5\u5c06\u4e00\u4e2a\u670d\u52a1\u5b89\u88c5\u914d\u7f6e\u597d\uff0c\u652f\u6301\u6b63\u5e38\u4f7f\u7528\u4e86\u3002Dockerfile\u4e5f\u662f\u4e00\u6837\uff0c\u4e5f\u662f\u7531\u4e00\u7ec4\u6307\u4ee4\u7ec4\u6210\u7684\u6587\u4ef6\uff0c\u5176\u4e2d\u6bcf\u6761\u6307\u4ee4\u5bf9\u5e94Linux\u4e2d\u7684\u4e00\u6761\u547d\u4ee4\uff0cDocker\u7a0b\u5e8f\u5c06\u901a\u8fc7\u8bfb\u53d6Dockerfile\u4e2d\u7684\u6307\u4ee4\u6700\u7ec8\u751f\u6210\u955c\u50cf\u3002<\/p>\n

    \u200b Dockerfile\u53ef\u4ee5\u8ba4\u4e3a\u662f\u4e00\u4e2a\u666e\u901a\u6587\u4ef6\uff0c\u5176\u7ed3\u6784\u5927\u81f4\u5206\u4e3a\u56db\u4e2a\u90e8\u5206\uff1a\u57fa\u7840\u955c\u50cf\u4fe1\u606f\u3001\u7ef4\u62a4\u8005\u4fe1\u606f\u3001\u955c\u50cf\u64cd\u4f5c\u6307\u4ee4\u4ee5\u53ca\u5bb9\u5668\u542f\u52a8\u65f6\u6267\u884c\u6307\u4ee4\u3002\u5e76\u4e14\u5728Dockerfile\u4e2d\u652f\u6301\u4ee5\u201c#\u201d\u5f00\u5934\u7684\u6ce8\u91ca\u3002<\/p>\n

    Docker\u955c\u50cf\u7684\u5206\u5c42<\/strong><\/span><\/div>\n
      \n
    1. Dockerfile\u4e2d\u7684\u6bcf\u4e2a\u6307\u4ee4\u90fd\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u955c\u50cf\u5c42<\/li>\n
    2. \u955c\u50cf\u5c42\u5c06\u88ab\u7f13\u5b58\u548c\u590d\u7528<\/li>\n
    3. \u5f53Dockerfile\u7684\u6307\u4ee4\u88ab\u4fee\u6539\u4e86\uff0c\u590d\u5236\u7684\u6587\u4ef6\u53d8\u5316\u4e86\uff0c\u6216\u8005\u6784\u5efa\u955c\u50cf\u65f6\u6307\u5b9a\u7684\u53d8\u91cf\u4e0d\u540c\u4e86\uff0c\u5bf9\u5e94\u7684\u955c\u50cf\u5c42\u7f13\u5b58\u5c31\u4f1a\u5931\u6548<\/li>\n
    4. \u67d0\u4e00\u5c42\u7684\u955c\u50cf\u7f13\u5b58\u5931\u6548\u540e\uff0c\u5176\u4e4b\u540e\u7684\u955c\u50cf\u5c42\u7f13\u5b58\u90fd\u4f1a\u968f\u4e4b\u5931\u6548<\/li>\n
    5. \u955c\u50cf\u5c42\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u5982\u679c\u5728\u67d0\u4e00\u5c42\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u6587\u4ef6\uff0c\u7136\u540e\u5728\u4e0b\u4e00\u5c42\u4e2d\u5220\u9664\u5219\u955c\u50cf\u4e2d\u4f9d\u7136\u4f1a\u5305\u542b\u8be5\u6587\u4ef6<\/li>\n<\/ol>\n
      Dockerfile\u7f16\u5199\u89c4\u5219<\/strong><\/span><\/div>\n

      Dockerfile\u4e2d\u662f\u57fa\u4e8e\u5176\u6307\u4ee4\u8fdb\u884c\u7f16\u5199\u7684\uff0c\u5176\u89c4\u5219\u53ef\u4ee5\u53c2\u8003\u4e0b\u9762\u7684\u8868\u683c\uff0c\u5f53\u7136\uff0c\u5728\u7f16\u5199Dockerfile\u65f6\uff0c\u5176\u683c\u5f0f\u662f\u9700\u8981\u4e25\u683c\u9075\u5faa\u7684\uff1a<\/p>\n

      \u9664\u6ce8\u91ca\u5916\uff0c\u7b2c\u4e00\u884c\u5fc5\u987b\u4f7f\u7528FROM\u6307\u4ee4\u6240\u57fa\u4e8e\u7684\u955c\u50cf\u540d\u79f0\uff1b\u4e4b\u540e\u4f7f\u7528MAINTAINER\u6307\u660e\u7ef4\u62a4\u4fe1\u606f\uff1b\u7136\u540e\u5c31\u662f\u4e00\u7cfb\u5217\u955c\u50cf\u64cd\u4f5c\u6307\u4ee4\uff0c\u5982RUN\u3001 ADD\u7b49\uff1b\u6700\u540e\u4fbf\u662fCMD\u6307\u4ee4\u6765\u6307\u5b9a\u542f\u52a8\u5bb9\u5668\u65f6\u8981\u8fd0\u884c\u7684\u547d\u4ee4\u64cd\u4f5c\u3002\u5176\u4e2dRUN\u6307\u4ee4\u53ef\u4ee5\u4f7f\u7528\u591a\u6761\uff0cCMD\u53ea\u6709\u6700\u540e\u4e00\u6761\u53ef\u4ee5\u751f\u6548\uff01<\/p>\n

      \"\"<\/p>\n

      \u200b\u672c\u6587\u4e3b\u8981\u662f\u8bb2\u8ff0docker\u6784\u5efa\u955c\u50cf\u7684\u4e09\u79cd\u65b9\u5f0f\uff0cDockerfile\u7684\u6784\u5efa\u5177\u4f53\u8fc7\u7a0b\u7b14\u8005\u5c06\u5728\u4e4b\u540e\u7684\u6587\u7ae0\u4e2d\u901a\u8fc7\u5404\u79cd\u5b9e\u9645\u6848\u4f8b\u6765\u8bb2\u8ff0\u6f14\u793a\uff0c\u672c\u6587\u6682\u65f6\u7ed9\u51fa\u4e00\u4e2aDockerfile\u6587\u4ef6\u7684\u4f8b\u5b50\u7ed9\u5927\u5bb6\u53c2\u8003\u3002<\/p>\n

      [root@localhost ~]# cat Dockerfile\r\n#\u52a1\u5fc5\u5148\u6307\u660e\u57fa\u4e8e\u7684\u57fa\u7840\u955c\u50cf\r\nFORM CentOS:7\r\n#\u7ef4\u62a4\u8be5\u955c\u50cf\u7684\u7528\u6237\u4fe1\u606f\uff08\u81ea\u5b9a\u4e49\uff09\r\nMAINTAINER lokott@123.com\r\n#\u955c\u50cf\u64cd\u4f5c\u547d\u4ee4\r\nRUN yum -y update\r\nRUN yum -y install openssh-server\r\nRUN sed -i 's\/UsePAM yes\/UsePAM no\/g' \/etc\/ssh\/sshd_config\r\nRUN ssh-keygen -t dsa -f \/etc\/ssh\/ssh_host_dsa_key\r\nRUN ssh-keygen -t rsa -f \/etc\/ssh\/ssh_host_rsa_key\r\n#\u5f00\u542f\u7aef\u53e3\r\nEXPOSE 22\r\n#\u542f\u52a8\u5bb9\u5668\u65f6\u6267\u884c\u6307\u4ee4\r\nCMD [\"\/usr\/sbin\/sshd\",\"-D\"]<\/pre>\n
      \u57fa\u4e8e\u5df2\u6709\u955c\u50cf\u6784\u5efa\u955c\u50cf<\/strong><\/div>\n

      \u4e0a\u8ff0\u7684Dockerfile\u7684\u6784\u5efa\u955c\u50cf\u7684\u65b9\u5f0f\u662f\u81ea\u52a8\u5316\u8fdb\u884c\u7684\uff0c\u90a3\u4e48\u624b\u52a8\u6784\u5efa\u7684\u65b9\u5f0f\u5c31\u662f\u5269\u4e0b\u7684\u4e24\u79cd\u4e86\u3002<\/p>\n

      \u57fa\u4e8e\u5df2\u6709\u7684\u955c\u50cf\u6784\u5efa\u4e3b\u8981\u662f\u901a\u8fc7docker commit \u547d\u4ee4\u6765\u6784\u5efa\u65b0\u7684\u955c\u50cf\uff0c\u5176\u5b9e\u8d28\u5c31\u662f\u5c06\u4e00\u4e2a\u5bb9\u5668\u91cc\u9762\u8fd0\u884c\u7684\u7a0b\u5e8f\u4ee5\u53ca\u8be5\u7a0b\u5e8f\u7684\u8fd0\u884c\u73af\u5883\u6253\u5305\u8d77\u6765\u751f\u6210\u65b0\u7684\u955c\u50cf<\/strong>\u3002<\/p>\n

      docker commit\u7684\u8bed\u6cd5\u89c4\u5219\u53ca\u53ef\u9009\u9879\u4ecb\u7ecd<\/strong><\/span><\/div>\n
      Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]\r\n\r\nCreate a new image from a container's changes\r\n\r\nOptions:\r\n-a, --author string Author (e.g., \"John Hannibal Smith <hannibal@a-team.com>\")#\u4f5c\u8005\u4fe1\u606f\r\n-c, --change list Apply Dockerfile instruction to the created image\r\n-m, --message string Commit message #\u8bf4\u660e\u4fe1\u606f\r\n-p, --pause Pause container during commit (default true) #\u751f\u6210\u8fc7\u7a0b\u4e2d\u505c\u6b62\u5bb9\u5668\u7684\u8fd0\u884c<\/pre>\n

      \u6848\u4f8b\uff1a\u57fa\u4e8e\u539f\u6709\u955c\u50cf\u6784\u5efa\u65b0\u7684\u955c\u50cf\uff08\u770b\u5b9e\u8d28\u539f\u7406\uff09<\/p>\n

      1.\u521b\u5efa\u4e00\u4e2a\u5bb9\u5668<\/strong><\/span><\/div>\n
      [root@localhost ~]# docker create -it nginx \/bin\/bash\r\nUnable to find image 'nginx:latest' locally\r\nlatest: Pulling from library\/nginx\r\n123275d6e508: Pull complete\r\n6cd6a943ce27: Pull complete\r\na50b5ac4a7fb: Pull complete\r\nDigest: sha256:d81f010955749350ef31a119fb94b180fde8b2f157da351ff5667ae037968b28\r\nStatus: Downloaded newer image for nginx:latest\r\nfb9a4cdb1b79a5e4d82177afdbe8c1f1956888733979f15ae562e0013f4e0f29\r\n\r\n[root@localhost ~]# docker ps -a\r\nCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES\r\nfb9a4cdb1b79 nginx \"\/bin\/bash\" 17 seconds ago Created vibrant_heisenberg<\/pre>\n
      2.\u57fa\u4e8e\u8be5\u5bb9\u5668\u4f7f\u7528docker commit \u547d\u4ee4\u521b\u5efa\u65b0\u7684\u955c\u50cf<\/strong><\/span><\/div>\n
      [root@localhost ~]# docker commit -m new_image -a lokott fb9a4cdb1b79 lokott:demo\r\nsha256:bb848fe7eb3655a07e6577e05bc04ab6404549a24c49fa5d3161ceb767a048f2\r\n[root@localhost ~]# docker images | grep lokott\r\nlokott demo bb848fe7eb36 26 seconds ago 127MB<\/pre>\n
      \u57fa\u4e8e\u672c\u5730\u6a21\u677f\u6784\u5efa\u955c\u50cf<\/strong><\/div>\n

      \u8be5\u65b9\u5f0f\u662f\u901a\u8fc7\u5bfc\u5165\u64cd\u4f5c\u7cfb\u7edf\u6a21\u677f\u6587\u4ef6\u751f\u6210\u955c\u50cf\uff0c\u6a21\u677f\u53ef\u4ee5\u4eceOPENVZ\u5f00\u6e90\u9879\u76ee\u4e0b\u8f7d\uff0c\u4e0b\u8f7d\u5730\u5740\u4e3a\uff1ahttp:\/\/openvz.org\/Download\/template\/precreated<\/p>\n

      \u4e0b\u9762\u76f4\u63a5\u7ed9\u51fa\u4f8b\u5b50\u2014\u2014\u5c06debian\u6a21\u677f\u538b\u7f29\u5305\u5bfc\u5165\u4e3a\u672c\u5730\u955c\u50cf<\/p>\n

      [root@localhost ~]# ls\r\nanaconda-ks.cfg docker.sh \u516c\u5171 \u89c6\u9891 \u6587\u6863 \u97f3\u4e50\r\ndebian-7.0-x86-minimal.tar.gz initial-setup-ks.cfg \u6a21\u677f \u56fe\u7247 \u4e0b\u8f7d \u684c\u9762\r\n[root@localhost ~]# cat debian-7.0-x86-minimal.tar.gz | docker import - lokott:new\r\nsha256:df2ce16066413515db7db8a76243fcaebbd904d10304ab467fefde852dddd9a8\r\n[root@localhost ~]# docker images | grep new\r\nlokott new df2ce1606641 14 seconds ago 215MB<\/pre>\n

      \u672c\u6587\u8bb2\u8ff0\u4e86docker\u6784\u5efa\u955c\u50cf\u7684\u4e09\u79cd\u65b9\u5f0f\uff0c\u6700\u91cd\u8981\u7684\u662f\u4e0a\u8ff0\u7684\u7b2c\u4e00\u79cd\u65b9\u5f0f\u4f7f\u7528\u7684\u975e\u5e38\u591a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

      \u6784\u5efa\u955c\u50cf\u7684\u4e09\u79cd\u65b9\u5f0f: Dockerfile \u57fa\u4e8e\u5df2\u6709\u7684\u955c\u50cf\u5bb9\u5668\u8fdb\u884c\u521b\u5efa \u57fa\u4e8e\u672c\u5730\u6a21\u677f\u521b\u5efa \u9996\u5148\uff0c\u6211\u4eec\u4e3a\u4ec0\u4e48\u9700\u8981 […]<\/p>\n","protected":false},"author":63,"featured_media":178022,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-178020","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\/178020","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=178020"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/178020\/revisions"}],"predecessor-version":[{"id":178026,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/178020\/revisions\/178026"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/178022"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=178020"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=178020"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=178020"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}