hello_world_daemon.rst 2.5 KB

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