hello_world_daemon.rst 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 the Ubuntu
  10. image already imported with ``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. Exit from the container attachment by pressing Control-C.
  41. .. code-block:: bash
  42. sudo docker ps
  43. Check the process list to make sure it is running.
  44. - **"docker ps"** this shows all running process managed by docker
  45. .. code-block:: bash
  46. sudo docker stop $CONTAINER_ID
  47. Stop the container, since we don't need it anymore.
  48. - **"docker stop"** This stops a container
  49. - **$CONTAINER_ID** The Id of the container we want to stop.
  50. .. code-block:: bash
  51. sudo docker ps
  52. Make sure it is really stopped.
  53. **Video:**
  54. See the example in action
  55. .. raw:: html
  56. <div style="margin-top:10px;">
  57. <iframe width="560" height="350" src="http://ascii.io/a/2562/raw" frameborder="0"></iframe>
  58. </div>
  59. Continue to the :ref:`python_web_app` example.