basics.rst 5.4 KB

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