{"id":6194,"date":"2017-03-24T16:19:33","date_gmt":"2017-03-24T08:19:33","guid":{"rendered":"https:\/\/kyle.ai\/blog\/?p=6194"},"modified":"2017-03-24T16:19:33","modified_gmt":"2017-03-24T08:19:33","slug":"%e6%90%ad%e5%bb%ba%e8%87%aa%e5%b7%b1%e7%9a%84docker-registry%e4%bb%93%e5%ba%93","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/6194.html","title":{"rendered":"\u642d\u5efa\u81ea\u5df1\u7684Docker Registry\u4ed3\u5e93"},"content":{"rendered":"<h1>\u672c\u6587\u5b9e\u9a8c\u7684\u73af\u5883<\/h1>\n<ul>\n<li>\u672c\u5730\u7aef\u4e3a\u81ea\u5df1\u7684Mac\u7b14\u8bb0\u672c\u7535\u8111<\/li>\n<li>\u4e91\u7aef\u4e3aUbuntu Server 14.04<\/li>\n<li>\u672c\u5730\u4e0e\u4e91\u7aef\u90fd\u9700\u8981\u5b89\u88c5Docker\uff0c\u6309\u7167\u5b98\u7f51\u6587\u6863\u5b89\u88c5\uff1ahttps:\/\/docs.docker.com\/engine\/installation\/<\/li>\n<\/ul>\n<h1>\u5728\u672c\u5730\u5236\u4f5cDocker\u955c\u50cf<\/h1>\n<p>\u672c\u6b65\u9aa4\u73af\u5883\u4e3a\u672c\u5730Mac\u3002<\/p>\n<p>\u7f16\u5199Dockefile\uff0c\u5c06\u6211\u4eec\u7684python api server\u6253\u5305\u6210\u4e00\u4e2a\u955c\u50cf\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\"> \r\n$ cd ~\/etc\/docker\/\r\n$ cat Dockerfile\r\n<\/pre>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\"> \r\nFROM alpine:3.5 \r\n\r\nRUN apk add --no-cache python &amp;&amp; \\\r\n    python -m ensurepip &amp;&amp; \\\r\n    rm -r \/usr\/lib\/python*\/ensurepip &amp;&amp; \\\r\n    pip install --upgrade pip setuptools &amp;&amp; \\\r\n    rm -r \/root\/.cache &amp;&amp; \\\r\n    apk add --no-cache vim py-mysqldb musl-dev linux-headers g++ python-dev libxml2-dev \\\r\n    libxml2 libxslt libxslt-dev &amp;&amp; \\\r\n    ln -s \/usr\/include\/locale.h \/usr\/include\/xlocale.h\r\n\r\n##############\r\n\r\nCOPY VikiQA\/requirements.txt \/src\/\r\nRUN pip install -r \/src\/requirements.txt\r\n\r\nCOPY VikiQA \/src\/VikiQA\r\nCOPY VikiAIML \/src\/VikiAIML\r\nRUN cd \/usr\/lib\/python2.7\/site-packages &amp;&amp; ln -s \/src\/VikiQA\/viki_qa . &amp;&amp; \\\r\n    cd \/src\/VikiQA\/viki_qa &amp;&amp; cp config_sample.py config.py &amp;&amp; \\\r\n    mkdir \/var\/log\/viki_qa\r\n\r\nWORKDIR \/src\/VikiQA\/viki_qa\/\r\n\r\nENTRYPOINT &#x5B;&quot;\/usr\/bin\/gunicorn&quot;, &quot;-k&quot;, &quot;tornado&quot;, &quot;-b&quot;, &quot;0.0.0.0:80&quot;, &quot;--workers=4&quot;, &quot;viki_qa.wsgi:app&quot;]\r\n<\/pre>\n<p>\u7136\u540e\u5236\u4f5c\u597d\u955c\u50cf\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\"> \r\n$ cd ~\/etc\/docker\/\r\n$ docker build -t kyle\/viki_qa .\r\n<\/pre>\n<p>\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u67e5\u770b\u5236\u4f5c\u597d\u7684\u955c\u50cf\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\"> \r\n$ docker images\r\nkyle@kyledeMacBook-Pro ~\/p\/a\/VikiDocker&gt; docker images\r\nREPOSITORY                      TAG                 IMAGE ID            CREATED             SIZE\r\nkyle\/viki_qa                     latest              5cd56c45db67        27 hours ago        347 MB\r\n<\/pre>\n<h1>\u5728\u4e91\u7aef\u8fd0\u884cDocker registry<\/h1>\n<p>\u672c\u6b65\u9aa4\u73af\u5883\u4e3a\u4e91\u7aefUbuntu Server\u3002<\/p>\n<p>\u5047\u8bbe\u4e91\u7aef\u670d\u52a1\u5668\u5730\u5740\u4e3a docker.kyle.ai\uff0c\u5728\u4e0a\u9762\u5b89\u88c5\u597ddocker\u540e\uff0c\u8fd0\u884cregistry\u5bb9\u5668\uff0c\u8fd9\u91cc\u6211\u4eec\u7528docker-compose\u6765\u8fd0\u884c\u3002<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\"> \r\n$ cd ~\/etc\/docker\/\r\n$ cat docker-compose.yml \r\n<\/pre>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\"> \r\nversion: '2'\r\n\r\nservices:\r\n\r\n    registry:\r\n        image: registry:2\r\n        restart: always\r\n        ports:\r\n          - 5000:5000\r\n        volumes:\r\n          - \/home\/deploy\/data\/docker\/registry:\/var\/lib\/registry\r\n#          - \/home\/deploy\/etc\/docker\/cert\/:\/certs\r\n#          - \/home\/deploy\/etc\/docker\/auth\/:\/auth\r\n#        environment:\r\n#          REGISTRY_HTTP_TLS_CERTIFICATE: \/certs\/214053375670003.pem\r\n#          REGISTRY_HTTP_TLS_KEY: \/certs\/214053375670003.key\r\n#          REGISTRY_AUTH: htpasswd\r\n#          REGISTRY_AUTH_HTPASSWD_PATH: \/auth\/htpasswd\r\n#          REGISTRY_AUTH_HTPASSWD_REALM: Registry Realm\r\n        container_name: registry\r\n<\/pre>\n<p>\u89e3\u91ca\u4e00\u4e0b\uff0c\u5c31\u662f\u5728\u672c\u5730\u8fd0\u884cregistry\u7a97\u53e3\u670d\u52a1\uff0c\u7aef\u53e3\u53f7\u4e3a5000\uff0c\u5e76\u628a\u4ed3\u5e93\u7684\u6587\u4ef6\u6570\u636e\u6301\u4e45\u5316\u5230\u672c\u5730\u78c1\u76d8 \/home\/deploy\/data\/docker\/registry \u3002<\/p>\n<p>\u6ce8\u91ca\u7684\u90a3\u4e9b\u884c\uff0c\u662f\u7528\u6765\u914d\u7f6e\u4f7f\u7528auth\u8ba4\u8bc1\u4e0essl\u8bc1\u4e66\u7684\uff0c\u4f46\u7531\u4e8e\u6211\u4eec\u4f7f\u7528nginx\u6765\u505a\u8fd9\u4e2a\u4e8b\u60c5\uff0c\u6240\u4ee5docker registry\u670d\u52a1\u5c31\u4e0d\u7528\u4e86\u3002\u4e0d\u80fdnginx\u4e0eregistry\u540c\u65f6\u914d\u7f6e\u8fd9\u4e24\u4e2a\uff0c\u4e0d\u7136\u4f1a\u8bbf\u95ee\u4e0d\u6210\u529f\u3002<\/p>\n<p>\u7136\u540e\u628a\u5bb9\u5668\u8fd0\u884c\u8d77\u6765<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\"> \r\n$ cd ~\/etc\/docker\/\r\n$ sudo docker-compose up -d\r\n$ sudo docker-compose ps\r\n  Name                Command               State           Ports          \r\n--------------------------------------------------------------------------\r\nregistry   \/entrypoint.sh \/etc\/docker ...   Up      0.0.0.0:5000-&gt;5000\/tcp \r\n<\/pre>\n<p>\u4e0b\u4e00\u6b65\u5c31\u9700\u8981\u914d\u7f6enginx\uff0c\u5c06\u670d\u52a1\u5668\u768480\u7aef\u53e3\u8f6c\u53d1\u5230registry\u670d\u52a1\u76845000\u7aef\u53e3\uff0c\u5e76\u8bbe\u7f6e\u597dssl\u4e0eauth\u8ba4\u8bc1\u3002<\/p>\n<p>nginx\u914d\u7f6e\u6587\u4ef6\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\"> \r\n$ cd ~\/etc\/nginx\/sites-enabled\/\r\n$ cat docker.kyle.ai\r\n<\/pre>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\"> \r\nserver {\r\n    listen 80;\r\n\tlisten 443 ssl;\r\n    ssl_certificate   \/etc\/nginx\/cert\/docker.kyle.ai\/ssl.pem;\r\n    ssl_certificate_key  \/etc\/nginx\/cert\/docker.kyle.ai\/ssl.key;\r\n    ssl_session_timeout 5m;\r\n    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;\r\n    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;\r\n    ssl_prefer_server_ciphers on;\r\n    add_header X-Frame-Options SAMEORIGIN;\r\n    add_header Strict-Transport-Security &quot;max-age=8640000;&quot;;\r\n    add_header X-Content-Type-Options: nosniff;\r\n\r\n\tserver_name docker.kyle.ai;\r\n    access_log  \/var\/log\/nginx\/docker-access.log;\r\n    error_log  \/var\/log\/nginx\/docker-error.log;\r\n\r\n    root \/home\/deploy\/www\/;\r\n    client_max_body_size 0;      # \u4e0d\u9650\u5236\u4e0a\u4f20\u6587\u4ef6\u7684\u5927\u5c0f\uff0c\u4e00\u4e2a\u955c\u50cf\u53ef\u80fd\u6709\u51e0\u767eM\r\n    chunked_transfer_encoding on;\r\n\r\n    if ($ssl_protocol = &quot;&quot;) {\r\n        return 302 https:\/\/$server_name$request_uri;\r\n    }\r\n\r\n\tlocation ^~ \/ {\r\n        proxy_set_header Host $http_host;\r\n        proxy_set_header X-Real-IP $remote_addr;\r\n        proxy_set_header X-Scheme $scheme;\r\n\t}\r\n\r\n\tupstream docker_registry{\r\n\t    server 127.0.0.1:5000;\r\n\t}\r\n\r\n    location \/v2\/ {\r\n        # Do not allow connections from docker 1.5 and earlier\r\n        # docker pre-1.6.0 did not properly set the user agent on ping, catch &quot;Go *&quot; user agents\r\n        if ($http_user_agent ~ &quot;^(docker\\\/1\\.(3|4|5(?!\\.&#x5B;0-9]-dev))|Go ).*$&quot; ) {\r\n            return 404;\r\n        }\r\n        auth_basic &quot;Registry realm&quot;;\r\n        auth_basic_user_file \/home\/deploy\/etc\/docker\/auth\/htpasswd;\r\n        add_header 'Docker-Distribution-Api-Version' 'registry\/2.0';\r\n\r\n        proxy_pass                          http:\/\/docker_registry;\r\n        proxy_set_header  Host              $http_host;   # required for docker client's sake\r\n        proxy_set_header  X-Real-IP         $remote_addr; # pass on real client's IP\r\n        proxy_set_header  X-Forwarded-For   $proxy_add_x_forwarded_for;\r\n        proxy_set_header  X-Forwarded-Proto $scheme;\r\n        proxy_read_timeout                  900;\r\n    }\r\n\r\n}\r\n<\/pre>\n<p>ssl\u8bc1\u4e66\uff0c\u6211\u662f\u76f4\u63a5\u5728\u963f\u91cc\u4e91\u7533\u8bf7\u7684\u514d\u8d39dv\u8bc1\u4e66\uff0c\u7533\u8bf7\u5730\u5740\uff1ahttps:\/\/common-buy.aliyun.com\/?commodityCode=cas#\/buy<\/p>\n<p>\u7533\u8bf7\u6210\u529f\u540e\uff0c\u76f8\u5e94\u7684\u914d\u7f6e ssl_certificate \u4e0e ssl_certificate_key \u6587\u4ef6\u8def\u5f84\u3002<\/p>\n<p>\u7136\u540e\u5c31\u662fbasic auth\u7684\u7528\u6237\u6587\u4ef6\uff1a\/home\/deploy\/etc\/docker\/auth\/htpasswd\uff0c\u53ef\u4ee5\u901a\u8fc7\u5982\u4e0b\u547d\u4ee4\u751f\u6210\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\"> \r\nsudo docker run --entrypoint htpasswd registry:2 -bn username password &gt; htpasswd\r\n<\/pre>\n<p>\u6ce8\u610f\u4e0d\u8981\u6dfb\u52a0-B\u53c2\u6570\uff0c\u4e4b\u524d\u6211\u7528\u7684 -Bbn \u53c2\u6570\uff0c\u7ed3\u679c\u540e\u9762nginx\u4f1a\u51fa\u73b0\u5982\u4e0b\u9519\u8bef\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\"> \r\n&#x5B;crit] 12472#0: *29935 crypt_r() failed (22: Invalid argument)\r\n<\/pre>\n<p>nginx\u7684\u8fd9\u4e2a\u914d\u7f6e\uff0c\u53ef\u80fd\u8ddfnginx\u7248\u672c\u6709\u5173\u7cfb<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\"> \r\nadd_header 'Docker-Distribution-Api-Version' 'registry\/2.0'; \r\n<\/pre>\n<p>\u9ad8\u7248\u672c\u7684nginx\u53ef\u80fd\u9700\u8981\u5728\u540e\u9762\u6dfb\u52a0\u4e2a always<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\"> \r\nadd_header 'Docker-Distribution-Api-Version' 'registry\/2.0' always;\r\n<\/pre>\n<p>\u914d\u7f6e\u597dnginx\u540e\uff0c\u5c31\u53ef\u4ee5\u901a\u8fc7\u8bbf\u95eeapi\u6765\u6d4b\u8bd5\uff1a curl https:\/\/docker.kyle.ai\/v2 \u8fd9\u6837\uff0c\u8fd9\u91cc\u6211\u53ea\u662f\u7528\u4e86\u793a\u4f8b\u57df\u540d\uff0c\u4f60\u9700\u8981\u6362\u6210\u4f60\u81ea\u5df1\u7684\u3002<\/p>\n<h1>\u5c06\u672c\u5730\u7684image push\u5230\u4e91\u7aef\u7684registry<\/h1>\n<p>\u672c\u6b65\u9aa4\u662f\u5728\u672c\u5730Mac\u7cfb\u7edf\u73af\u5883\u3002<\/p>\n<p>\u4e0a\u9762\u7684\u6b65\u9aa4\u4e2d\uff0c\u6211\u4eec\u5df2\u7ecf\u6210\u529f\u5730\u5728\u672c\u5730build\u4e86\u4e00\u4e2aImage\uff0c\u53eb kyle\/viki_qa\uff0c\u73b0\u5728\u628a\u8fd9\u4e2aimage\u6620\u5c04\u5230\u4e91\u7aef\u7684registry\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\"> \r\n$ sudo docker tag kyle\/viki_qa docker.kyle.ai\/kyle\/viki_qa\r\n<\/pre>\n<p>\u7136\u540e push \u5230\u4e91\u7aef\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\"> \r\n$ sudo docker push docker.kyle.ai\/kyle\/viki_qa\r\nError: Status 405 trying to push repository kyle\/viki_qa: &quot;&lt;html&gt;\\r\\n&lt;head&gt;&lt;title&gt;405 Not Allowed&lt;\/title&gt;\r\n<\/pre>\n<p>\u62a5405 Not Allowed\u7684\u9519\u8bef\uff0c \u662f\u7531\u4e8e\u9700\u8981\u5148\u767b\u9646\uff0c\u8fd8\u8bb0\u5f97\u6211\u4eec\u5728\u4e0a\u9762\u914d\u7f6enginx\u7684\u65f6\u5019\uff0c\u8bbe\u7f6e\u7684basic auth\u7684\u7528\u6237\u4e0e\u5bc6\u7801\uff0c\u7528\u8fd9\u4e2a\u6765\u767b\u9646\u4e00\u4e0b<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\"> \r\n$ sudo docker login docker.kyle.ai\r\n\u8f93\u5165\u5bc6\u7801\u4e0e\u5bc6\u7801\r\n<\/pre>\n<p>\u4e4b\u540e\u518dpush\u5c31\u884c\u4e86\u3002<\/p>\n<h1>\u5728\u5176\u5b83\u673a\u5668\u62c9\u53d6\u6211\u4eec\u7684\u955c\u50cf<\/h1>\n<p>\u76f4\u63a5<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\"> \r\n$ sudo docker pull docker.kyle.ai\/kyle\/viki_qa\r\n$ sudo docker run -it --rm -d docker.kyle.ai\/kyle\/viki_qa\r\n<\/pre>\n<h1>\u5176\u5b83<\/h1>\n<p>\u5220\u9664\u8fdc\u7a0b\u7684image\u6bd4\u8f83\u9ebb\u70e6\uff0c\u5b98\u65b9\u6ca1\u6709\u63d0\u4f9b\u5f88\u597d\u7684\u529e\u6cd5\u3002<\/p>\n<p>\u76ee\u524ddocker\u5b98\u65b9\u63d0\u4f9b\u4e86\u5982\u4e0b3\u4e2a\u8f6f\u5220\u9664\u7684\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li>DELETE:\/v2\/\/manifests\/\uff1a\u8fd9\u4e2aAPI\u662f\u8f6f\u5220\u9664\u4e00\u4e2a\u6e05\u5355\uff08manifest\uff09\uff0c\u4f46\u662f\u771f\u6b63\u5360\u7528\u5b58\u50a8\u7a7a\u95f4\u7684\u5c42\u8fd8\u5728\u3002<\/li>\n<li>DELETE:\/v2\/\/blobs\/\uff1a\u8fd9\u4e2aAPI\u7c7b\u4f3c\u4e0a\u9762\u90a3\u4e2a\uff0c\u53ea\u4e0d\u8fc7\u5b83\u8981\u8f6f\u5220\u9664\u7684\u5bf9\u8c61\u662f\u5c42\uff08layer\uff09\u7f62\u4e86\u3002<\/li>\n<li>DELETE:\/v2\/\/blobs\/uploads\/\uff1a\u8fd9\u4e2a\u53ea\u662f\u53d6\u6d88\u6389\u53e6\u4e00\u4e2a\u4e0a\u4f20\u7684\u8fdb\u7a0b\u7f62\u4e86\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u5b9e\u9a8c\u7684\u73af\u5883 \u672c\u5730\u7aef\u4e3a\u81ea\u5df1\u7684Mac\u7b14\u8bb0\u672c\u7535\u8111 \u4e91\u7aef\u4e3aUbuntu Server 14.04 \u672c\u5730\u4e0e\u4e91\u7aef\u90fd\u9700\u8981 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-6194","post","type-post","status-publish","format-standard","hentry","category-skill"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/6194","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=6194"}],"version-history":[{"count":1,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/6194\/revisions"}],"predecessor-version":[{"id":6195,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/6194\/revisions\/6195"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=6194"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=6194"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=6194"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}