basics.rst 6.1 KB

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