hello_world.rst 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. :title: Hello world example
  2. :description: A simple hello world example with Docker
  3. :keywords: docker, example, hello world
  4. .. _examples:
  5. Hello World
  6. -----------
  7. .. _running_examples:
  8. Running the Examples
  9. ====================
  10. All the examples assume your machine is running the docker daemon. To
  11. run the docker daemon in the background, simply type:
  12. .. code-block:: bash
  13. sudo docker -d &
  14. Now you can run docker in client mode: by default all commands will be
  15. forwarded to the ``docker`` daemon via a protected Unix socket, so you
  16. must run as root.
  17. .. code-block:: bash
  18. sudo docker help
  19. ----
  20. .. _hello_world:
  21. Hello World
  22. ===========
  23. .. include:: example_header.inc
  24. This is the most basic example available for using Docker.
  25. Download the base image (named "ubuntu"):
  26. .. code-block:: bash
  27. # Download an ubuntu image
  28. sudo docker pull ubuntu
  29. Alternatively to the *ubuntu* image, you can select *busybox*, a bare
  30. minimal Linux system. The images are retrieved from the Docker
  31. repository.
  32. .. code-block:: bash
  33. #run a simple echo command, that will echo hello world back to the console over standard out.
  34. sudo docker run ubuntu /bin/echo hello world
  35. **Explanation:**
  36. - **"sudo"** execute the following commands as user *root*
  37. - **"docker run"** run a command in a new container
  38. - **"ubuntu"** is the image we want to run the command inside of.
  39. - **"/bin/echo"** is the command we want to run in the container
  40. - **"hello world"** is the input for the echo command
  41. **Video:**
  42. See the example in action
  43. .. raw:: html
  44. <div style="margin-top:10px;">
  45. <iframe width="560" height="350" src="http://ascii.io/a/2603/raw" frameborder="0"></iframe>
  46. </div>
  47. ----
  48. .. _hello_world_daemon:
  49. Hello World Daemon
  50. ==================
  51. .. include:: example_header.inc
  52. And now for the most boring daemon ever written!
  53. This example assumes you have Docker installed and the Ubuntu
  54. image already imported with ``docker pull ubuntu``. We will use the Ubuntu
  55. image to run a simple hello world daemon that will just print hello
  56. world to standard out every second. It will continue to do this until
  57. we stop it.
  58. **Steps:**
  59. .. code-block:: bash
  60. CONTAINER_ID=$(sudo docker run -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done")
  61. We are going to run a simple hello world daemon in a new container
  62. made from the *ubuntu* image.
  63. - **"docker run -d "** run a command in a new container. We pass "-d"
  64. so it runs as a daemon.
  65. - **"ubuntu"** is the image we want to run the command inside of.
  66. - **"/bin/sh -c"** is the command we want to run in the container
  67. - **"while true; do echo hello world; sleep 1; done"** is the mini
  68. script we want to run, that will just print hello world once a
  69. second until we stop it.
  70. - **$CONTAINER_ID** the output of the run command will return a
  71. container id, we can use in future commands to see what is going on
  72. with this process.
  73. .. code-block:: bash
  74. sudo docker logs $CONTAINER_ID
  75. Check the logs make sure it is working correctly.
  76. - **"docker logs**" This will return the logs for a container
  77. - **$CONTAINER_ID** The Id of the container we want the logs for.
  78. .. code-block:: bash
  79. sudo docker attach $CONTAINER_ID
  80. Attach to the container to see the results in realtime.
  81. - **"docker attach**" This will allow us to attach to a background
  82. process to see what is going on.
  83. - **$CONTAINER_ID** The Id of the container we want to attach too.
  84. Exit from the container attachment by pressing Control-C.
  85. .. code-block:: bash
  86. sudo docker ps
  87. Check the process list to make sure it is running.
  88. - **"docker ps"** this shows all running process managed by docker
  89. .. code-block:: bash
  90. sudo docker stop $CONTAINER_ID
  91. Stop the container, since we don't need it anymore.
  92. - **"docker stop"** This stops a container
  93. - **$CONTAINER_ID** The Id of the container we want to stop.
  94. .. code-block:: bash
  95. sudo docker ps
  96. Make sure it is really stopped.
  97. **Video:**
  98. See the example in action
  99. .. raw:: html
  100. <div style="margin-top:10px;">
  101. <iframe width="560" height="350" src="http://ascii.io/a/2562/raw" frameborder="0"></iframe>
  102. </div>
  103. The next example in the series is a :ref:`python_web_app` example, or
  104. you could skip to any of the other examples:
  105. * :ref:`python_web_app`
  106. * :ref:`nodejs_web_app`
  107. * :ref:`running_redis_service`
  108. * :ref:`running_ssh_service`
  109. * :ref:`running_couchdb_service`
  110. * :ref:`postgresql_service`
  111. * :ref:`mongodb_image`