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. As we've used the ``--link redis:db`` option, Docker has created some environment
  46. variables in our web application container.
  47. .. code-block:: bash
  48. env | grep DB_
  49. # Should return something similar to this with your values
  50. DB_NAME=/violet_wolf/db
  51. DB_PORT_6379_TCP_PORT=6379
  52. DB_PORT=tcp://172.17.0.33:6379
  53. DB_PORT_6379_TCP=tcp://172.17.0.33:6379
  54. DB_PORT_6379_TCP_ADDR=172.17.0.33
  55. DB_PORT_6379_TCP_PROTO=tcp
  56. We can see that we've got a small list of environment variables prefixed with ``DB``.
  57. The ``DB`` comes from the link alias specified when we launched the container. Let's use
  58. the ``DB_PORT_6379_TCP_ADDR`` variable to connect to our Redis container.
  59. .. code-block:: bash
  60. redis-cli -h $DB_PORT_6379_TCP_ADDR
  61. redis 172.17.0.33:6379>
  62. redis 172.17.0.33:6379> set docker awesome
  63. OK
  64. redis 172.17.0.33:6379> get docker
  65. "awesome"
  66. redis 172.17.0.33:6379> exit
  67. We could easily use this or other environment variables in our web application to make a
  68. connection to our ``redis`` container.