basics.rst 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 --no-trunc=true``
  30. **If you're using OS X** then you shouldn't use ``sudo``
  31. Running an interactive shell
  32. ----------------------------
  33. .. code-block:: bash
  34. # Run an interactive shell in the ubuntu image,
  35. # allocate a tty, attach stdin and stdout
  36. # To detach the tty without exiting the shell,
  37. # use the escape sequence Ctrl-p + Ctrl-q
  38. # note: This will continue to exist in a stopped state once exited (see "docker ps -a")
  39. sudo docker run -i -t ubuntu /bin/bash
  40. .. _bind_docker:
  41. Bind Docker to another host/port or a Unix socket
  42. -------------------------------------------------
  43. .. warning:: Changing the default ``docker`` daemon binding to a TCP
  44. port or Unix *docker* user group will increase your security risks
  45. by allowing non-root users to gain *root* access on the
  46. host. Make sure you control access to ``docker``. If you are binding
  47. to a TCP port, anyone with access to that port has full Docker access;
  48. so it is not advisable on an open network.
  49. With ``-H`` it is possible to make the Docker daemon to listen on a
  50. specific IP and port. By default, it will listen on
  51. ``unix:///var/run/docker.sock`` to allow only local connections by the
  52. *root* user. You *could* set it to ``0.0.0.0:4243`` or a specific host IP to
  53. give access to everybody, but that is **not recommended** because then
  54. it is trivial for someone to gain root access to the host where the
  55. daemon is running.
  56. Similarly, the Docker client can use ``-H`` to connect to a custom port.
  57. ``-H`` accepts host and port assignment in the following format:
  58. ``tcp://[host][:port]`` or ``unix://path``
  59. For example:
  60. * ``tcp://host:4243`` -> tcp connection on host:4243
  61. * ``unix://path/to/socket`` -> unix socket located at ``path/to/socket``
  62. ``-H``, when empty, will default to the same value as when no ``-H`` was passed in.
  63. ``-H`` also accepts short form for TCP bindings:
  64. ``host[:port]`` or ``:port``
  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 containers
  89. ------------------
  90. .. code-block:: bash
  91. sudo docker ps # Lists only running containers
  92. sudo docker ps -a # Lists all containers
  93. Controlling containers
  94. ----------------------
  95. .. code-block:: bash
  96. # Start a new container
  97. JOB=$(sudo docker run -d ubuntu /bin/sh -c "while true; do echo Hello world; sleep 1; done")
  98. # Stop the container
  99. docker stop $JOB
  100. # Start the container
  101. docker start $JOB
  102. # Restart the container
  103. docker restart $JOB
  104. # SIGKILL a container
  105. docker kill $JOB
  106. # Remove a container
  107. docker stop $JOB # Container must be stopped to remove it
  108. docker rm $JOB
  109. Bind a service on a TCP port
  110. ------------------------------
  111. .. code-block:: bash
  112. # Bind port 4444 of this container, and tell netcat to listen on it
  113. JOB=$(sudo docker run -d -p 4444 ubuntu:12.10 /bin/nc -l 4444)
  114. # Which public port is NATed to my container?
  115. PORT=$(sudo docker port $JOB 4444 | awk -F: '{ print $2 }')
  116. # Connect to the public port
  117. echo hello world | nc 127.0.0.1 $PORT
  118. # Verify that the network connection worked
  119. echo "Daemon received: $(sudo docker logs $JOB)"
  120. Committing (saving) a container state
  121. -------------------------------------
  122. Save your containers state to a container image, so the state can be re-used.
  123. When you commit your container only the differences between the image the
  124. container was created from and the current state of the container will be
  125. stored (as a diff). See which images you already have using the ``docker
  126. images`` command.
  127. .. code-block:: bash
  128. # Commit your container to a new named image
  129. sudo docker commit <container_id> <some_name>
  130. # List your containers
  131. sudo docker images
  132. You now have a image state from which you can create new instances.
  133. Read more about :ref:`working_with_the_repository` or continue to the
  134. complete :ref:`cli`