hello_world.rst 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. :title: Hello world example
  2. :description: A simple hello world example with Docker
  3. :keywords: docker, example, hello world
  4. .. _running_examples:
  5. Check your Docker install
  6. -------------------------
  7. This guide assumes you have a working installation of Docker. To check
  8. your Docker install, run the following command:
  9. .. code-block:: bash
  10. # Check that you have a working install
  11. $ sudo docker info
  12. If you get ``docker: command not found`` or something like
  13. ``/var/lib/docker/repositories: permission denied`` you may have an incomplete
  14. Docker installation or insufficient privileges to access docker on your machine.
  15. Please refer to :ref:`installation_list` for installation instructions.
  16. .. _hello_world:
  17. Hello World
  18. -----------
  19. .. include:: example_header.inc
  20. This is the most basic example available for using Docker.
  21. Download the small base image named ``busybox``:
  22. .. code-block:: bash
  23. # Download a busybox image
  24. $ sudo docker pull busybox
  25. The ``busybox`` image is a minimal Linux system. You can do the same
  26. with any number of other images, such as ``debian``, ``ubuntu`` or ``centos``.
  27. The images can be found and retrieved using the `Docker index`_.
  28. .. _Docker index: http://index.docker.io
  29. .. code-block:: bash
  30. $ sudo docker run busybox /bin/echo hello world
  31. This command will run a simple ``echo`` command, that will echo ``hello world`` back to the console over standard out.
  32. **Explanation:**
  33. - **"sudo"** execute the following commands as user *root*
  34. - **"docker run"** run a command in a new container
  35. - **"busybox"** is the image we are running the command in.
  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. <iframe width="560" height="400" frameborder="0"
  42. sandbox="allow-same-origin allow-scripts"
  43. srcdoc="<body><script type=&quot;text/javascript&quot;
  44. src=&quot;https://asciinema.org/a/7658.js&quot;
  45. id=&quot;asciicast-7658&quot; async></script></body>">
  46. </iframe>
  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. We will use the Ubuntu image to run a simple hello world daemon that will just print hello
  54. world to standard out every second. It will continue to do this until
  55. we stop it.
  56. **Steps:**
  57. .. code-block:: bash
  58. container_id=$(sudo docker run -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done")
  59. We are going to run a simple hello world daemon in a new container
  60. made from the ``ubuntu`` image.
  61. - **"sudo docker run -d "** run a command in a new container. We pass "-d"
  62. so it runs as a daemon.
  63. - **"ubuntu"** is the image we want to run the command inside of.
  64. - **"/bin/sh -c"** is the command we want to run in the container
  65. - **"while true; do echo hello world; sleep 1; done"** is the mini
  66. script we want to run, that will just print hello world once a
  67. second until we stop it.
  68. - **$container_id** the output of the run command will return a
  69. container id, we can use in future commands to see what is going on
  70. with this process.
  71. .. code-block:: bash
  72. sudo docker logs $container_id
  73. Check the logs make sure it is working correctly.
  74. - **"docker logs**" This will return the logs for a container
  75. - **$container_id** The Id of the container we want the logs for.
  76. .. code-block:: bash
  77. sudo docker attach --sig-proxy=false $container_id
  78. Attach to the container to see the results in real-time.
  79. - **"docker attach**" This will allow us to attach to a background
  80. process to see what is going on.
  81. - **"--sig-proxy=false"** Do not forward signals to the container; allows
  82. us to exit the attachment using Control-C without stopping the container.
  83. - **$container_id** The Id of the container we want to attach to.
  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. <iframe width="560" height="400" frameborder="0"
  101. sandbox="allow-same-origin allow-scripts"
  102. srcdoc="<body><script type=&quot;text/javascript&quot;
  103. src=&quot;https://asciinema.org/a/2562.js&quot;
  104. id=&quot;asciicast-2562&quot; async></script></body>">
  105. </iframe>
  106. The next example in the series is a :ref:`nodejs_web_app` example, or
  107. you could skip to any of the other examples:
  108. * :ref:`nodejs_web_app`
  109. * :ref:`running_redis_service`
  110. * :ref:`running_ssh_service`
  111. * :ref:`running_couchdb_service`
  112. * :ref:`postgresql_service`
  113. * :ref:`mongodb_image`
  114. * :ref:`python_web_app`