hello_world_daemon.rst 2.7 KB

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