hello_world_daemon.rst 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. :title: Hello world daemon example
  2. :description: A simple hello world daemon example with Docker
  3. :keywords: docker, example, hello world, daemon
  4. .. _hello_world_daemon:
  5. Hello World Daemon
  6. ==================
  7. .. include:: example_header.inc
  8. The most boring daemon ever written.
  9. This example assumes you have Docker installed and with the Ubuntu
  10. image already imported ``docker pull ubuntu``. We will use the Ubuntu
  11. image to run a simple hello world daemon that will just print hello
  12. world to standard out every second. It will continue to do this until
  13. we stop it.
  14. **Steps:**
  15. .. code-block:: bash
  16. CONTAINER_ID=$(sudo docker run -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done")
  17. We are going to run a simple hello world daemon in a new container
  18. made from the *ubuntu* image.
  19. - **"docker run -d "** run a command in a new container. We pass "-d"
  20. so it runs as a daemon.
  21. - **"ubuntu"** is the image we want to run the command inside of.
  22. - **"/bin/sh -c"** is the command we want to run in the container
  23. - **"while true; do echo hello world; sleep 1; done"** is the mini
  24. script we want to run, that will just print hello world once a
  25. second until we stop it.
  26. - **$CONTAINER_ID** the output of the run command will return a
  27. container id, we can use in future commands to see what is going on
  28. with this process.
  29. .. code-block:: bash
  30. sudo docker logs $CONTAINER_ID
  31. Check the logs make sure it is working correctly.
  32. - **"docker logs**" This will return the logs for a container
  33. - **$CONTAINER_ID** The Id of the container we want the logs for.
  34. .. code-block:: bash
  35. sudo docker attach $CONTAINER_ID
  36. Attach to the container to see the results in realtime.
  37. - **"docker attach**" This will allow us to attach to a background
  38. process to see what is going on.
  39. - **$CONTAINER_ID** The Id of the container we want to attach too.
  40. .. code-block:: bash
  41. sudo docker ps
  42. Check the process list to make sure it is running.
  43. - **"docker ps"** this shows all running process managed by docker
  44. .. code-block:: bash
  45. sudo docker stop $CONTAINER_ID
  46. Stop the container, since we don't need it anymore.
  47. - **"docker stop"** This stops a container
  48. - **$CONTAINER_ID** The Id of the container we want to stop.
  49. .. code-block:: bash
  50. sudo docker ps
  51. Make sure it is really stopped.
  52. **Video:**
  53. See the example in action
  54. .. raw:: html
  55. <div style="margin-top:10px;">
  56. <iframe width="560" height="350" src="http://ascii.io/a/2562/raw" frameborder="0"></iframe>
  57. </div>
  58. Continue to the :ref:`python_web_app` example.