Provision a data backend for your service

Estimated reading time: 2 minutes

Docker Cloud offers a large number of data stores in the Jumpstart library, including Redis, MongoDB, PostgreSQL, and MySQL.

You may have noticed that your app has a visit counter that’s been disabled up until now. In this step you’ll add a data backend that your service will use. In this specific tutorial we’ll use a Redis cache, but most concepts apply to any data backend.

Provision the service

The first step is to provision the data service itself. Run this command to create and run the Redis service using the redis image:

$ docker-cloud service run \
--env REDIS_PASS="password" \
--name redis \
redis

–env REDIS_PASS=”password” defines an environment variable that sets the password for Redis. Because we are not publishing any ports for this service, only services linked to your Redis service will be able to connect to it.

Use docker-cloud service ps to check if your new redis service is running. This might take a minute or two.

$ docker-cloud service ps
NAME                 UUID      STATUS            IMAGE                                          DEPLOYED
redis                89806f93  ▶ Running         redis:latest                                   29 minutes ago
web                  bf644f91  ▶ Running         my-username/python-quickstart:latest           26 minutes ago
lb                   2f0d4b38  ▶ Running         dockercloud/haproxy:latest                     25 minutes ago

Next, we’ll set up the link between the redis service and the web service.

$ docker-cloud service set --link redis:redis --redeploy web

In this command, we’re creating a link from the web service (specified at the end of the command) to the redis service, and naming the link redis.

Next, visit or curl the load balanced web endpoint again. You’ll notice that the web service now counts of the number of visits to the web service. This uses the Redis data backend, and is synchronized between all of the service’s containers.

If you’re using curl, you should see the counter incrementing like this:

$ curl lb-1.$DOCKER_ID_USER.cont.dockerapp.io
Hello World</br>Hostname: web-1</br>Counter: 1%
$ curl lb-1.$DOCKER_ID_USER.cont.dockerapp.io
Hello World</br>Hostname: web-3</br>Counter: 2%
$ curl lb-1.$DOCKER_ID_USER.cont.dockerapp.io
Hello World</br>Hostname: web-2</br>Counter: 3%
$ curl lb-1.$DOCKER_ID_USER.cont.dockerapp.io
Hello World</br>Hostname: web-5</br>Counter: 4%
$ curl lb-1.$DOCKER_ID_USER.cont.dockerapp.io
Hello World</br>Hostname: web-3</br>Counter: 5%

What’s Next?

Next, we’ll look at Stackfiles for your service.

chat icon Feedback? Suggestions? Can't find something in the docs?
Edit this page Request docs changes Get support