hello_world_daemon.rst 2.4 KB

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