Data management with volumesEstimated reading time: 5 minutes
In the previous step, we set up Redis but didn’t provide it a way to store the data it’s caching. This means that if you redeployed the redis service, or if the container crashed, the data would be lost. To save the data so it persists beyond the life of a container, or share data from one container to another, you’ll need to define a volume.
In order to persist, data in Docker Cloud must be stored in a volume. The volume can be defined on the image (for example in the Dockerfile), or specified when you create a new service in the Docker Cloud web UI. Learn more about volumes in Docker Cloud here.
Test for lack of persistence
redeploy the Redis service you created earlier, you’ll see that the counter resets.
Let’s try that. First, redeploy the redis service to reset the counter.
$ docker-cloud service redeploy redis --not-reuse-volumes
Check the container status using the
container ps command, and wait until the new container is running again. In the example below you can see the original container in the “Terminated” state, and the new container that is “Starting”.
$ docker-cloud container ps --service redis NAME UUID STATUS IMAGE RUN COMMAND EXIT CODE DEPLOYED PORTS redis-1 5ddc0d66 ✘ Terminated redis:staging /run.sh 0 15 minutes ago 6379/tcp redis-1 3eff67a9 ⚙ Starting redis:staging /run.sh
Once the container is running, get the web endpoint using
container ps, then try curling or visiting the web endpoint again
$ curl lb-1.$DOCKER_ID_USER.cont.dockerapp.io:80 <h3>Hello Friendly Users!</h3><b>Hostname:</b> web-1<br/><b>Visits:</b> 1%
The Redis cache service redeployment caused the counter to reset.
The specific Redis image (redis) in this tutorial supports data persistence. This is not a common requirement for a Redis cache and it’s not enabled by default in most images. However to activate this in our image, you only need to set two environment variables.
Run the following command to create and set these two environment variables.
$ docker-cloud service set \ -e REDIS_APPENDONLY=yes \ -e REDIS_APPENDFSYNC=always \ redis --redeploy
This command defines two new environment variables in the redis service and
then redeploys the service so they take effect. You can learn more about our
redis image here.
With these settings, Redis can create and store its data in a volume. The volume is in
Visit the web endpoint a few more times to make sure that the cache is working as expected. Then redeploy the Redis service to see if the counter resets, or if it persists even after the container is terminated and re-created.
Curl the service to increment the counter:
$ curl lb-1.$DOCKER_ID_USER.cont.dockerapp.io:80 <h3>Hello Python users!!</h3><b>Hostname:</b> web-1<br/><b>Visits:</b> 1% $ curl lb-1.$DOCKER_ID_USER.cont.dockerapp.io:80 <h3>Hello Python users!!</h3><b>Hostname:</b> web-2<br/><b>Visits:</b> 2% $ curl lb-1.$DOCKER_ID_USER.cont.dockerapp.io:80 <h3>Hello Python users!!</h3><b>Hostname:</b> web-3<br/><b>Visits:</b> 3%
Next, redeploy the service using the
service redeploy command:
$ docker-cloud service redeploy redis
Check the service status:
$ docker-cloud container ps --service redis NAME UUID STATUS IMAGE RUN COMMAND EXIT CODE DEPLOYED PORTS cache-1 8193cc1b ✘ Terminated redis:staging /run.sh 0 10 minutes ago 6379/tcp cache-1 61f63d97 ▶ Running redis:staging /run.sh 37 seconds ago 6379/tcp
Once the service is running again, curl the web page again to see what the counter value is.
$ curl lb-1.$DOCKER_ID_USER.cont.dockerapp.io:80 <h3>Hello Python users!!</h3><b>Hostname:</b> web-3<br/><b>Visits:</b> 4%
Congratulations! You’ve set up data persistence in Docker Cloud!
Sharing/reusing data volumes between services
A service’s volume can be accessed by another service. To do this you use the
--volumes-from flag when creating the new service.
You might use this functionality to share data between two services, or to back up, restore, or migrate a volume to a local host or a cloud storage provider.
Download volume data for backup
In this next step, you’ll download the
/data volume from Redis to your local host using SCP (secure copy).
First, run a SSH service that mounts the volumes of the redis you want to back up:
$ docker-cloud service run -n download -p 2222:22 -e AUTHORIZED_KEYS="$(cat ~/.ssh/id_rsa.pub)" --volumes-from redis tutum/ubuntu
Then run scp to download the data volume files in Redis:
$ scp -r -P 2222 root@downloader-1.$DOCKER_ID_USER.svc.dockerapp.io:/data .
You now have a backup copy of the Redis data on your local host machine!
Congratulations! You’ve completed the tutorials! You can now push an image to Docker Cloud, deploy an app to your Cloud nodes, set environment variables, scale the service, view logs, set up a load balancer and a data back end, and set up a volume to save the data.
You might also want to delete or remove all of your hello world Stacks, Services, and Nodes running in Docker Cloud. To clean up when you’re finished with the tutorial:
- Click Stacks in the left navigation, hover over the stack you created and click the selection box that appears, then click Terminate.
- Once the Stack has terminated, click Services in the left navigation, hover over each service you created, click the selection box that appears, then click Terminate.
- Click Node Clusters in the left navigation, hover over the node cluster you created, click the selection box that appears, then click Terminate.
Objects (Stacks, Services, Node Clusters, and Containers and nodes) still appear in the list in Docker Cloud for about five minutes after they are terminated.
Happy Docking!Python, data, management