basics.rst 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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
  24. --------------------------------
  25. If you want Docker to listen to another port and bind to another ip
  26. use -host and -port on both deamon and client
  27. .. code-block:: bash
  28. # Run docker in daemon mode
  29. sudo <path to>/docker -H 0.0.0.0:5555 &
  30. # Download a base image
  31. docker -H :5555 pull base
  32. Starting a long-running worker process
  33. --------------------------------------
  34. .. code-block:: bash
  35. # Start a very useful long-running process
  36. JOB=$(docker run -d base /bin/sh -c "while true; do echo Hello world; sleep 1; done")
  37. # Collect the output of the job so far
  38. docker logs $JOB
  39. # Kill the job
  40. docker kill $JOB
  41. Listing all running containers
  42. ------------------------------
  43. .. code-block:: bash
  44. docker ps
  45. Expose a service on a TCP port
  46. ------------------------------
  47. .. code-block:: bash
  48. # Expose port 4444 of this container, and tell netcat to listen on it
  49. JOB=$(docker run -d -p 4444 base /bin/nc -l -p 4444)
  50. # Which public port is NATed to my container?
  51. PORT=$(docker port $JOB 4444)
  52. # Connect to the public port via the host's public address
  53. # Please note that because of how routing works connecting to localhost or 127.0.0.1 $PORT will not work.
  54. IP=$(ifconfig eth0 | perl -n -e 'if (m/inet addr:([\d\.]+)/g) { print $1 }')
  55. echo hello world | nc $IP $PORT
  56. # Verify that the network connection worked
  57. echo "Daemon received: $(docker logs $JOB)"
  58. Committing (saving) a container state
  59. -------------------------------------
  60. Save your containers state to a container image, so the state can be re-used.
  61. When you commit your container only the differences between the image the container was created from
  62. and the current state of the container will be stored (as a diff). See which images you already have
  63. using ``docker images``
  64. .. code-block:: bash
  65. # Commit your container to a new named image
  66. docker commit <container_id> <some_name>
  67. # List your containers
  68. docker images
  69. You now have a image state from which you can create new instances.
  70. Read more about :ref:`working_with_the_repository` or continue to the complete :ref:`cli`