hello_world.rst 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. <iframe width="560" height="400" frameborder="0"
  44. sandbox="allow-same-origin allow-scripts"
  45. srcdoc="<body><script type=&quot;text/javascript&quot;
  46. src=&quot;https://asciinema.org/a/2603.js&quot;
  47. id=&quot;asciicast-2603&quot; async></script></body>">
  48. </iframe>
  49. ----
  50. .. _hello_world_daemon:
  51. Hello World Daemon
  52. ==================
  53. .. include:: example_header.inc
  54. And now for the most boring daemon ever written!
  55. We will use the Ubuntu 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. - **"sudo 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 -sig-proxy=false $CONTAINER_ID
  80. Attach to the container to see the results in real-time.
  81. - **"docker attach**" This will allow us to attach to a background
  82. process to see what is going on.
  83. - **"-sig-proxy=false"** Do not forward signals to the container; allows
  84. us to exit the attachment using Control-C without stopping the container.
  85. - **$CONTAINER_ID** The Id of the container we want to attach too.
  86. Exit from the container attachment by pressing Control-C.
  87. .. code-block:: bash
  88. sudo docker ps
  89. Check the process list to make sure it is running.
  90. - **"docker ps"** this shows all running process managed by docker
  91. .. code-block:: bash
  92. sudo docker stop $CONTAINER_ID
  93. Stop the container, since we don't need it anymore.
  94. - **"docker stop"** This stops a container
  95. - **$CONTAINER_ID** The Id of the container we want to stop.
  96. .. code-block:: bash
  97. sudo docker ps
  98. Make sure it is really stopped.
  99. **Video:**
  100. See the example in action
  101. .. raw:: html
  102. <iframe width="560" height="400" frameborder="0"
  103. sandbox="allow-same-origin allow-scripts"
  104. srcdoc="<body><script type=&quot;text/javascript&quot;
  105. src=&quot;https://asciinema.org/a/2562.js&quot;
  106. id=&quot;asciicast-2562&quot; async></script></body>">
  107. </iframe>
  108. The next example in the series is a :ref:`python_web_app` example, or
  109. you could skip to any of the other examples:
  110. * :ref:`python_web_app`
  111. * :ref:`nodejs_web_app`
  112. * :ref:`running_redis_service`
  113. * :ref:`running_ssh_service`
  114. * :ref:`running_couchdb_service`
  115. * :ref:`postgresql_service`
  116. * :ref:`mongodb_image`