hello_world.rst 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. :title: Hello world example
  2. :description: A simple hello world example with Docker
  3. :keywords: docker, example, hello world
  4. .. _examples:
  5. .. _running_examples:
  6. Check your Docker install
  7. -------------------------
  8. This guide assumes you have a working installation of Docker. To check
  9. your Docker install, run the following command:
  10. .. code-block:: bash
  11. # Check that you have a working install
  12. $ sudo docker info
  13. If you get ``docker: command not found`` or something like
  14. ``/var/lib/docker/repositories: permission denied`` you may have an incomplete
  15. Docker installation or insufficient privileges to access docker on your machine.
  16. Please refer to :ref:`installation_list` for installation instructions.
  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 small base image named ``busybox``:
  23. .. code-block:: bash
  24. # Download an busybox image
  25. $ sudo docker pull busybox
  26. The ``busybox`` image is a minimal Linux system. You can do the same
  27. with any number of other images, such as ``debian``, ``ubuntu`` or ``centos``.
  28. The images can be found and retrieved using the `Docker index`_.
  29. .. _Docker index: http://index.docker.io
  30. .. code-block:: bash
  31. $ sudo docker run busybox /bin/echo hello world
  32. This command will run a simple ``echo`` command, that will echo ``hello world`` back to the console over standard out.
  33. **Explanation:**
  34. - **"sudo"** execute the following commands as user *root*
  35. - **"docker run"** run a command in a new container
  36. - **"busybox"** is the image we are running the command in.
  37. - **"/bin/echo"** is the command we want to run in the container
  38. - **"hello world"** is the input for the echo command
  39. **Video:**
  40. See the example in action
  41. .. raw:: html
  42. <iframe width="560" height="400" frameborder="0"
  43. sandbox="allow-same-origin allow-scripts"
  44. srcdoc="<body><script type=&quot;text/javascript&quot;
  45. src=&quot;https://asciinema.org/a/2603.js&quot;
  46. id=&quot;asciicast-2603&quot; async></script></body>">
  47. </iframe>
  48. ----
  49. .. _hello_world_daemon:
  50. Hello World Daemon
  51. ------------------
  52. .. include:: example_header.inc
  53. And now for the most boring daemon ever written!
  54. We will use the Ubuntu image to run a simple hello world daemon that will just print hello
  55. world to standard out every second. It will continue to do this until
  56. we stop it.
  57. **Steps:**
  58. .. code-block:: bash
  59. CONTAINER_ID=$(sudo docker run -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done")
  60. We are going to run a simple hello world daemon in a new container
  61. made from the ``ubuntu`` image.
  62. - **"sudo docker run -d "** run a command in a new container. We pass "-d"
  63. so it runs as a daemon.
  64. - **"ubuntu"** is the image we want to run the command inside of.
  65. - **"/bin/sh -c"** is the command we want to run in the container
  66. - **"while true; do echo hello world; sleep 1; done"** is the mini
  67. script we want to run, that will just print hello world once a
  68. second until we stop it.
  69. - **$CONTAINER_ID** the output of the run command will return a
  70. container id, we can use in future commands to see what is going on
  71. with this process.
  72. .. code-block:: bash
  73. sudo docker logs $CONTAINER_ID
  74. Check the logs make sure it is working correctly.
  75. - **"docker logs**" This will return the logs for a container
  76. - **$CONTAINER_ID** The Id of the container we want the logs for.
  77. .. code-block:: bash
  78. sudo docker attach -sig-proxy=false $CONTAINER_ID
  79. Attach to the container to see the results in real-time.
  80. - **"docker attach**" This will allow us to attach to a background
  81. process to see what is going on.
  82. - **"-sig-proxy=false"** Do not forward signals to the container; allows
  83. us to exit the attachment using Control-C without stopping the container.
  84. - **$CONTAINER_ID** The Id of the container we want to attach too.
  85. Exit from the container attachment by pressing Control-C.
  86. .. code-block:: bash
  87. sudo docker ps
  88. Check the process list to make sure it is running.
  89. - **"docker ps"** this shows all running process managed by docker
  90. .. code-block:: bash
  91. sudo docker stop $CONTAINER_ID
  92. Stop the container, since we don't need it anymore.
  93. - **"docker stop"** This stops a container
  94. - **$CONTAINER_ID** The Id of the container we want to stop.
  95. .. code-block:: bash
  96. sudo docker ps
  97. Make sure it is really stopped.
  98. **Video:**
  99. See the example in action
  100. .. raw:: html
  101. <iframe width="560" height="400" frameborder="0"
  102. sandbox="allow-same-origin allow-scripts"
  103. srcdoc="<body><script type=&quot;text/javascript&quot;
  104. src=&quot;https://asciinema.org/a/2562.js&quot;
  105. id=&quot;asciicast-2562&quot; async></script></body>">
  106. </iframe>
  107. The next example in the series is a :ref:`nodejs_web_app` example, or
  108. you could skip to any of the other examples:
  109. * :ref:`nodejs_web_app`
  110. * :ref:`running_redis_service`
  111. * :ref:`running_ssh_service`
  112. * :ref:`running_couchdb_service`
  113. * :ref:`postgresql_service`
  114. * :ref:`mongodb_image`
  115. * :ref:`python_web_app`