2015-06-07 23:07:20 -04:00
|
|
|
<!--[metadata]>
|
|
|
|
+++
|
|
|
|
title = "Dockerizing a Redis service"
|
|
|
|
description = "Installing and running an redis service"
|
|
|
|
keywords = ["docker, example, package installation, networking, redis"]
|
|
|
|
[menu.main]
|
2016-01-23 23:36:40 -05:00
|
|
|
parent = "engine_dockerize"
|
2015-06-07 23:07:20 -04:00
|
|
|
+++
|
|
|
|
<![end-metadata]-->
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2015-04-21 11:50:09 -04:00
|
|
|
# Dockerizing a Redis service
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
|
|
Very simple, no frills, Redis service attached to a web application
|
|
|
|
using a link.
|
|
|
|
|
2015-04-21 11:50:09 -04:00
|
|
|
## Create a Docker container for Redis
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
|
|
Firstly, we create a `Dockerfile` for our new Redis
|
|
|
|
image.
|
|
|
|
|
2015-01-07 10:12:02 -05:00
|
|
|
FROM ubuntu:14.04
|
2014-07-07 14:06:34 -04:00
|
|
|
RUN apt-get update && apt-get install -y redis-server
|
2014-04-15 20:53:12 -04:00
|
|
|
EXPOSE 6379
|
|
|
|
ENTRYPOINT ["/usr/bin/redis-server"]
|
|
|
|
|
|
|
|
Next we build an image from our `Dockerfile`.
|
|
|
|
Replace `<your username>` with your own user name.
|
|
|
|
|
2015-03-26 14:12:37 -04:00
|
|
|
$ docker build -t <your username>/redis .
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
|
|
## Run the service
|
|
|
|
|
2014-04-23 16:48:28 -04:00
|
|
|
Use the image we've just created and name your container `redis`.
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-04-23 16:48:28 -04:00
|
|
|
Running the service with `-d` runs the container in detached mode, leaving
|
|
|
|
the container running in the background.
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-04-23 16:48:28 -04:00
|
|
|
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
|
2014-04-15 20:53:12 -04:00
|
|
|
database.
|
|
|
|
|
2015-03-26 14:12:37 -04:00
|
|
|
$ docker run --name redis -d <your username>/redis
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
|
|
## Create your web application container
|
|
|
|
|
2014-04-23 16:48:28 -04: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.
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2015-03-26 14:12:37 -04:00
|
|
|
$ docker run --link redis:db -i -t ubuntu:14.04 /bin/bash
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
|
|
Once inside our freshly created container we need to install Redis to
|
|
|
|
get the `redis-cli` binary to test our connection.
|
|
|
|
|
2014-07-07 14:06:34 -04:00
|
|
|
$ sudo apt-get update
|
|
|
|
$ sudo apt-get install redis-server
|
|
|
|
$ sudo service redis-server stop
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-04-23 16:48:28 -04:00
|
|
|
As we've used the `--link redis:db` option, Docker
|
2014-04-15 20:53:12 -04:00
|
|
|
has created some environment variables in our web application container.
|
|
|
|
|
2014-05-01 10:13:34 -04:00
|
|
|
$ env | grep DB_
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2014-04-23 16:48:28 -04:00
|
|
|
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.
|
2014-04-15 20:53:12 -04:00
|
|
|
|
2014-05-01 10:13:34 -04:00
|
|
|
$ redis-cli -h $DB_PORT_6379_TCP_ADDR
|
|
|
|
$ redis 172.17.0.33:6379>
|
|
|
|
$ redis 172.17.0.33:6379> set docker awesome
|
2014-04-15 20:53:12 -04:00
|
|
|
OK
|
2014-05-01 10:13:34 -04:00
|
|
|
$ redis 172.17.0.33:6379> get docker
|
2014-04-15 20:53:12 -04:00
|
|
|
"awesome"
|
2014-05-01 10:13:34 -04:00
|
|
|
$ redis 172.17.0.33:6379> exit
|
2014-04-15 20:53:12 -04:00
|
|
|
|
|
|
|
We could easily use this or other environment variables in our web
|
|
|
|
application to make a connection to our `redis`
|
|
|
|
container.
|