working_with_links_names.rst 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. :title: Working with Links and Names
  2. :description: How to create and use links and names
  3. :keywords: Examples, Usage, links, docker, documentation, examples, names, name, container naming
  4. .. _working_with_links_names:
  5. Working with Links and Names
  6. ============================
  7. From version 0.6.5 you are now able to ``name`` a container and ``link`` it to another
  8. container by referring to its name. This will create a parent -> child relationship
  9. where the parent container can see selected information about its child.
  10. .. _run_name:
  11. Container Naming
  12. ----------------
  13. .. versionadded:: v0.6.5
  14. You can now name your container by using the ``-name`` flag. If no name is provided, Docker
  15. will automatically generate a name. You can see this name using the ``docker ps`` command.
  16. .. code-block:: bash
  17. # format is "sudo docker run -name <container_name> <image_name> <command>"
  18. $ sudo docker run -name test ubuntu /bin/bash
  19. # the flag "-a" Show all containers. Only running containers are shown by default.
  20. $ sudo docker ps -a
  21. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  22. 2522602a0d99 ubuntu:12.04 /bin/bash 14 seconds ago Exit 0 test
  23. .. _run_link:
  24. Links: service discovery for docker
  25. -----------------------------------
  26. .. versionadded:: v0.6.5
  27. Links allow containers to discover and securely communicate with each other by using the
  28. flag ``-link name:alias``. Inter-container communication can be disabled with the daemon
  29. flag ``-icc=false``. With this flag set to false, Container A cannot access Container B
  30. unless explicitly allowed via a link. This is a huge win for securing your containers.
  31. When two containers are linked together Docker creates a parent child relationship
  32. between the containers. The parent container will be able to access information via
  33. environment variables of the child such as name, exposed ports, IP and other selected
  34. environment variables.
  35. When linking two containers Docker will use the exposed ports of the container to create
  36. a secure tunnel for the parent to access. If a database container only exposes port 8080
  37. then the linked container will only be allowed to access port 8080 and nothing else if
  38. inter-container communication is set to false.
  39. .. code-block:: bash
  40. # Example: there is an image called redis-2.6 that exposes the port 6379 and starts redis-server.
  41. # Let's name the container as "redis" based on that image and run it as daemon.
  42. $ sudo docker run -d -name redis redis-2.6
  43. We can issue all the commands that you would expect using the name "redis"; start, stop,
  44. attach, using the name for our container. The name also allows us to link other containers
  45. into this one.
  46. Next, we can start a new web application that has a dependency on Redis and apply a link
  47. to connect both containers. If you noticed when running our Redis server we did not use
  48. the -p flag to publish the Redis port to the host system. Redis exposed port 6379 and
  49. this is all we need to establish a link.
  50. .. code-block:: bash
  51. # Linking the redis container as a child
  52. $ sudo docker run -t -i -link redis:db -name webapp ubuntu bash
  53. When you specified -link redis:db you are telling docker to link the container named redis
  54. into this new container with the alias db. Environment variables are prefixed with the alias
  55. so that the parent container can access network and environment information from the containers
  56. that are linked into it.
  57. If we inspect the environment variables of the second container, we would see all the information
  58. about the child container.
  59. .. code-block:: bash
  60. $ root@4c01db0b339c:/# env
  61. HOSTNAME=4c01db0b339c
  62. DB_NAME=/webapp/db
  63. TERM=xterm
  64. DB_PORT=tcp://172.17.0.8:6379
  65. DB_PORT_6379_TCP=tcp://172.17.0.8:6379
  66. DB_PORT_6379_TCP_PROTO=tcp
  67. DB_PORT_6379_TCP_ADDR=172.17.0.8
  68. DB_PORT_6379_TCP_PORT=6379
  69. PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
  70. PWD=/
  71. SHLVL=1
  72. HOME=/
  73. container=lxc
  74. _=/usr/bin/env
  75. root@4c01db0b339c:/#
  76. Accessing the network information along with the environment of the child container allows
  77. us to easily connect to the Redis service on the specific IP and port in the environment.