linking_into_redis.rst 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 -name redis crosbymichael/redis --requirepass=docker
  38. This will run our redis container using the default port of 6379 and using docker
  39. as password to secure our service. By specifying the ``-name`` flag on run
  40. we will assign the name ``redis`` to this container.
  41. We can issue all the commands that you would expect; start, stop, attach, using the name.
  42. The name also allows us to link other containers into this one. If you do not specify a
  43. name on docker run, docker will automatically generate a name for your container.
  44. Linking redis as a child
  45. ------------------------
  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 service we did not use
  48. the ``-p`` option to publish the redis port to the host system. Redis exposed port 6379
  49. but we did not publish the port. This allows docker to prevent all network traffic to
  50. the redis container except when explicitly specified within a link. This is a big win
  51. for security.
  52. Now lets start our web application with a link into redis.
  53. .. code-block:: bash
  54. docker run -t -i -link /redis:db -name webapp ubuntu bash
  55. root@4c01db0b339c:/# env
  56. HOSTNAME=4c01db0b339c
  57. DB_NAME=/webapp/db
  58. TERM=xterm
  59. DB_PORT=tcp://172.17.0.8:6379
  60. DB_PORT_6379_TCP=tcp://172.17.0.8:6379
  61. DB_PORT_6379_TCP_PROTO=tcp
  62. DB_PORT_6379_TCP_ADDR=172.17.0.8
  63. DB_PORT_6379_TCP_PORT=6379
  64. PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
  65. PWD=/
  66. DB_ENV_PASSWORD=dockerpass
  67. SHLVL=1
  68. HOME=/
  69. container=lxc
  70. _=/usr/bin/env
  71. root@4c01db0b339c:/#
  72. When we inspect the environment of the linked container we can see a few extra environment
  73. variables have been added. When you specified ``-link /redis:db`` you are telling docker
  74. to link the container named ``/redis`` into this new container with the alias ``db``.
  75. Environment variables are prefixed with the alias so that the parent container can access
  76. network and environment information from the child.
  77. .. code-block:: bash
  78. # The name of the child container
  79. DB_NAME=/webapp/db
  80. # The default protocol, ip, and port of the service running in the container
  81. DB_PORT=tcp://172.17.0.8:6379
  82. # A specific protocol, ip, and port of various services
  83. DB_PORT_6379_TCP=tcp://172.17.0.8:6379
  84. DB_PORT_6379_TCP_PROTO=tcp
  85. DB_PORT_6379_TCP_ADDR=172.17.0.8
  86. DB_PORT_6379_TCP_PORT=6379
  87. # Get environment variables of the container
  88. DB_ENV_PASSWORD=dockerpass
  89. Accessing the network information along with the environment of the child container allows
  90. us to easily connect to the redis service on the specific ip and port and use the password
  91. specified in the environment.