linking_into_redis.rst 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. :title: Linking to an Redis container
  2. :description: Running redis linked into your web app
  3. :keywords: docker, example, networking, redis, link
  4. .. _linking_redis:
  5. Linking Redis
  6. =============
  7. .. include:: example_header.inc
  8. Building a redis container to link as a child of our web application.
  9. Building the redis container
  10. ----------------------------
  11. We will use a pre-build version of redis from the index under
  12. the name ``crosbymichael/redis``. If you are interested in the
  13. Dockerfile that was used to build this container here it is.
  14. .. code-block:: bash
  15. # Build redis from source
  16. # Make sure you have the redis source code checked out in
  17. # the same directory as this Dockerfile
  18. FROM ubuntu
  19. RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.list
  20. RUN apt-get update
  21. RUN apt-get upgrade -y
  22. RUN apt-get install -y gcc make g++ build-essential libc6-dev tcl
  23. ADD . /redis
  24. RUN (cd /redis && make)
  25. RUN (cd /redis && make test)
  26. RUN mkdir -p /redis-data
  27. VOLUME ["/redis-data"]
  28. EXPOSE 6379
  29. ENTRYPOINT ["/redis/src/redis-server"]
  30. CMD ["--dir", "/redis-data"]
  31. We need to ``EXPOSE`` the default port of 6379 so that our link knows what ports
  32. to connect to our redis container on. If you do not expose any ports for the
  33. image then docker will not be able to establish the link between containers.
  34. Run the redis container
  35. -----------------------
  36. .. code-block:: bash
  37. docker run -d -e PASSWORD=docker crosbymichael/redis --requirepass=docker
  38. This will run our redis container using the default port of 6379 and using
  39. as password to secure our service. Next we will link the redis container to
  40. a new name using ``docker link`` and ``docker ls``.
  41. Linking an existing container
  42. -----------------------------
  43. .. code-block:: bash
  44. docker ls
  45. NAME ID IMAGE
  46. /39588b6a45100ef5b328b2c302ea085624f29e6cbab70f88be04793af02cec89 39588b6a45100ef5b328b2c302ea085624f29e6cbab70f88be04793af02cec89 crosbymichael/redis:latest
  47. Docker will automatically create an initial link with the container's id but
  48. because the is long and not very user friendly we can link the container with
  49. a new name.
  50. .. code-block:: bash
  51. docker link /39588b6a45100ef5b328b2c302ea085624f29e6cbab70f88be04793af02cec89 /redis
  52. docker ls
  53. NAME ID IMAGE
  54. /redis 39588b6a45100ef5b328b2c302ea085624f29e6cbab70f88be04793af02cec89 crosbymichael/redis:latest
  55. /39588b6a45100ef5b328b2c302ea085624f29e6cbab70f88be04793af02cec89 39588b6a45100ef5b328b2c302ea085624f29e6cbab70f88be04793af02cec89 crosbymichael/redis:latest
  56. Now we can reference our running redis service using the friendly name ``/redis``.
  57. We can issue all the commands that you would expect; start, stop, attach, using the new name.
  58. Linking redis as a child
  59. ------------------------
  60. Next we can start a new web application that has a dependency on redis and apply a link
  61. to connect both containers. If you noticed when running our redis service we did not use
  62. the ``-p`` option to publish the redis port to the host system. Redis exposed port 6379
  63. but we did not publish the port. This allows docker to prevent all network traffic to
  64. the redis container except when explicitly specified within a link. This is a big win
  65. for security.
  66. Now lets start our web application with a link into redis.
  67. .. code-block:: bash
  68. docker run -t -i -link /redis:db ubuntu bash
  69. root@4c01db0b339c:/# env
  70. HOSTNAME=4c01db0b339c
  71. DB_NAME=/4c01db0b339cf19958731255a796ee072040a652f51652a4ade190ab8c27006f/db
  72. TERM=xterm
  73. DB_PORT=tcp://172.17.0.8:6379
  74. DB_PORT_6379_TCP=tcp://172.17.0.8:6379
  75. PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
  76. PWD=/
  77. DB_ENV_PASSWORD=dockerpass
  78. SHLVL=1
  79. HOME=/
  80. container=lxc
  81. _=/usr/bin/env
  82. root@4c01db0b339c:/#
  83. When we inspect the environment of the linked container we can see a few extra environment
  84. variables have been added. When you specified ``-link /redis:db`` you are telling docker
  85. to link the container named ``/redis`` into this new container with the alias ``db``.
  86. Environment variables are prefixed with the alias so that the parent container can access
  87. network and environment information from the child.
  88. .. code-block:: bash
  89. # The name of the child container
  90. DB_NAME=/4c01db0b339cf19958731255a796ee072040a652f51652a4ade190ab8c27006f/db
  91. # The default protocol, ip, and port of the service running in the container
  92. DB_PORT=tcp://172.17.0.8:6379
  93. # A specific protocol, ip, and port of various services
  94. DB_PORT_6379_TCP=tcp://172.17.0.8:6379
  95. # Get environment variables of the container
  96. DB_ENV_PASSWORD=dockerpass
  97. Accessing the network information along with the environment of the child container allows
  98. us to easily connect to the redis service on the specific ip and port and use the password
  99. specified in the environment.