basics.rst 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. Running an interactive shell
  18. ----------------------------
  19. .. code-block:: bash
  20. # Download an ubuntu image
  21. sudo docker pull ubuntu
  22. # Run an interactive shell in the ubuntu image,
  23. # allocate a tty, attach stdin and stdout
  24. sudo docker run -i -t ubuntu /bin/bash
  25. Why ``sudo``?
  26. -------------
  27. The ``docker`` daemon always runs as root, and since ``docker``
  28. version 0.5.2, ``docker`` binds to a Unix socket instead of a TCP
  29. port. By default that Unix socket is owned by the user *root*, and so,
  30. by default, you can access it with ``sudo``.
  31. Starting in version 0.5.3, if you create a Unix group called *docker*
  32. and add users to it, then the ``docker`` daemon will make the
  33. ownership of the Unix socket read/writable by the *docker* group when
  34. the daemon starts. The ``docker`` daemon must always run as root, but
  35. if you run the ``docker`` client as a user in the *docker* group then
  36. you don't need to add ``sudo`` to all the client commands.
  37. Bind Docker to another host/port or a Unix socket
  38. -------------------------------------------------
  39. .. DANGER:: Changing the default ``docker`` daemon binding to a TCP
  40. port or Unix *docker* user group will increase your security risks
  41. by allowing non-root users to potentially gain *root* access on the
  42. host (`e.g. #1369
  43. <https://github.com/dotcloud/docker/issues/1369>`_). Make sure you
  44. control access to ``docker``.
  45. With -H it is possible to make the Docker daemon to listen on a
  46. specific ip and port. By default, it will listen on
  47. ``unix:///var/run/docker.sock`` to allow only local connections by the
  48. *root* user. You *could* set it to 0.0.0.0:4243 or a specific host ip to
  49. give access to everybody, but that is **not recommended** because then
  50. it is trivial for someone to gain root access to the host where the
  51. daemon is running.
  52. Similarly, the Docker client can use ``-H`` to connect to a custom port.
  53. ``-H`` accepts host and port assignment in the following format:
  54. ``tcp://[host][:port]`` or ``unix://path``
  55. For example:
  56. * ``tcp://host:4243`` -> tcp connection on host:4243
  57. * ``unix://path/to/socket`` -> unix socket located at ``path/to/socket``
  58. .. code-block:: bash
  59. # Run docker in daemon mode
  60. sudo <path to>/docker -H 0.0.0.0:5555 -d &
  61. # Download an ubuntu image
  62. sudo docker -H :5555 pull ubuntu
  63. You can use multiple ``-H``, for example, if you want to listen on
  64. both TCP and a Unix socket
  65. .. code-block:: bash
  66. # Run docker in daemon mode
  67. sudo <path to>/docker -H tcp://127.0.0.1:4243 -H unix:///var/run/docker.sock -d &
  68. # Download an ubuntu image, use default Unix socket
  69. sudo docker pull ubuntu
  70. # OR use the TCP port
  71. sudo docker -H tcp://127.0.0.1:4243 pull ubuntu
  72. Starting a long-running worker process
  73. --------------------------------------
  74. .. code-block:: bash
  75. # Start a very useful long-running process
  76. JOB=$(sudo docker run -d ubuntu /bin/sh -c "while true; do echo Hello world; sleep 1; done")
  77. # Collect the output of the job so far
  78. sudo docker logs $JOB
  79. # Kill the job
  80. sudo docker kill $JOB
  81. Listing all running containers
  82. ------------------------------
  83. .. code-block:: bash
  84. sudo docker ps
  85. Expose a service on a TCP port
  86. ------------------------------
  87. .. code-block:: bash
  88. # Expose port 4444 of this container, and tell netcat to listen on it
  89. JOB=$(sudo docker run -d -p 4444 ubuntu /bin/nc -l -p 4444)
  90. # Which public port is NATed to my container?
  91. PORT=$(sudo docker port $JOB 4444)
  92. # Connect to the public port via the host's public address
  93. # Please note that because of how routing works connecting to localhost or 127.0.0.1 $PORT will not work.
  94. # Replace *eth0* according to your local interface name.
  95. IP=$(ip -o -4 addr list eth0 | perl -n -e 'if (m{inet\s([\d\.]+)\/\d+\s}xms) { print $1 }')
  96. echo hello world | nc $IP $PORT
  97. # Verify that the network connection worked
  98. echo "Daemon received: $(sudo docker logs $JOB)"
  99. Committing (saving) a container state
  100. -------------------------------------
  101. Save your containers state to a container image, so the state can be re-used.
  102. When you commit your container only the differences between the image
  103. the container was created from and the current state of the container
  104. will be stored (as a diff). See which images you already have using
  105. ``sudo docker images``
  106. .. code-block:: bash
  107. # Commit your container to a new named image
  108. sudo docker commit <container_id> <some_name>
  109. # List your containers
  110. sudo docker images
  111. You now have a image state from which you can create new instances.
  112. Read more about :ref:`working_with_the_repository` or continue to the
  113. complete :ref:`cli`