hello_world.rst 4.3 KB

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