basics.rst 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. The sudo command and the docker Group
  40. -------------------------------------
  41. The ``docker`` daemon always runs as the root user, and since Docker version
  42. 0.5.2, the ``docker`` daemon binds to a Unix socket instead of a TCP port. By
  43. default that Unix socket is owned by the user *root*, and so, by default, you
  44. 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 the root user, 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.
  52. .. warning:: The *docker* group is root-equivalent.
  53. **Example:**
  54. .. code-block:: bash
  55. # Add the docker group if it doesn't already exist.
  56. sudo groupadd docker
  57. # Add the connected user "${USER}" to the docker group.
  58. # Change the user name to match your preferred user.
  59. # You may have to logout and log back in again for
  60. # this to take effect.
  61. sudo gpasswd -a ${USER} docker
  62. # Restart the docker daemon.
  63. sudo service docker restart
  64. .. _bind_docker:
  65. Bind Docker to another host/port or a Unix socket
  66. -------------------------------------------------
  67. .. warning:: Changing the default ``docker`` daemon binding to a TCP
  68. port or Unix *docker* user group will increase your security risks
  69. by allowing non-root users to potentially gain *root* access on the
  70. host (`e.g. #1369
  71. <https://github.com/dotcloud/docker/issues/1369>`_). Make sure you
  72. control access to ``docker``.
  73. With ``-H`` it is possible to make the Docker daemon to listen on a
  74. specific IP and port. By default, it will listen on
  75. ``unix:///var/run/docker.sock`` to allow only local connections by the
  76. *root* user. You *could* set it to ``0.0.0.0:4243`` or a specific host IP to
  77. give access to everybody, but that is **not recommended** because then
  78. it is trivial for someone to gain root access to the host where the
  79. daemon is running.
  80. Similarly, the Docker client can use ``-H`` to connect to a custom port.
  81. ``-H`` accepts host and port assignment in the following format:
  82. ``tcp://[host][:port]`` or ``unix://path``
  83. For example:
  84. * ``tcp://host:4243`` -> tcp connection on host:4243
  85. * ``unix://path/to/socket`` -> unix socket located at ``path/to/socket``
  86. ``-H``, when empty, will default to the same value as when no ``-H`` was passed in.
  87. ``-H`` also accepts short form for TCP bindings:
  88. ``host[:port]`` or ``:port``
  89. .. code-block:: bash
  90. # Run docker in daemon mode
  91. sudo <path to>/docker -H 0.0.0.0:5555 -d &
  92. # Download an ubuntu image
  93. sudo docker -H :5555 pull ubuntu
  94. You can use multiple ``-H``, for example, if you want to listen on
  95. both TCP and a Unix socket
  96. .. code-block:: bash
  97. # Run docker in daemon mode
  98. sudo <path to>/docker -H tcp://127.0.0.1:4243 -H unix:///var/run/docker.sock -d &
  99. # Download an ubuntu image, use default Unix socket
  100. sudo docker pull ubuntu
  101. # OR use the TCP port
  102. sudo docker -H tcp://127.0.0.1:4243 pull ubuntu
  103. Starting a long-running worker process
  104. --------------------------------------
  105. .. code-block:: bash
  106. # Start a very useful long-running process
  107. JOB=$(sudo docker run -d ubuntu /bin/sh -c "while true; do echo Hello world; sleep 1; done")
  108. # Collect the output of the job so far
  109. sudo docker logs $JOB
  110. # Kill the job
  111. sudo docker kill $JOB
  112. Listing all running containers
  113. ------------------------------
  114. .. code-block:: bash
  115. sudo docker ps
  116. Bind a service on a TCP port
  117. ------------------------------
  118. .. code-block:: bash
  119. # Bind port 4444 of this container, and tell netcat to listen on it
  120. JOB=$(sudo docker run -d -p 4444 ubuntu:12.10 /bin/nc -l 4444)
  121. # Which public port is NATed to my container?
  122. PORT=$(sudo docker port $JOB 4444 | awk -F: '{ print $2 }')
  123. # Connect to the public port
  124. echo hello world | nc 127.0.0.1 $PORT
  125. # Verify that the network connection worked
  126. echo "Daemon received: $(sudo docker logs $JOB)"
  127. Committing (saving) a container state
  128. -------------------------------------
  129. Save your containers state to a container image, so the state can be re-used.
  130. When you commit your container only the differences between the image the
  131. container was created from and the current state of the container will be
  132. stored (as a diff). See which images you already have using the ``docker
  133. images`` command.
  134. .. code-block:: bash
  135. # Commit your container to a new named image
  136. sudo docker commit <container_id> <some_name>
  137. # List your containers
  138. sudo docker images
  139. You now have a image state from which you can create new instances.
  140. Read more about :ref:`working_with_the_repository` or continue to the
  141. complete :ref:`cli`