linking_into_redis.rst 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. Lets build a redis image with the following Dockerfile.
  12. .. code-block:: bash
  13. git clone https://github.com/antirez/redis.git
  14. cd redis
  15. git checkout 2.6
  16. # Save this Dockerfile to the root of the redis repository.
  17. # Build redis from source
  18. # Make sure you have the redis source code checked out in
  19. # the same directory as this Dockerfile
  20. FROM ubuntu
  21. RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.list
  22. RUN apt-get update
  23. RUN apt-get upgrade -y
  24. RUN apt-get install -y gcc make g++ build-essential libc6-dev tcl
  25. ADD . /redis
  26. RUN (cd /redis && make)
  27. RUN mkdir -p /redis-data
  28. VOLUME ["/redis-data"]
  29. EXPOSE 6379
  30. ENTRYPOINT ["/redis/src/redis-server"]
  31. CMD ["--dir", "/redis-data"]
  32. # docker build our new redis image from source
  33. docker build -t redis-2.6 .
  34. We need to ``EXPOSE`` the default port of 6379 so that our link knows what ports
  35. to connect to our redis container on. If you do not expose any ports for the
  36. image then docker will not be able to establish the link between containers.
  37. Run the redis container
  38. -----------------------
  39. .. code-block:: bash
  40. docker run -d -e PASSWORD=docker -name redis redis-2.6 --requirepass docker
  41. This will run our redis container wit the password docker
  42. to secure our service. By specifying the ``-name`` flag on run
  43. we will assign the name ``redis`` to this container. If we do not specify a name for
  44. our container via the ``-name`` flag docker will automatically generate a name for us.
  45. We can issue all the commands that you would expect; start, stop, attach, using the name for our container.
  46. The name also allows us to link other containers into this one.
  47. Linking redis as a child
  48. ------------------------
  49. Next we can start a new web application that has a dependency on redis and apply a link
  50. to connect both containers. If you noticed when running our redis server we did not use
  51. the ``-p`` flag to publish the redis port to the host system. Redis exposed port 6379 via the Dockerfile
  52. and this is all we need to establish a link.
  53. Now lets start our web application with a link into redis.
  54. .. code-block:: bash
  55. docker run -t -i -link redis:db -name webapp ubuntu bash
  56. root@4c01db0b339c:/# env
  57. HOSTNAME=4c01db0b339c
  58. DB_NAME=/webapp/db
  59. TERM=xterm
  60. DB_PORT=tcp://172.17.0.8:6379
  61. DB_PORT_6379_TCP=tcp://172.17.0.8:6379
  62. DB_PORT_6379_TCP_PROTO=tcp
  63. DB_PORT_6379_TCP_ADDR=172.17.0.8
  64. DB_PORT_6379_TCP_PORT=6379
  65. PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
  66. PWD=/
  67. DB_ENV_PASSWORD=docker
  68. SHLVL=1
  69. HOME=/
  70. container=lxc
  71. _=/usr/bin/env
  72. root@4c01db0b339c:/#
  73. When we inspect the environment of the linked container we can see a few extra environment
  74. variables have been added. When you specified ``-link redis:db`` you are telling docker
  75. to link the container named ``redis`` into this new container with the alias ``db``.
  76. Environment variables are prefixed with the alias so that the parent container can access
  77. network and environment information from the containers that are linked into it.
  78. .. code-block:: bash
  79. # The name of the child container
  80. DB_NAME=/webapp/db
  81. # The default protocol, ip, and port of the service running in the container
  82. DB_PORT=tcp://172.17.0.8:6379
  83. # A specific protocol, ip, and port of various services
  84. DB_PORT_6379_TCP=tcp://172.17.0.8:6379
  85. DB_PORT_6379_TCP_PROTO=tcp
  86. DB_PORT_6379_TCP_ADDR=172.17.0.8
  87. DB_PORT_6379_TCP_PORT=6379
  88. # Get environment variables of the container
  89. DB_ENV_PASSWORD=docker
  90. Accessing the network information along with the environment of the child container allows
  91. us to easily connect to the redis service on the specific ip and port and use the password
  92. specified in the environment.