{"id":9072,"date":"2017-03-29T10:30:22","date_gmt":"2017-03-29T01:30:22","guid":{"rendered":"http:\/\/sitest.jp\/blog\/?p=9072"},"modified":"2017-03-29T10:56:08","modified_gmt":"2017-03-29T01:56:08","slug":"python%e7%92%b0%e5%a2%83%e3%82%92%e3%80%81docker%e3%81%ab%e7%a7%bb%e8%a1%8c%e3%81%97%e3%81%a6%e3%81%bf%e3%81%9fmac%e7%89%88","status":"publish","type":"post","link":"https:\/\/sitest.jp\/blog\/?p=9072","title":{"rendered":"python\u74b0\u5883\u3092\u3001docker\u306b\u79fb\u884c\u3057\u3066\u307f\u305f(Mac\u7248)"},"content":{"rendered":"<p>\u6700\u8fd1python\u3092\u4f7f\u3063\u3066web\u30a2\u30d7\u30ea\u306e\u958b\u767a\u3092\u884c\u3063\u3066\u3044\u307e\u3059\u3002Web\u30a2\u30d7\u30ea\u306e\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u306b\u306f\u3001\u8efd\u91cf\u3067\u305d\u3053\u305d\u3053\u9ad8\u6a5f\u80fd\u306eflask\u3092\u4f7f\u7528\u3057\u3066\u304a\u308a\u307e\u3059\u3002<\/p>\n<p>\u3053\u308c\u307e\u3067\u3001python\u3092\u4f7f\u3046\u4eba\u306b\u306f\u304a\u306a\u3058\u307f\u306e\u3001virtualenv\u3092\u4f7f\u3063\u305f\u4eee\u60f3\u74b0\u5883\u3067\u958b\u767a\u3092\u884c\u3063\u3066\u304d\u305f\u306e\u3067\u3059\u304c\u3001\u3053\u306e\u5ea6\u3001\u5b66\u7fd2\u3082\u304b\u306d\u3066\u958b\u767a\u74b0\u5883\u3092docker\u306b\u79fb\u3059\u3053\u3068\u306b\u3057\u307e\u3057\u305f\u3002<\/p>\n<p><!--more--><\/p>\n<div class=\"frame_headding\">\n<h3 style=\"text-align: left;\">\n\u79fb\u884c\u306e\u6e96\u5099<br \/>\n<\/h3>\n<\/div>\n<p>\u307e\u305a\u306f\u4ecavirtualenv\u74b0\u5883\u306b\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3055\u308c\u3066\u3044\u308b\u30e2\u30b8\u30e5\u30fc\u30eb\u4e00\u89a7\u304c\u8a18\u8ff0\u3055\u308c\u305f\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n<pre><code>$ pip freeze &gt; requirements.txt\n<\/code><\/pre>\n<p>\u4e0a\u8a18\u30b3\u30de\u30f3\u30c9\u4e00\u767a\u3067\u4f5c\u6210\u3067\u304d\u307e\u3059\u3002\u3053\u308c\u304c\u3042\u308b\u72b6\u614b\u3067<\/p>\n<pre><code>$ pip install -r requirements.txt\n<\/code><\/pre>\n<p>\u3068\u3044\u3046\u30b3\u30de\u30f3\u30c9\u3092virtualenv\u5185\u3067\u6253\u3066\u3070\u3001\u65b0\u3057\u3044\u74b0\u5883\u3067\u3082\u3001\u3053\u308c\u307e\u3067\u4f7f\u3063\u3066\u3044\u305f\u30e9\u30a4\u30d6\u30e9\u30ea\u304c\u307e\u3068\u3081\u3066\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3055\u308c\u307e\u3059\u3002\u305f\u3060\u3001\u4eca\u56de\u306fdocker\u3067\u4f5c\u3063\u305f\u30de\u30b7\u30f3\u306e\u4e2d\u304b\u3089\u3053\u306e\u30b3\u30de\u30f3\u30c9\u3092 \u81ea\u52d5\u5b9f\u884c\u3057\u307e\u3059\u3002<\/p>\n<p>\u3053\u308c\u304b\u3089docker\u306e\u4e2d\u306bpython\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u306a\u3044\u3068\u3044\u3051\u306a\u3044\u304b\u3068\u3044\u3046\u3068\u3001\u305d\u3093\u306a\u3053\u3068\u306f\u3042\u308a\u307e\u305b\u3093\u3002<\/p>\n<p>docker\u306e\u5229\u70b9\u306f\u3001\u3059\u3067\u306bdockerhub\u306b\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\u3055\u308c\u3066\u3044\u308b\u30a4\u30e1\u30fc\u30b8\u3092\u5143\u306b\u3001\u305d\u308c\u306b\u5bfe\u3057\u3066\u30a2\u30ec\u30f3\u30b8\u3092\u52a0\u3048\u3001\u81ea\u5206\u306e\u30de\u30b7\u30f3\u3068\u3057\u3066\u4f7f\u3048\u308b\u3068\u3044\u3046\u70b9\u306b\u3042\u308a\u307e\u3059\u3002<br \/>\n\u3053\u3053\u3067\u306f\u4f55\u3082\u8003\u3048\u305a\u3001\u516c\u5f0f\u306epython\u30a4\u30e1\u30fc\u30b8\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<p>\u4eca\u56de\u306f<a href=\"https:\/\/hub.docker.com\/_\/python\/\"\u3000rel=\"nofollow\" target=\"_blank\">https:\/\/hub.docker.com\/_\/python\/<\/a><br \/>\n\u306b\u7d39\u4ecb\u3055\u308c\u3066\u3044\u308b\u3082\u306e\u306e\u4e2d\u304b\u3089\u3001\u73fe\u5728vierutalenv\u3067\u4f7f\u3063\u3066\u3044\u308b\u306e\u3068\u30d0\u30fc\u30b8\u30e7\u30f3\u304c\u540c\u30583.5\u3001\u304b\u3064\u3001\u540c\u3058\u30d5\u30a9\u30eb\u30c0\u306brequirements.txt\u304c\u3042\u308c\u3070\u3001\u305d\u306e\u5185\u5bb9\u3092\u81ea\u52d5\u7684\u306b\u30d3\u30eb\u30c9\u3057\u3066\u304f\u308c\u308b\u300c<a href=\"https:\/\/github.com\/docker-library\/python\/blob\/9a9021f2134d953165b31d98cacb95aa34076f90\/3.5\/onbuild\/Dockerfile\"\u3000rel=\"nofollow\" target=\"_blank\">on-build<\/a>\u300d\u306e\u3082\u306e\u3092\u9078\u3073\u307e\u3057\u305f\u3002<\/p>\n<div class=\"frame_headding\">\n<h3 style=\"text-align: left;\">\nDockerfile\u3092\u4f5c\u6210\u3059\u308b<br \/>\n<\/h3>\n<\/div>\n<p>\u3055\u3066\u3001\u30d5\u30a1\u30a4\u30eb\u306e\u6e96\u5099\u3067\u3059\u3002<br \/>\nWeb\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u306eflask\u3092\u4f7f\u3063\u3066\u3044\u307e\u3059\u306e\u3067\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u305f\u3061\u3042\u3052\u308b\u305f\u3081\u306einit.py\u30d5\u30a1\u30a4\u30eb\u304c\u3042\u308a\u307e\u3059\u3002\u52d5\u3044\u3066\u3044\u308b\u3082\u306e\u306b\u30c6\u30b9\u30c8\u7528\u306e\u30a2\u30af\u30bb\u30b9\u30dd\u30a4\u30f3\u30c8\u3092\u52a0\u3048\u3066\u3053\u3093\u306a\u611f\u3058\u3067\u7528\u610f\u3057\u307e\u3057\u305f<\/p>\n<pre><code>from flask import Flask, jsonify, abort, make_response, request, url_for, abort, Response\n# (\u4e2d\u7565)\napp = Flask(__name__)\n# (\u4e2d\u7565)\n@app.route('\/test\/')\ndef test():\n    title = \"test\"\n    return 'docker\u3067python\u306e\u30c6\u30b9\u30c8\u3067\u3059!'\n# (\u4e2d\u7565)\nif __name__ == '__main__':\n    app.run(host='0.0.0.0', port=3000)\n<\/code><\/pre>\n<p>\u66f4\u306b\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u304c\u5165\u3063\u3066\u3044\u308b\u30d5\u30a9\u30eb\u30c0(myapp)\u306e\u4e26\u3073\u306b<br \/>\ninit.py\u3092\u8d77\u52d5\u3059\u308b\u305f\u3081\u306e<br \/>\nstart.sh\u3092\u4f5c\u308a\u307e\u3059<\/p>\n<pre><code>python3 myapp\/init.py\n<\/code><\/pre>\n<p>Dockerfile\u3092\u4f5c\u308a\u307e\u3059<\/p>\n<pre><code>from python:3.5.3-onbuild\ncmd [\"sh\",\"start.sh\"]\n<\/code><\/pre>\n<p>\u5168\u4f53\u3068\u3057\u3066\u306f\u4e0b\u8a18\u306e\u3088\u3046\u306a\u30c7\u30a3\u30c6\u30af\u30c8\u30ea\u69cb\u6210\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre><code>\u3000\u250c myapp\/\n\u3000\u2502\u3000\u251c(\u30a2\u30d7\u30ea\u52d5\u4f5c\u306b\u5fc5\u8981\u306a\u30d5\u30a1\u30a4\u30eb)\n\u3000\u2502\u3000\u2514 init.py\n\u3000\u2502\n\u3000\u251c Dockerfile\n\u3000\u251c start.sh\n\u3000\u2514 requirements.txt\n<\/code><\/pre>\n<div class=\"frame_headding\">\n<h3 style=\"text-align: left;\">\ndocker\u30a4\u30e1\u30fc\u30b8\u306e\u4f5c\u6210<br \/>\n<\/h3>\n<\/div>\n<p>\u3055\u3066\u3001\u79c1\u306e\u74b0\u5883\u3067\u306f\u3059\u3067\u306bdocker-machine\u304c\u5165\u3063\u3066\u3044\u308b\u306e\u3067\u4ee5\u524d\u4f5c\u3063\u305f\u300cdefault\u300d\u3068\u3044\u3046\u540d\u524d\u306e\u4eee\u60f3\u30de\u30b7\u30f3\u3092\u7acb\u3061\u4e0a\u3052\u307e\u3059\u3002<\/p>\n<p>&#8220;`$ docker-machine start default<br \/>\nStarting &quot;default&quot;&#8230;<br \/>\n(default) Check network to re-create if needed&#8230;<br \/>\n(default) Waiting for an IP&#8230;<br \/>\nMachine &quot;default&quot; was started.<br \/>\nWaiting for SSH to be available&#8230;<br \/>\nDetecting the provisioner&#8230;<br \/>\nStarted machines may have new IP addresses. You may need to re-run the &lt;code&gt;docker-machine env&lt;\/code&gt; command.<\/p>\n<p>&lt;pre&gt;&lt;code&gt;docker-machine\u306e\u8a2d\u5b9a\u3092\u78ba\u8a8d\u3057\u307e\u3059\u3002<\/p>\n<p>&#8220;`$ docker-machine env default<br \/>\nexport DOCKER_TLS_VERIFY=&#8221;1&#8243;<br \/>\nexport DOCKER_HOST=&#8221;tcp:\/\/192.168.99.100:2376&#8243;<br \/>\nexport DOCKER_CERT_PATH=&#8221;\/Users\/xxxxxxxx\/.docker\/machine\/machines\/default&#8221;<br \/>\nexport DOCKER_MACHINE_NAME=&#8221;default&#8221;<br \/>\n# Run this command to configure your shell:<br \/>\n# eval $(docker-machine env default)<br \/>\n<\/code><\/p>\n<p>\u6307\u793a\u901a\u308a\u306b<\/p>\n<pre><code>$ eval $(docker-machine env default)\u3000\n<\/code><\/pre>\n<p>\u3092\u884c\u3063\u3066docker\u30b3\u30de\u30f3\u30c9\u304c\u4f7f\u3048\u308b\u3088\u3046\u306b\u3057\u307e\u3059\u3002<\/p>\n<p>\u30b3\u30f3\u30c6\u30ca\u3092\u4f5c\u308b\u3082\u3068\u306b\u306a\u308bdocker\u30a4\u30e1\u30fc\u30b8\u306e\u4f5c\u6210\u3092\u884c\u3044\u307e\u3059\u3002<br \/>\n\u5148\u7a0b\u66f8\u3044\u305fDockerfile\u306e\u3042\u308b\u30c7\u30a3\u30c6\u30af\u30c8\u30ea\u306b\u79fb\u52d5\u3057\u3066\u4e0b\u8a18\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002<br \/>\n-t\u30aa\u30d7\u30b7\u30e7\u30f3\u3067docker\u30a4\u30e1\u30fc\u30b8\u306b\u540d\u524d\u3092\u3064\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre><code>$ docker build .\/ -t pythonflask\n(\u4e2d\u7565)\nRemoving intermediate container 851396f76f39\nSuccessfully built 9bcebb91c9b7\n<\/code><\/pre>\n<p>\u7121\u4e8b\u3067\u304d\u305f\u306e\u3067\u3001\u7d50\u679c\u3092\u78ba\u8a8d\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre><code>$ docker images\nREPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE\npythonflask                    latest              9bcebb91c9b7        18 minutes ago      714 MB\npython                         3.5.3-onbuild       38202ab7f963        9 days ago          687 MB\n<\/code><\/pre>\n<div class=\"frame_headding\">\n<h3 style=\"text-align: left;\">\ndocker\u30a4\u30e1\u30fc\u30b8\u3092\u8d77\u52d5\u3057\u3066\u307f\u307e\u3059<br \/>\n<\/h3>\n<\/div>\n<p>\u3055\u3066\u3001\u30a4\u30e1\u30fc\u30b8\u304c\u5b8c\u6210\u3057\u305f\u306e\u3067\u3001\u5b9f\u969b\u306bdocker\u30b3\u30f3\u30c6\u30ca\u3092\u8d77\u52d5\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<br \/>\n\u7acb\u3061\u4e0a\u3052\u308b\u306e\u306f\u300cpythonflask\u300d\u30a4\u30e1\u30fc\u30b8\u3067\u3059\u3002-p\u30aa\u30d7\u30b7\u30e7\u30f3\u3067docker-machine\u306e3000\u30dd\u30fc\u30c8\u3068\u3001\u30b3\u30f3\u30c6\u30ca\u306e3000\u30dd\u30fc\u30c8\u3092\u5bfe\u5fdc\u4ed8\u3051\u3066\u3001\u30a2\u30af\u30bb\u30b9\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u307e\u3059\u3002<\/p>\n<pre><code>$ docker run  -p 3000:3000 pythonflask\n * Running on http:\/\/0.0.0.0:3000\/ (Press CTRL+C to quit)\n<\/code><\/pre>\n<p>\u7acb\u3061\u4e0a\u304c\u308a\u307e\u3057\u305f\u306e\u3067\u30a2\u30af\u30bb\u30b9\u3057\u307e\u3059\u3002\u30b3\u30f3\u30c6\u30ca\u306f0.0.0.0\u3067\u7acb\u3061\u4e0a\u304c\u308a\u307e\u3057\u305f\u304cdocker-machine\u306eIP\u304c\u5148\u7a0b\u898b\u305f\u3088\u3046\u306b192.168.99.100\u306a\u306e\u3067\u3001192.168.99.100:3000\/test\/ \u306b\u30a2\u30af\u30bb\u30b9\u3057\u307e\u3059\u3002<\/p>\n<p><img src=\"https:\/\/storage.googleapis.com\/sitest-wp\/blog\/wp-content\/uploads\/2017\/03\/1857b6f606cc4ddc6caf9ed57eaa9f68.png\" alt=\"\u30a2\u30af\u30bb\u30b9\" \/><\/p>\n<p>\u7121\u4e8bdocker\u74b0\u5883\u3067\u306e\u69cb\u7bc9\u304c\u5b8c\u4e86\u3057\u307e\u3057\u305f\u3002\u3042\u3068\u306fdocker run\u306e\u30aa\u30d7\u30b7\u30e7\u30f3\u3067\u3001\u958b\u767a\u3057\u3084\u3059\u3044\u74b0\u5883\u3092\u6574\u3048\u3066\u3044\u304d\u307e\u3059\u3002\u305d\u308c\u3067\u306f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1python\u3092\u4f7f\u3063\u3066web\u30a2\u30d7\u30ea\u306e\u958b\u767a\u3092\u884c\u3063\u3066\u3044\u307e\u3059\u3002Web\u30a2\u30d7\u30ea\u306e\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u306b\u306f\u3001\u8efd\u91cf\u3067\u305d\u3053\u305d\u3053\u9ad8\u6a5f\u80fd\u306eflask\u3092\u4f7f\u7528\u3057\u3066\u304a\u308a\u307e\u3059\u3002 \u3053\u308c\u307e\u3067\u3001python\u3092\u4f7f\u3046\u4eba\u306b\u306f\u304a\u306a\u3058\u307f\u306e\u3001virtualenv\u3092\u4f7f\u3063\u305f\u4eee\u60f3\u74b0\u5883\u3067\u958b\u767a\u3092\u884c\u3063\u3066\u304d\u305f\u306e\u3067\u3059\u304c\u3001\u3053\u306e\u5ea6\u3001\u5b66\u7fd2\u3082\u304b\u306d\u3066\u958b\u767a\u74b0\u5883\u3092docker\u306b\u79fb\u3059\u3053\u3068\u306b\u3057\u307e\u3057\u305f\u3002<\/p>\n","protected":false},"author":1,"featured_media":9367,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[27],"tags":[],"acf":false,"_links":{"self":[{"href":"https:\/\/sitest.jp\/blog\/wp-json\/wp\/v2\/posts\/9072"}],"collection":[{"href":"https:\/\/sitest.jp\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sitest.jp\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sitest.jp\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sitest.jp\/blog\/wp-json\/wp\/v2\/comments?post=9072"}],"version-history":[{"count":16,"href":"https:\/\/sitest.jp\/blog\/wp-json\/wp\/v2\/posts\/9072\/revisions"}],"predecessor-version":[{"id":9368,"href":"https:\/\/sitest.jp\/blog\/wp-json\/wp\/v2\/posts\/9072\/revisions\/9368"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sitest.jp\/blog\/wp-json\/wp\/v2\/media\/9367"}],"wp:attachment":[{"href":"https:\/\/sitest.jp\/blog\/wp-json\/wp\/v2\/media?parent=9072"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sitest.jp\/blog\/wp-json\/wp\/v2\/categories?post=9072"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sitest.jp\/blog\/wp-json\/wp\/v2\/tags?post=9072"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}