basics.rst 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. :title: First steps with Docker
  2. :description: Common usage and commands
  3. :keywords: Examples, Usage, basic commands, docker, documentation, examples
  4. First steps with Docker
  5. =======================
  6. Check your Docker install
  7. -------------------------
  8. This guide assumes you have a working installation of Docker. To check
  9. your Docker install, run the following command:
  10. .. code-block:: bash
  11. # Check that you have a working install
  12. docker info
  13. If you get ``docker: command not found`` or something like
  14. ``/var/lib/docker/repositories: permission denied`` you may have an incomplete
  15. docker installation or insufficient privileges to access Docker on your machine.
  16. Please refer to :ref:`installation_list` for installation instructions.
  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. # note: This will continue to exist in a stopped state once exited (see "docker ps -a")
  38. sudo docker run -i -t ubuntu /bin/bash
  39. .. _bind_docker:
  40. Bind Docker to another host/port or a Unix socket
  41. -------------------------------------------------
  42. .. warning:: Changing the default ``docker`` daemon binding to a TCP
  43. port or Unix *docker* user group will increase your security risks
  44. by allowing non-root users to gain *root* access on the
  45. host. Make sure you control access to ``docker``. If you are binding
  46. to a TCP port, anyone with access to that port has full Docker access;
  47. so it is not advisable on an open network.
  48. With ``-H`` it is possible to make the Docker daemon to listen on a
  49. specific IP and port. By default, it will listen on
  50. ``unix:///var/run/docker.sock`` to allow only local connections by the
  51. *root* user. You *could* set it to ``0.0.0.0:4243`` or a specific host IP to
  52. give access to everybody, but that is **not recommended** because then
  53. it is trivial for someone to gain root access to the host where the
  54. daemon is running.
  55. Similarly, the Docker client can use ``-H`` to connect to a custom port.
  56. ``-H`` accepts host and port assignment in the following format:
  57. ``tcp://[host][:port]`` or ``unix://path``
  58. For example:
  59. * ``tcp://host:4243`` -> tcp connection on host:4243
  60. * ``unix://path/to/socket`` -> unix socket located at ``path/to/socket``
  61. ``-H``, when empty, will default to the same value as when no ``-H`` was passed in.
  62. ``-H`` also accepts short form for TCP bindings:
  63. ``host[:port]`` or ``:port``
  64. .. code-block:: bash
  65. # Run docker in daemon mode
  66. sudo <path to>/docker -H 0.0.0.0:5555 -d &
  67. # Download an ubuntu image
  68. sudo docker -H :5555 pull ubuntu
  69. You can use multiple ``-H``, for example, if you want to listen on
  70. both TCP and a Unix socket
  71. .. code-block:: bash
  72. # Run docker in daemon mode
  73. sudo <path to>/docker -H tcp://127.0.0.1:4243 -H unix:///var/run/docker.sock -d &
  74. # Download an ubuntu image, use default Unix socket
  75. sudo docker pull ubuntu
  76. # OR use the TCP port
  77. sudo docker -H tcp://127.0.0.1:4243 pull ubuntu
  78. Starting a long-running worker process
  79. --------------------------------------
  80. .. code-block:: bash
  81. # Start a very useful long-running process
  82. JOB=$(sudo docker run -d ubuntu /bin/sh -c "while true; do echo Hello world; sleep 1; done")
  83. # Collect the output of the job so far
  84. sudo docker logs $JOB
  85. # Kill the job
  86. sudo docker kill $JOB
  87. Listing containers
  88. ------------------
  89. .. code-block:: bash
  90. sudo docker ps # Lists only running containers
  91. sudo docker ps -a # Lists all containers
  92. Controlling containers
  93. ----------------------
  94. .. code-block:: bash
  95. # Start a new container
  96. JOB=$(sudo docker run -d ubuntu /bin/sh -c "while true; do echo Hello world; sleep 1; done")
  97. # Stop the container
  98. docker stop $JOB
  99. # Start the container
  100. docker start $JOB
  101. # Restart the container
  102. docker restart $JOB
  103. # SIGKILL a container
  104. docker kill $JOB
  105. # Remove a container
  106. docker stop $JOB # Container must be stopped to remove it
  107. docker rm $JOB
  108. Bind a service on a TCP port
  109. ------------------------------
  110. .. code-block:: bash
  111. # Bind port 4444 of this container, and tell netcat to listen on it
  112. JOB=$(sudo docker run -d -p 4444 ubuntu:12.10 /bin/nc -l 4444)
  113. # Which public port is NATed to my container?
  114. PORT=$(sudo docker port $JOB 4444 | awk -F: '{ print $2 }')
  115. # Connect to the public port
  116. echo hello world | nc 127.0.0.1 $PORT
  117. # Verify that the network connection worked
  118. echo "Daemon received: $(sudo docker logs $JOB)"
  119. Committing (saving) a container state
  120. -------------------------------------
  121. Save your containers state to a container image, so the state can be re-used.
  122. When you commit your container only the differences between the image the
  123. container was created from and the current state of the container will be
  124. stored (as a diff). See which images you already have using the ``docker
  125. images`` command.
  126. .. code-block:: bash
  127. # Commit your container to a new named image
  128. sudo docker commit <container_id> <some_name>
  129. # List your containers
  130. sudo docker images
  131. You now have a image state from which you can create new instances.
  132. Read more about :ref:`working_with_the_repository` or continue to the
  133. complete :ref:`cli`