{"id":1161,"date":"2016-02-21T21:14:30","date_gmt":"2016-02-21T21:14:30","guid":{"rendered":"http:\/\/www.tekhead.org\/blog\/?p=1161"},"modified":"2016-02-21T21:15:05","modified_gmt":"2016-02-21T21:15:05","slug":"docker-part-3-howto-create-a-simple-python-web-app-in-docker","status":"publish","type":"post","link":"https:\/\/tekhead.it\/blog\/2016\/02\/docker-part-3-howto-create-a-simple-python-web-app-in-docker\/","title":{"rendered":"Docker Part 3 &#8211; HOWTO Create a Simple Python Web App in Docker"},"content":{"rendered":"<p>If you\u2019ve been following this series (last part <a href=\"http:\/\/www.tekhead.org\/blog\/2014\/09\/docker-part-2-howto-remove-delete-docker-containers\/\" target=\"_blank\">here<\/a>), we now have docker installed, but what do we do next? Create our first containers of course!<\/p>\n<p>I think we need to make it a bit more interesting though as just creating containers is a bit meaningless, in real life we\u2019re actually going to do something with them. The scenario is that we want a few copies of our simple python web application. To achieve this we need to use a few simple docker commands:<\/p>\n<ul>\n<li>Create a new container<\/li>\n<li>Install an application inside of it<\/li>\n<li>Store it as an image<\/li>\n<li>Duplicate it more than once and make these available to other clients<\/li>\n<li>Test each instance to ensure they are unique and accessible<\/li>\n<\/ul>\n<p>The good thing here is that all of the above steps are repeatable with whatever application you wish to install inside your containers. This is just a simple way to help get your head around the concepts and commands.<\/p>\n<p>We start by creating our first empty Ubuntu container. The \u2013i connects us to the shell of the container (interactive).<\/p>\n<pre>$ sudo docker run -i -t --name=\"firstcontainer\" ubuntu:14.04 \/bin\/bash<\/pre>\n<p><br style=\"height: 1em;\" \/>Then in this case we need to install the python and web.py dependencies INSIDE of the container. This could be modified for any required dependencies or apps.<\/p>\n<pre>$ apt-get update<\/pre>\n<pre>$ apt-get install -y python python-webpy<\/pre>\n<p><br style=\"height: 1em;\" \/>Within the container, create a new python script:<\/p>\n<pre>$ sudo mkdir \/home\/test1\n$ sudo vi \/home\/test1\/app.py<\/pre>\n<p><br style=\"height: 1em;\" \/>The contents of the script are:<\/p>\n<pre>#!\/usr\/bin\/python<\/pre>\n<pre>import web,sys<\/pre>\n<pre>urls = (\n '\/', 'index'\n )<\/pre>\n<pre>app = web.application(urls, globals())<\/pre>\n<pre>class index:\n def GET(self):\n argumentone = sys.argv[2]\n greeting = \"Hello World, the test message is \" + argumentone\n return greeting<\/pre>\n<pre>if __name__ == '__main__' :\n app = web.application(urls, globals())\n app.run()<\/pre>\n<p><br style=\"height: 1em;\" \/>Exit the container, back to the Native OS:<\/p>\n<pre>$ exit<\/pre>\n<p><br style=\"height: 1em;\" \/>Confirm the name of your container (the last container run):<\/p>\n<pre>$ sudo docker ps \u2013l\n CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES\n f711ff0fd695 ubuntu:14.04 \/bin\/bash 32 minutes ago Exit 0 firstcontainer<\/pre>\n<p><br style=\"height: 1em;\" \/>Create a new image from your docker called testpython1<\/p>\n<pre>$ sudo docker commit firstcontainer testpython:0.1<\/pre>\n<p><br style=\"height: 1em;\" \/>Confirm you can see the image and get the image ID:<\/p>\n<pre>$ sudo docker images\n REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE\n testpython 0.1 <span style=\"color: #ff0000;\">fcb365f7591b<\/span> 2 minutes ago 247.8 MB<\/pre>\n<p><br style=\"height: 1em;\" \/>Finally, start up 3 instances of your web application:<\/p>\n<pre>$ sudo docker run -d -p 8081:8081 fcb365f7591b python \/home\/test1\/app.py 8081 \"instance1\"\n$ sudo docker run -d -p 8082:8082 fcb365f7591b python \/home\/test1\/app.py 8082 \"instance2\"\n$ sudo docker run -d -p 8083:8083 fcb365f7591b python \/home\/test1\/app.py 8083 \"instance3\"<\/pre>\n<p><br style=\"height: 1em;\" \/>Open a browser on your network and connect to http:\/\/dockerserverip:8081<br \/>\nTry the same for the other two port numbers. Note we now have a system running 3 separate containers which could then be load balanced using a third party tool, or even run completely different content. Cool huh?<\/p>\n<p>Next, how to mount a drive into your container&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you\u2019ve been following this series (last part here), we now have docker installed, but what do we do next? Create our first containers of course! I think [..]<\/p>\n","protected":false},"author":1,"featured_media":1157,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_yoast_wpseo_focuskw":"","_yoast_wpseo_title":"","_yoast_wpseo_metadesc":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"New Blog Post: #Docker Part 3 - HOWTO Create a Simple #Python Web App in Docker | #vExpert","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[337],"tags":[340,339,342,332,351,344,346,349,350,58,348,347],"class_list":["post-1161","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-docker-2","tag-1-2-0","tag-14-04","tag-containers","tag-docker","tag-howto","tag-images","tag-python","tag-test","tag-tutorial","tag-ubuntu","tag-web-application","tag-web-py"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/tekhead.it\/wp-uploads\/www.tekhead.org\/2014\/09\/dockerlogo-e1452888993804.png","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2l3lU-iJ","amp_enabled":true,"_links":{"self":[{"href":"https:\/\/tekhead.it\/blog\/wp-json\/wp\/v2\/posts\/1161","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tekhead.it\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tekhead.it\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tekhead.it\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tekhead.it\/blog\/wp-json\/wp\/v2\/comments?post=1161"}],"version-history":[{"count":0,"href":"https:\/\/tekhead.it\/blog\/wp-json\/wp\/v2\/posts\/1161\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tekhead.it\/blog\/wp-json\/wp\/v2\/media\/1157"}],"wp:attachment":[{"href":"https:\/\/tekhead.it\/blog\/wp-json\/wp\/v2\/media?parent=1161"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tekhead.it\/blog\/wp-json\/wp\/v2\/categories?post=1161"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tekhead.it\/blog\/wp-json\/wp\/v2\/tags?post=1161"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}