basics.rst 5.5 KB

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