python_web_app.rst 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. :title: Python Web app example
  2. :description: Building your own python web app using docker
  3. :keywords: docker, example, python, web app
  4. .. _python_web_app:
  5. Python Web App
  6. ==============
  7. .. include:: example_header.inc
  8. While using Dockerfiles is the prefered way to create maintainable
  9. and repeatable images, its useful to know how you can try things out
  10. and then commit your live changes to an image.
  11. The goal of this example is to show you how you can modify your own
  12. Docker images by making changes to a running
  13. container, and then saving the results as a new image. We will do
  14. that by making a simple 'hello world' Flask web application image.
  15. **Steps:**
  16. .. code-block:: bash
  17. $ sudo docker pull shykes/pybuilder
  18. Download the ``shykes/pybuilder`` Docker image from the ``http://index.docker.io``
  19. registry. Note that this container was built with a very old version of docker
  20. (May 2013), but can still be used now.
  21. .. code-block:: bash
  22. $ sudo docker run -i -t -name pybuilder_run shykes/pybuilder bash
  23. $$ URL=http://github.com/shykes/helloflask/archive/master.tar.gz
  24. $$ /usr/local/bin/buildapp $URL
  25. [lots of output later]
  26. $$ exit
  27. We then start a new container running interactively using the
  28. image.
  29. First, we set a ``URL`` variable that points to a tarball of a simple
  30. helloflask web app, and then we run a command contained in the image called
  31. ``buildapp``, passing it the ``$URL`` variable. The container is
  32. given a name ``pybuilder_run`` which we will use in the next steps.
  33. While this example is simple, you could run any number of interactive commands,
  34. try things out, and then exit when you're done.
  35. .. code-block:: bash
  36. $ sudo docker commit pybuilder_run /builds/github.com/shykes/helloflask/master
  37. c8b2e8228f11b8b3e492cbf9a49923ae66496230056d61e07880dc74c5f495f9
  38. Save the changes we just made in the container to a new image called
  39. ``/builds/github.com/hykes/helloflask/master``. You now have 3 different
  40. ways to refer to the container, name, short-id ``c8b2e8228f11``, or
  41. long-id ``c8b2e8228f11b8b3e492cbf9a49923ae66496230056d61e07880dc74c5f495f9``.
  42. .. code-block:: bash
  43. $ WEB_WORKER=$(sudo docker run -d -p 5000 /builds/github.com/hykes/helloflask/master /usr/local/bin/runapp)
  44. Use the new image to create a new container with
  45. network port 5000, and return the container ID and store in the
  46. ``WEB_WORKER`` variable (rather than naming a container/image, you can use the ID's).
  47. - **"docker run -d "** run a command in a new container. We pass "-d"
  48. so it runs as a daemon.
  49. - **"-p 5000"** the web app is going to listen on this port, so it
  50. must be mapped from the container to the host system.
  51. - **/usr/local/bin/runapp** is the command which starts the web app.
  52. .. code-block:: bash
  53. $ sudo docker logs -f $WEB_WORKER
  54. * Running on http://0.0.0.0:5000/
  55. View the logs for the new container using the ``WEB_WORKER`` variable, and
  56. if everything worked as planned you should see the line ``Running on
  57. http://0.0.0.0:5000/`` in the log output.
  58. To exit the view without stopping the container, hit Ctrl-C, or open another
  59. terminal and continue with the example while watching the result in the logs.
  60. .. code-block:: bash
  61. $ WEB_PORT=$(sudo docker port $WEB_WORKER 5000 | awk -F: '{ print $2 }')
  62. Look up the public-facing port which is NAT-ed. Find the private port
  63. used by the container and store it inside of the ``WEB_PORT`` variable.
  64. .. code-block:: bash
  65. # install curl if necessary, then ...
  66. $ curl http://127.0.0.1:$WEB_PORT
  67. Hello world!
  68. Access the web app using the ``curl`` binary. If everything worked as planned you
  69. should see the line ``Hello world!`` inside of your console.
  70. .. code-block:: bash
  71. $ sudo docker ps --all
  72. List ``--all`` the Docker containers. If this container had already finished
  73. running, it will still be listed here with a status of 'Exit 0'.
  74. .. code-block:: bash
  75. $ sudo docker stop $WEB_WORKER
  76. $ sudo docker rm $WEB_WORKER pybuilder_run
  77. $ sudo docker rmi /builds/github.com/shykes/helloflask/master shykes/pybuilder:latest
  78. And now stop the running web worker, and delete the containers, so that we can
  79. then delete the images that we used.