basics.rst 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 installed with upstart, Ubuntu's
  9. system for starting processes at boot time. You should be able to run ``docker help`` and get output.
  10. If you get ``docker: command not found`` or something like ``/var/lib/docker/repositories: permission denied``
  11. you will need to specify the path to it and manually start it.
  12. .. code-block:: bash
  13. # Run docker in daemon mode
  14. sudo <path to>/docker -d &
  15. Running an interactive shell
  16. ----------------------------
  17. .. code-block:: bash
  18. # Download a base image
  19. docker pull base
  20. # Run an interactive shell in the base image,
  21. # allocate a tty, attach stdin and stdout
  22. docker run -i -t base /bin/bash
  23. Bind Docker to another host/port or a unix socket
  24. -------------------------------------------------
  25. With -H it is possible to make the Docker daemon to listen on a specific ip and port. By default, it will listen on 127.0.0.1:4243 to allow only local connections but you can set it to 0.0.0.0:4243 or a specific host ip to give access to everybody.
  26. Similarly, the Docker client can use -H to connect to a custom port.
  27. -H accepts host and port assignment in the following format: tcp://[host][:port] or unix://path
  28. For example:
  29. * tcp://host -> tcp connection on host:4243
  30. * tcp://host:port -> tcp connection on host:port
  31. * tcp://:port -> tcp connection on 127.0.0.1:port
  32. * unix://path/to/socket -> unix socket located at path/to/socket
  33. .. code-block:: bash
  34. # Run docker in daemon mode
  35. sudo <path to>/docker -H 0.0.0.0:5555 -d &
  36. # Download a base image
  37. docker -H :5555 pull base
  38. You can use multiple -H, for example, if you want to listen
  39. on both tcp and a unix socket
  40. .. code-block:: bash
  41. # Run docker in daemon mode
  42. sudo <path to>/docker -H tcp://127.0.0.1:4243 -H unix:///var/run/docker.sock -d &
  43. # Download a base image
  44. docker pull base
  45. # OR
  46. docker -H unix:///var/run/docker.sock pull base
  47. Starting a long-running worker process
  48. --------------------------------------
  49. .. code-block:: bash
  50. # Start a very useful long-running process
  51. JOB=$(docker run -d base /bin/sh -c "while true; do echo Hello world; sleep 1; done")
  52. # Collect the output of the job so far
  53. docker logs $JOB
  54. # Kill the job
  55. docker kill $JOB
  56. Listing all running containers
  57. ------------------------------
  58. .. code-block:: bash
  59. docker ps
  60. Expose a service on a TCP port
  61. ------------------------------
  62. .. code-block:: bash
  63. # Expose port 4444 of this container, and tell netcat to listen on it
  64. JOB=$(docker run -d -p 4444 base /bin/nc -l -p 4444)
  65. # Which public port is NATed to my container?
  66. PORT=$(docker port $JOB 4444)
  67. # Connect to the public port via the host's public address
  68. # Please note that because of how routing works connecting to localhost or 127.0.0.1 $PORT will not work.
  69. # Replace *eth0* according to your local interface name.
  70. IP=$(ip -o -4 addr list eth0 | perl -n -e 'if (m{inet\s([\d\.]+)\/\d+\s}xms) { print $1 }')
  71. echo hello world | nc $IP $PORT
  72. # Verify that the network connection worked
  73. echo "Daemon received: $(docker logs $JOB)"
  74. Committing (saving) a container state
  75. -------------------------------------
  76. Save your containers state to a container image, so the state can be re-used.
  77. When you commit your container only the differences between the image the container was created from
  78. and the current state of the container will be stored (as a diff). See which images you already have
  79. using ``docker images``
  80. .. code-block:: bash
  81. # Commit your container to a new named image
  82. docker commit <container_id> <some_name>
  83. # List your containers
  84. docker images
  85. You now have a image state from which you can create new instances.
  86. Read more about :ref:`working_with_the_repository` or continue to the complete :ref:`cli`