# Challenge

When you work with docker images it is sometimes difficult to get persistency done right. Where is stuff saved? What can I delete? What do I need to backup?
What if an upgrade comes along, do I loose my data?

# Solution: Docker Data Volumes

I found the problem out the hard way. I lost all my test data because of an upgrade. I totally forgot that if you delete an image you lose the data. So I had a new goal. Get the whole setup working again but now with external data.

Why not put the data on the native file system? Well because that it is more difficult to setup because of the different native file systems.

In this blog I will try to explain how to make this all work with the standard postgres image

# Case: Make postgres use a data volume

I have the standard postgres docker image and want to externalise its data to a data volume and be able to:

• Make backups
• Restore backups
etc.

# Step 1: Create a data only docker image

It is actually very easy to create a data volume.

This command will do it:

This command will create a named images called “data” with a volume /data based on the busybox image.
If you have another images that uses a volume situated in /data you can make it use the one you created by using the –from-volume option.

You can inspect images to see what they are using for volumes.

with result:
map[/var/lib/postgresql/data:{}]

This means that the postgres image has one volume configured and it is located at: /var/lib/postgresql/data
Now we have something to work with.
If you do not configure a data volume, postgres will store its data at the mentioned location, but that also means that if you throw away the image because of e.g. an upgrade you will loose the data.
Separation of concerns baby… so we want a datavolume.

If you look at your docker images you will see that it created the ivonet-postgres-data image.

# Step 2: Have fun with a data volume

Just to get used to some tricks… a bit of playing around to get familiair with it all.

will do not much ;-) and if you docker ps it, it will not show because the “output” is true…
but how to access the /var/lib/postgresql/data volume then?
remember the –volumes-from parameter I mentioned earlier?

This will enter the shell of the busybox images but with the volumes-from ivonet-postgres-data associated with it. When ls-ing it it will show a data folder and yes that is the folder of your data-volume.

now still in the shell do the following:

now leave the shell by entering ctrl-d or logout
you now should have created a file in the /data folder of your data-volume called HelloWorld

# Step 3: Lets make backups work

what does this command do:

• run a busybox image that will be removed after quit
• give it access to the current local host volume mounted on /backup in the image
• use the ivonet-postgres-data volumes for access
• create a tar.gz from the postgres data folder
• quit

The result of this command in this example will be a file called something like backup-postgres-data-2015-12-11.tar in the folder you ran the command in (host folder).

If you unzip this it will reveal the HelloWorld file you created earlier.

# Step 4: Lets restore a backup

to test this we of course first need to remove the HelloWorld file. Or you can remove the whole data volume and recreate it from scratch. You should now have the skills :-)

You can also do it this way:

Now the restore:

Look if it worked. It should contain the HelloWorld file again.

Now remove the HelloWorld file again because Postgres needs an empty folder to initialize it’s database.

# Step 4: Now create a postgres with it all

Realy easy step :-)

You can look at the log:

Now if it all went wel you can look at the data folder again to see if it is filled with something like:

# recap

• Now you can create a data volume
• look at it
• manipulate it
• backup it
• restore it
• use it
• have fun with it!

Of course the backup you did is of the raw data and not the actual exported db data