working_with_links_names.rst 4.8 KB

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