{"id":600,"date":"2019-03-01T15:22:12","date_gmt":"2019-03-01T07:22:12","guid":{"rendered":"https:\/\/www.zhuyanbin.com\/?p=600"},"modified":"2019-03-01T16:58:57","modified_gmt":"2019-03-01T08:58:57","slug":"docker%e5%a4%9a%e6%ad%a5%e9%aa%a4%e5%88%9b%e5%bb%ba","status":"publish","type":"post","link":"https:\/\/www.yanbin888.com\/?p=600","title":{"rendered":"Docker\u591a\u6b65\u9aa4\u521b\u5efa\u955c\u50cf"},"content":{"rendered":"\n<p>\u81ea 17.05 \u7248\u672c\u5f00\u59cb\uff0cDocker \u652f\u6301\u591a\u6b65\u9aa4\u955c\u50cf\u521b\u5efa( Multi-stage build)\u7279\u6027\uff0c\u53ef\u4ee5\u7cbe\u7b80\u6700\u7ec8\u751f\u6210\u7684\u955c\u50cf\u5927\u5c0f \u3002<\/p>\n\n\n\n<p>\u5bf9\u4e8e\u9700\u8981\u7f16\u8bd1\u7684\u5e94\u7528(\u5982 <strong>C<\/strong>\u3001<strong>Go<\/strong>\u6216<strong>Java<\/strong>\u8bed\u8a00\u7b49)\u6765\u8bf4\uff0c\u901a\u5e38\u60c5\u51b5\u4e0b\u81f3\u5c11\u9700\u8981\u51c6\u5907\u4e24\u4e2a \u73af\u5883\u7684 Docker\u955c\u50cf:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u7f16\u8bd1\u73af\u5883\u955c\u50cf : \u5305\u62ec\u5b8c\u6574\u7684\u7f16\u8bd1\u5f15\u64ce\u3001\u4f9d\u8d56\u5e93\u7b49\uff0c\u5f80\u5f80\u6bd4\u8f83\u5e9e\u5927\u3002\u4f5c\u7528\u662f\u7f16\u8bd1\u5e94\u7528\u4e3a\u4e8c\u8fdb\u5236\u6587\u4ef6;<\/li><li> \u8fd0\u884c\u73af\u5883\u955c\u50cf: \u5229\u7528\u7f16\u8bd1\u597d\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6\uff0c\u8fd0\u884c\u5e94\u7528\uff0c\u7531\u4e8e\u4e0d\u9700\u8981\u7f16\u8bd1\u73af\u5883\uff0c\u4f53\u79ef\u6bd4\u8f83\u5c0f\u3002<\/li><\/ul>\n\n\n\n<p>\u4f7f\u7528\u591a\u6b65\u9aa4\u521b\u5efa\uff0c\u53ef\u4ee5\u5728\u4fdd\u8bc1\u6700\u7ec8\u751f\u6210\u7684\u8fd0\u884c\u73af\u5883\u955c\u50cf\u4fdd\u6301\u7cbe\u7b52\u7684\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528\u5355\u4e00\u7684Dockerfile\uff0c\u964d\u4f4e\u7ef4\u62a4\u590d\u6742\u5ea6 \u3002<\/p>\n\n\n\n<p>\u4ee5 <strong>Go<\/strong>\u8bed\u8a00\u5e94\u7528\u4e3a\u4f8b\u3002\u521b\u5efa\u5e72\u51c0\u76ee\u5f55\uff0c\u8fdb\u5165\u5230\u76ee\u5f55\u4e2d\uff0c\u521b\u5efa<strong>main.go<\/strong>\u6587\u4ef6\uff0c\u5185\u5bb9\u4e3a:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ main.go will output \"Hello, Docker\"\npackage main\n\nimport (\"fmt\")\n\nfunc main() {\n    fmt.println(\"Hello, Docker\")\n}<\/code><\/pre>\n\n\n\n<p>\u521b\u5efa Dockerfile\uff0c\u4f7f\u7528 golang:1.9\u955c\u50cf\u7f16\u8bd1\u5e94\u7528\u4e8c\u8fdb\u5236\u6587\u4ef6\u4e3a app\uff0c\u4f7f\u7528\u7cbe\u7b80\u7684\u955c\u50cf alpine:latest \u4f5c\u4e3a\u8fd0\u884c\u73af\u5883 \u3002Dockerfile \u5b8c\u6574\u5185\u5bb9\u4e3a:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>FROM golang:1.9 as builder # define stage name as builder\nRUN mkdir -p \/go\/src\/test\nWORKDIR \/go\/src\/test\nCOPY main.go\nRUN CGO_ENABLED=O GOOS=linux go build -o app\n\nFROM alpine:latest\nRUN apk \u2013no-cache add ca-certificates\nWORKDIR \/root\/\nCOPY \u2013from=builder \/go\/src\/test\/app . # copy file from the builder stage\nCMD [\".\/app\"]<\/code><\/pre>\n\n\n\n<p>\u6267\u884c\u5982\u4e0b\u547d\u4ee4\u521b\u5efa\u955c\u50cf\uff0c\u5e76\u8fd0\u884c\u5e94\u7528: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ docker build -t yeasy\/test multistage:latest .\nSending build context to Docker daemon 3.072kB\nStep 1\/10 : FROM golang:1.9\nSuccessfully built 5fd0cb93dda0\nSuccessfully tagged yeasy\/test-multistage:latest \n$ docker run --rm yeasy\/test-m ultistage:latest\nHello, Docker<\/code><\/pre>\n\n\n\n<p>\u67e5\u770b\u751f\u6210\u7684\u6700\u7ec8\u955c\u50cf\uff0c\u5927\u5c0f\u53ea\u6709 6.55 MB: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$docker images | grep test-multistage\nyeasy\/test-multistage latest 5fd0cb93dda0 1 minutes ago 6.55MB<\/code><\/pre>\n\n\n\n<p><strong>\u6587\u7ae0\u6765\u6e90\uff1a<\/strong><\/p>\n\n\n\n<p>\u300aDocker\u6280\u672f\u5165\u95e8\u4e0e\u5b9e\u8df5\u300b\u7b2c\u4e09\u7248  \u6768\u4fdd\u534e\u3001\u6234\u738b\u5251\u3001\u66f9\u4e9a\u4ed1 \u7f16\u8457<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u81ea 17.05 \u7248\u672c\u5f00\u59cb\uff0cDocker \u652f\u6301\u591a\u6b65\u9aa4\u955c\u50cf\u521b\u5efa( Multi-stage build)\u7279\u6027\uff0c\u53ef\u4ee5\u7cbe <span class=\"ellipsis\">&hellip;<\/span> <span class=\"more-link-wrap\"><a href=\"https:\/\/www.yanbin888.com\/?p=600\" class=\"more-link\"><span>Read More &rarr;<\/span><\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[53],"class_list":["post-600","post","type-post","status-publish","format-standard","hentry","category-linux","tag-docker"],"_links":{"self":[{"href":"https:\/\/www.yanbin888.com\/index.php?rest_route=\/wp\/v2\/posts\/600","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yanbin888.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yanbin888.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yanbin888.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yanbin888.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=600"}],"version-history":[{"count":19,"href":"https:\/\/www.yanbin888.com\/index.php?rest_route=\/wp\/v2\/posts\/600\/revisions"}],"predecessor-version":[{"id":624,"href":"https:\/\/www.yanbin888.com\/index.php?rest_route=\/wp\/v2\/posts\/600\/revisions\/624"}],"wp:attachment":[{"href":"https:\/\/www.yanbin888.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=600"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yanbin888.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=600"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yanbin888.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=600"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}