hello_world.rst 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. Check your Docker install
  9. -------------------------
  10. This guide assumes you have a working installation of Docker. To check
  11. your Docker install, run the following command:
  12. .. code-block:: bash
  13. # Check that you have a working install
  14. docker info
  15. If you get ``docker: command not found`` or something like
  16. ``/var/lib/docker/repositories: permission denied`` you may have an incomplete
  17. Docker installation or insufficient privileges to access docker on your machine.
  18. Please refer to :ref:`installation_list` for installation instructions.
  19. .. _hello_world:
  20. Hello World
  21. ===========
  22. .. include:: example_header.inc
  23. This is the most basic example available for using Docker.
  24. Download the base image which is named ``ubuntu``:
  25. .. code-block:: bash
  26. # Download an ubuntu image
  27. sudo docker pull ubuntu
  28. Alternatively to the ``ubuntu`` image, you can select ``busybox``, a bare
  29. minimal Linux system. The images are retrieved from the Docker
  30. repository.
  31. .. code-block:: bash
  32. sudo docker run ubuntu /bin/echo hello world
  33. This command will run a simple ``echo`` command, that will echo ``hello world`` back to the console over standard out.
  34. **Explanation:**
  35. - **"sudo"** execute the following commands as user *root*
  36. - **"docker run"** run a command in a new container
  37. - **"ubuntu"** is the image we want to run the command inside of.
  38. - **"/bin/echo"** is the command we want to run in the container
  39. - **"hello world"** is the input for the echo command
  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/2603/raw" frameborder="0"></iframe>
  45. </div>
  46. ----
  47. .. _hello_world_daemon:
  48. Hello World Daemon
  49. ==================
  50. .. include:: example_header.inc
  51. And now for the most boring daemon ever written!
  52. We will use the Ubuntu 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. - **"sudo 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 -sig-proxy=false $CONTAINER_ID
  77. Attach to the container to see the results in real-time.
  78. - **"docker attach**" This will allow us to attach to a background
  79. process to see what is going on.
  80. - **"-sig-proxy=false"** Do not forward signals to the container; allows
  81. us to exit the attachment using Control-C without stopping the container.
  82. - **$CONTAINER_ID** The Id of the container we want to attach too.
  83. Exit from the container attachment by pressing Control-C.
  84. .. code-block:: bash
  85. sudo docker ps
  86. Check the process list to make sure it is running.
  87. - **"docker ps"** this shows all running process managed by docker
  88. .. code-block:: bash
  89. sudo docker stop $CONTAINER_ID
  90. Stop the container, since we don't need it anymore.
  91. - **"docker stop"** This stops a container
  92. - **$CONTAINER_ID** The Id of the container we want to stop.
  93. .. code-block:: bash
  94. sudo docker ps
  95. Make sure it is really stopped.
  96. **Video:**
  97. See the example in action
  98. .. raw:: html
  99. <div style="margin-top:10px;">
  100. <iframe width="560" height="350" src="http://ascii.io/a/2562/raw" frameborder="0"></iframe>
  101. </div>
  102. The next example in the series is a :ref:`python_web_app` example, or
  103. you could skip to any of the other examples:
  104. * :ref:`python_web_app`
  105. * :ref:`nodejs_web_app`
  106. * :ref:`running_redis_service`
  107. * :ref:`running_ssh_service`
  108. * :ref:`running_couchdb_service`
  109. * :ref:`postgresql_service`
  110. * :ref:`mongodb_image`