linking_into_redis.rst 4.2 KB

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