python_web_app.rst 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 docker images using a parent image, making changes to it, and then saving the results as a new image. We will do that by making a simple hello flask web application image.
  9. **Steps:**
  10. .. code-block:: bash
  11. docker pull shykes/pybuilder
  12. We are downloading the "shykes/pybuilder" docker image
  13. .. code-block:: bash
  14. URL=http://github.com/shykes/helloflask/archive/master.tar.gz
  15. We set a URL variable that points to a tarball of a simple helloflask web app
  16. .. code-block:: bash
  17. BUILD_JOB=$(docker run -d -t shykes/pybuilder:latest /usr/local/bin/buildapp $URL)
  18. Inside of the "shykes/pybuilder" image there is a command called buildapp, we are running that command and passing the $URL variable from step 2 to it, and running the whole thing inside of a new container. BUILD_JOB will be set with the new container_id.
  19. .. code-block:: bash
  20. docker attach $BUILD_JOB
  21. [...]
  22. We attach to the new container to see what is going on. Ctrl-C to disconnect
  23. .. code-block:: bash
  24. BUILD_IMG=$(docker commit $BUILD_JOB _/builds/github.com/shykes/helloflask/master)
  25. Save the changes we just made in the container to a new image called "_/builds/github.com/hykes/helloflask/master" and save the image id in the BUILD_IMG variable name.
  26. .. code-block:: bash
  27. WEB_WORKER=$(docker run -d -p 5000 $BUILD_IMG /usr/local/bin/runapp)
  28. - **"docker run -d "** run a command in a new container. We pass "-d" so it runs as a daemon.
  29. - **"-p 5000"** the web app is going to listen on this port, so it must be mapped from the container to the host system.
  30. - **"$BUILD_IMG"** is the image we want to run the command inside of.
  31. - **/usr/local/bin/runapp** is the command which starts the web app.
  32. Use the new image we just created and create a new container with network port 5000, and return the container id and store in the WEB_WORKER variable.
  33. .. code-block:: bash
  34. docker logs $WEB_WORKER
  35. * Running on http://0.0.0.0:5000/
  36. View the logs for the new container using the WEB_WORKER variable, and if everything worked as planned you should see the line "Running on http://0.0.0.0:5000/" in the log output.
  37. .. code-block:: bash
  38. WEB_PORT=$(docker port $WEB_WORKER 5000)
  39. Look up the public-facing port which is NAT-ed. Find the private port used by the container and store it inside of the WEB_PORT variable.
  40. .. code-block:: bash
  41. # install curl if necessary, then ...
  42. curl http://127.0.0.1:$WEB_PORT
  43. Hello world!
  44. Access the web app using curl. If everything worked as planned you should see the line "Hello world!" inside of your console.
  45. **Video:**
  46. See the example in action
  47. .. raw:: html
  48. <div style="margin-top:10px;">
  49. <iframe width="720" height="350" src="http://ascii.io/a/2573/raw" frameborder="0"></iframe>
  50. </div>
  51. Continue to :ref:`running_ssh_service`.