python_web_app.rst 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. The goal of this example is to show you how you can author your own
  9. Docker images using a parent image, making changes to it, and then
  10. saving the results as a new image. We will do that by making a simple
  11. hello Flask web application image.
  12. **Steps:**
  13. .. code-block:: bash
  14. sudo docker pull shykes/pybuilder
  15. We are downloading the ``shykes/pybuilder`` Docker image
  16. .. code-block:: bash
  17. URL=http://github.com/shykes/helloflask/archive/master.tar.gz
  18. We set a ``URL`` variable that points to a tarball of a simple helloflask web app
  19. .. code-block:: bash
  20. BUILD_JOB=$(sudo docker run -d -t shykes/pybuilder:latest /usr/local/bin/buildapp $URL)
  21. Inside of the ``shykes/pybuilder`` image there is a command called
  22. ``buildapp``, we are running that command and passing the ``$URL`` variable
  23. from step 2 to it, and running the whole thing inside of a new
  24. container. The ``BUILD_JOB`` environment variable will be set with the new container ID.
  25. .. code-block:: bash
  26. sudo docker attach -sig-proxy=false $BUILD_JOB
  27. [...]
  28. While this container is running, we can attach to the new container to
  29. see what is going on. The flag ``--sig-proxy`` set as ``false`` allows you to connect and
  30. disconnect (Ctrl-C) to it without stopping the container.
  31. .. code-block:: bash
  32. sudo docker ps -a
  33. List all Docker containers. If this container has already finished
  34. running, it will still be listed here.
  35. .. code-block:: bash
  36. BUILD_IMG=$(sudo docker commit $BUILD_JOB _/builds/github.com/shykes/helloflask/master)
  37. Save the changes we just made in the container to a new image called
  38. ``_/builds/github.com/hykes/helloflask/master`` and save the image ID in
  39. the ``BUILD_IMG`` variable name.
  40. .. code-block:: bash
  41. WEB_WORKER=$(sudo docker run -d -p 5000 $BUILD_IMG /usr/local/bin/runapp)
  42. - **"docker run -d "** run a command in a new container. We pass "-d"
  43. so it runs as a daemon.
  44. - **"-p 5000"** the web app is going to listen on this port, so it
  45. must be mapped from the container to the host system.
  46. - **"$BUILD_IMG"** is the image we want to run the command inside of.
  47. - **/usr/local/bin/runapp** is the command which starts the web app.
  48. Use the new image we just created and create a new container with
  49. network port 5000, and return the container ID and store in the
  50. ``WEB_WORKER`` variable.
  51. .. code-block:: bash
  52. sudo docker logs $WEB_WORKER
  53. * Running on http://0.0.0.0:5000/
  54. View the logs for the new container using the ``WEB_WORKER`` variable, and
  55. if everything worked as planned you should see the line ``Running on
  56. http://0.0.0.0:5000/`` in the log output.
  57. .. code-block:: bash
  58. WEB_PORT=$(sudo docker port $WEB_WORKER 5000 | awk -F: '{ print $2 }')
  59. Look up the public-facing port which is NAT-ed. Find the private port
  60. used by the container and store it inside of the ``WEB_PORT`` variable.
  61. .. code-block:: bash
  62. # install curl if necessary, then ...
  63. curl http://127.0.0.1:$WEB_PORT
  64. Hello world!
  65. Access the web app using the ``curl`` binary. If everything worked as planned you
  66. should see the line ``Hello world!`` inside of your console.
  67. **Video:**
  68. See the example in action
  69. .. raw:: html
  70. <div style="margin-top:10px;">
  71. <iframe width="720" height="350" src="http://ascii.io/a/2573/raw" frameborder="0"></iframe>
  72. </div>
  73. Continue to :ref:`running_ssh_service`.