2013-04-09 11:04:14 -04:00
|
|
|
:title: Running a Redis service
|
|
|
|
:description: Installing and running an redis service
|
|
|
|
:keywords: docker, example, package installation, networking, redis
|
|
|
|
|
|
|
|
.. _running_redis_service:
|
|
|
|
|
2013-06-02 01:03:28 -04:00
|
|
|
Redis Service
|
|
|
|
=============
|
2013-04-09 11:04:14 -04:00
|
|
|
|
2013-04-09 17:50:12 -04:00
|
|
|
.. include:: example_header.inc
|
2013-04-09 11:04:14 -04:00
|
|
|
|
2013-11-04 20:18:37 -05:00
|
|
|
Very simple, no frills, Redis service attached to a web application using a link.
|
2013-04-09 11:04:14 -04:00
|
|
|
|
2013-11-04 20:18:37 -05:00
|
|
|
Create a docker container for Redis
|
|
|
|
-----------------------------------
|
2013-04-09 11:04:14 -04:00
|
|
|
|
2013-11-04 20:18:37 -05:00
|
|
|
Firstly, we create a ``Dockerfile`` for our new Redis image.
|
2013-04-09 11:04:14 -04:00
|
|
|
|
2013-04-09 17:50:12 -04:00
|
|
|
.. code-block:: bash
|
2013-04-09 11:04:14 -04:00
|
|
|
|
2013-11-04 20:18:37 -05:00
|
|
|
FROM ubuntu:12.10
|
|
|
|
RUN apt-get update
|
|
|
|
RUN apt-get -y install redis-server
|
|
|
|
EXPOSE 6379
|
|
|
|
ENTRYPOINT ["/usr/bin/redis-server"]
|
2013-04-09 11:04:14 -04:00
|
|
|
|
2013-11-04 20:18:37 -05:00
|
|
|
Next we build an image from our ``Dockerfile``. Replace ``<your username>``
|
|
|
|
with your own user name.
|
2013-04-09 11:04:14 -04:00
|
|
|
|
2013-04-09 17:50:12 -04:00
|
|
|
.. code-block:: bash
|
2013-04-09 11:04:14 -04:00
|
|
|
|
2013-11-15 10:16:59 -05:00
|
|
|
sudo docker build -t <your username>/redis .
|
2013-04-09 11:04:14 -04:00
|
|
|
|
|
|
|
Run the service
|
|
|
|
---------------
|
|
|
|
|
2013-11-04 20:18:37 -05:00
|
|
|
Use the image we've just created and name your container ``redis``.
|
|
|
|
|
2013-11-02 21:26:52 -04:00
|
|
|
Running the service with ``-d`` runs the container in detached mode, leaving the
|
2013-11-04 20:18:37 -05:00
|
|
|
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.
|
2013-04-09 17:50:12 -04:00
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
2013-11-04 20:18:37 -05:00
|
|
|
sudo docker run -name redis -d <your username>/redis
|
2013-04-09 17:50:12 -04:00
|
|
|
|
2013-11-04 20:18:37 -05:00
|
|
|
Create your web application container
|
|
|
|
-------------------------------------
|
2013-04-09 17:50:12 -04:00
|
|
|
|
2013-11-04 20:18:37 -05:00
|
|
|
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.
|
2013-04-09 17:50:12 -04:00
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
2013-11-04 20:18:37 -05:00
|
|
|
sudo docker run -link redis:db -i -t ubuntu:12.10 /bin/bash
|
2013-04-09 11:04:14 -04:00
|
|
|
|
2013-11-04 20:18:37 -05:00
|
|
|
Once inside our freshly created container we need to install Redis to get the
|
|
|
|
``redis-cli`` binary to test our connection.
|
2013-04-09 11:04:14 -04:00
|
|
|
|
2013-11-04 20:18:37 -05:00
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
apt-get update
|
|
|
|
apt-get -y install redis-server
|
|
|
|
service redis-server stop
|
|
|
|
|
|
|
|
Now we can test the connection. Firstly, let's look at the available environmental
|
|
|
|
variables in our web application container. We can use these to get the IP and port
|
|
|
|
of our ``redis`` container.
|
2013-04-09 11:04:14 -04:00
|
|
|
|
2013-04-09 17:50:12 -04:00
|
|
|
.. code-block:: bash
|
2013-04-09 11:04:14 -04:00
|
|
|
|
2013-11-04 20:18:37 -05:00
|
|
|
env
|
|
|
|
. . .
|
|
|
|
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 environmental varaibles 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
|
2013-04-09 11:04:14 -04:00
|
|
|
OK
|
2013-11-04 20:18:37 -05:00
|
|
|
redis 172.17.0.33:6379> get docker
|
2013-04-09 11:04:14 -04:00
|
|
|
"awesome"
|
2013-11-04 20:18:37 -05:00
|
|
|
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.
|
|
|
|
|