running_redis_service.rst 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. :title: Running a Redis service
  2. :description: Installing and running an redis service
  3. :keywords: docker, example, package installation, networking, redis
  4. .. _running_redis_service:
  5. Redis Service
  6. =============
  7. .. include:: example_header.inc
  8. Very simple, no frills, Redis service attached to a web application using a link.
  9. Create a docker container for Redis
  10. -----------------------------------
  11. Firstly, we create a ``Dockerfile`` for our new Redis image.
  12. .. code-block:: bash
  13. FROM ubuntu:12.10
  14. RUN apt-get update
  15. RUN apt-get -y install redis-server
  16. EXPOSE 6379
  17. ENTRYPOINT ["/usr/bin/redis-server"]
  18. Next we build an image from our ``Dockerfile``. Replace ``<your username>``
  19. with your own user name.
  20. .. code-block:: bash
  21. sudo docker build -t <your username>/redis .
  22. Run the service
  23. ---------------
  24. Use the image we've just created and name your container ``redis``.
  25. Running the service with ``-d`` runs the container in detached mode, leaving the
  26. container running in the background.
  27. Importantly, we're not exposing any ports on our container. Instead we're going to
  28. use a container link to provide access to our Redis database.
  29. .. code-block:: bash
  30. sudo docker run -name redis -d <your username>/redis
  31. Create your web application container
  32. -------------------------------------
  33. Next we can create a container for our application. We're going to use the ``-link``
  34. flag to create a link to the ``redis`` container we've just created with an alias of
  35. ``db``. This will create a secure tunnel to the ``redis`` container and expose the
  36. Redis instance running inside that container to only this container.
  37. .. code-block:: bash
  38. sudo docker run -link redis:db -i -t ubuntu:12.10 /bin/bash
  39. Once inside our freshly created container we need to install Redis to get the
  40. ``redis-cli`` binary to test our connection.
  41. .. code-block:: bash
  42. apt-get update
  43. apt-get -y install redis-server
  44. service redis-server stop
  45. Now we can test the connection. Firstly, let's look at the available environmental
  46. variables in our web application container. We can use these to get the IP and port
  47. of our ``redis`` container.
  48. .. code-block:: bash
  49. env
  50. . . .
  51. DB_NAME=/violet_wolf/db
  52. DB_PORT_6379_TCP_PORT=6379
  53. DB_PORT=tcp://172.17.0.33:6379
  54. DB_PORT_6379_TCP=tcp://172.17.0.33:6379
  55. DB_PORT_6379_TCP_ADDR=172.17.0.33
  56. DB_PORT_6379_TCP_PROTO=tcp
  57. We can see that we've got a small list of environment variables prefixed with ``DB``.
  58. The ``DB`` comes from the link alias specified when we launched the container. Let's use
  59. the ``DB_PORT_6379_TCP_ADDR`` variable to connect to our Redis container.
  60. .. code-block:: bash
  61. redis-cli -h $DB_PORT_6379_TCP_ADDR
  62. redis 172.17.0.33:6379>
  63. redis 172.17.0.33:6379> set docker awesome
  64. OK
  65. redis 172.17.0.33:6379> get docker
  66. "awesome"
  67. redis 172.17.0.33:6379> exit
  68. We could easily use this or other environment variables in our web application to make a
  69. connection to our ``redis`` container.