basics.rst 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. :title: Basic Commands
  2. :description: Common usage and commands
  3. :keywords: Examples, Usage, basic commands, docker, documentation, examples
  4. The Basics
  5. ==========
  6. Starting Docker
  7. ---------------
  8. If you have used one of the quick install paths', Docker may have been
  9. installed with upstart, Ubuntu's system for starting processes at boot
  10. time. You should be able to run ``sudo docker help`` and get output.
  11. If you get ``docker: command not found`` or something like
  12. ``/var/lib/docker/repositories: permission denied`` you will need to
  13. specify the path to it and manually start it.
  14. .. code-block:: bash
  15. # Run docker in daemon mode
  16. sudo <path to>/docker -d &
  17. Download a pre-built image
  18. --------------------------
  19. .. code-block:: bash
  20. # Download an ubuntu image
  21. sudo docker pull ubuntu
  22. This will find the ``ubuntu`` image by name in the :ref:`Central Index
  23. <searching_central_index>` and download it from the top-level Central
  24. Repository to a local image cache.
  25. .. NOTE:: When the image has successfully downloaded, you will see a 12
  26. character hash ``539c0211cd76: Download complete`` which is the short
  27. form of the image ID. These short image IDs are the first 12 characters
  28. of the full image ID - which can be found using ``docker inspect`` or
  29. ``docker images -notrunc=true``
  30. .. _dockergroup:
  31. Running an interactive shell
  32. ----------------------------
  33. .. code-block:: bash
  34. # Run an interactive shell in the ubuntu image,
  35. # allocate a tty, attach stdin and stdout
  36. # To detach the tty without exiting the shell,
  37. # use the escape sequence Ctrl-p + Ctrl-q
  38. sudo docker run -i -t ubuntu /bin/bash
  39. Why ``sudo``?
  40. -------------
  41. The ``docker`` daemon always runs as root, and since ``docker``
  42. version 0.5.2, ``docker`` binds to a Unix socket instead of a TCP
  43. port. By default that Unix socket is owned by the user *root*, and so,
  44. by default, you can access it with ``sudo``.
  45. Starting in version 0.5.3, if you create a Unix group called *docker*
  46. and add users to it, then the ``docker`` daemon will make the
  47. ownership of the Unix socket read/writable by the *docker* group when
  48. the daemon starts. The ``docker`` daemon must always run as root, but
  49. if you run the ``docker`` client as a user in the *docker* group then
  50. you don't need to add ``sudo`` to all the client commands.
  51. .. code-block:: bash
  52. # Add the docker group
  53. sudo groupadd docker
  54. # Add the ubuntu user to the docker group
  55. # You may have to logout and log back in again for
  56. # this to take effect
  57. sudo gpasswd -a ubuntu docker
  58. # Restart the docker daemon
  59. sudo service docker restart
  60. .. _bind_docker:
  61. Bind Docker to another host/port or a Unix socket
  62. -------------------------------------------------
  63. .. DANGER:: Changing the default ``docker`` daemon binding to a TCP
  64. port or Unix *docker* user group will increase your security risks
  65. by allowing non-root users to potentially gain *root* access on the
  66. host (`e.g. #1369
  67. <https://github.com/dotcloud/docker/issues/1369>`_). Make sure you
  68. control access to ``docker``.
  69. With -H it is possible to make the Docker daemon to listen on a
  70. specific ip and port. By default, it will listen on
  71. ``unix:///var/run/docker.sock`` to allow only local connections by the
  72. *root* user. You *could* set it to 0.0.0.0:4243 or a specific host ip to
  73. give access to everybody, but that is **not recommended** because then
  74. it is trivial for someone to gain root access to the host where the
  75. daemon is running.
  76. Similarly, the Docker client can use ``-H`` to connect to a custom port.
  77. ``-H`` accepts host and port assignment in the following format:
  78. ``tcp://[host][:port]`` or ``unix://path``
  79. For example:
  80. * ``tcp://host:4243`` -> tcp connection on host:4243
  81. * ``unix://path/to/socket`` -> unix socket located at ``path/to/socket``
  82. .. code-block:: bash
  83. # Run docker in daemon mode
  84. sudo <path to>/docker -H 0.0.0.0:5555 -d &
  85. # Download an ubuntu image
  86. sudo docker -H :5555 pull ubuntu
  87. You can use multiple ``-H``, for example, if you want to listen on
  88. both TCP and a Unix socket
  89. .. code-block:: bash
  90. # Run docker in daemon mode
  91. sudo <path to>/docker -H tcp://127.0.0.1:4243 -H unix:///var/run/docker.sock -d &
  92. # Download an ubuntu image, use default Unix socket
  93. sudo docker pull ubuntu
  94. # OR use the TCP port
  95. sudo docker -H tcp://127.0.0.1:4243 pull ubuntu
  96. Starting a long-running worker process
  97. --------------------------------------
  98. .. code-block:: bash
  99. # Start a very useful long-running process
  100. JOB=$(sudo docker run -d ubuntu /bin/sh -c "while true; do echo Hello world; sleep 1; done")
  101. # Collect the output of the job so far
  102. sudo docker logs $JOB
  103. # Kill the job
  104. sudo docker kill $JOB
  105. Listing all running containers
  106. ------------------------------
  107. .. code-block:: bash
  108. sudo docker ps
  109. Bind a service on a TCP port
  110. ------------------------------
  111. .. code-block:: bash
  112. # Bind port 4444 of this container, and tell netcat to listen on it
  113. JOB=$(sudo docker run -d -p 4444 ubuntu:12.10 /bin/nc -l 4444)
  114. # Which public port is NATed to my container?
  115. PORT=$(sudo docker port $JOB 4444 | awk -F: '{ print $2 }')
  116. # Connect to the public port
  117. echo hello world | nc 127.0.0.1 $PORT
  118. # Verify that the network connection worked
  119. echo "Daemon received: $(sudo docker logs $JOB)"
  120. Committing (saving) a container state
  121. -------------------------------------
  122. Save your containers state to a container image, so the state can be re-used.
  123. When you commit your container only the differences between the image
  124. the container was created from and the current state of the container
  125. will be stored (as a diff). See which images you already have using
  126. ``sudo docker images``
  127. .. code-block:: bash
  128. # Commit your container to a new named image
  129. sudo docker commit <container_id> <some_name>
  130. # List your containers
  131. sudo docker images
  132. You now have a image state from which you can create new instances.
  133. Read more about :ref:`working_with_the_repository` or continue to the
  134. complete :ref:`cli`