summaryrefslogtreecommitdiff
path: root/docs/sources/examples/running_redis_service.rst
blob: c9424867a422b19a3373449c10aefcbe415e45f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
:title: Running a Redis service
:description: Installing and running an redis service
:keywords: docker, example, package installation, networking, redis

.. _running_redis_service:

Redis Service
=============

.. include:: example_header.inc

Very simple, no frills, Redis service attached to a web application using a link.

Create a docker container for Redis
-----------------------------------

Firstly, we create a ``Dockerfile`` for our new Redis image.

.. code-block:: bash

    FROM        ubuntu:12.10
    RUN         apt-get update
    RUN         apt-get -y install redis-server
    EXPOSE      6379
    ENTRYPOINT  ["/usr/bin/redis-server"]

Next we build an image from our ``Dockerfile``. Replace ``<your username>`` 
with your own user name.

.. code-block:: bash

    sudo docker build -t <your username>/redis .

Run the service
---------------

Use the image we've just created and name your container ``redis``.

Running the service with ``-d`` runs the container in detached mode, leaving the
container running in the background.

Importantly, we're not exposing any ports on our container. Instead we're going to 
use a container link to provide access to our Redis database.

.. code-block:: bash

    sudo docker run --name redis -d <your username>/redis

Create your web application container
-------------------------------------

Next we can create a container for our application. We're going to use the ``-link`` 
flag to create a link to the ``redis`` container we've just created with an alias of 
``db``. This will create a secure tunnel to the ``redis`` container and expose the 
Redis instance running inside that container to only this container.

.. code-block:: bash

    sudo docker run --link redis:db -i -t ubuntu:12.10 /bin/bash

Once inside our freshly created container we need to install Redis to get the 
``redis-cli`` binary to test our connection.

.. code-block:: bash

    apt-get update
    apt-get -y install redis-server
    service redis-server stop

As we've used the ``--link redis:db`` option, Docker has created some environment 
variables in our web application container.

.. code-block:: bash

    env | grep DB_
    
    # Should return something similar to this with your values 
    DB_NAME=/violet_wolf/db
    DB_PORT_6379_TCP_PORT=6379
    DB_PORT=tcp://172.17.0.33:6379
    DB_PORT_6379_TCP=tcp://172.17.0.33:6379
    DB_PORT_6379_TCP_ADDR=172.17.0.33
    DB_PORT_6379_TCP_PROTO=tcp

We can see that we've got a small list of environment variables prefixed with ``DB``.
The ``DB`` comes from the link alias specified when we launched the container. Let's use 
the ``DB_PORT_6379_TCP_ADDR`` variable to connect to our Redis container.

.. code-block:: bash

    redis-cli -h $DB_PORT_6379_TCP_ADDR
    redis 172.17.0.33:6379>
    redis 172.17.0.33:6379> set docker awesome
    OK
    redis 172.17.0.33:6379> get docker
    "awesome"
    redis 172.17.0.33:6379> exit

We could easily use this or other environment variables in our web application to make a 
connection to our ``redis`` container.