A few weeks ago I was presenting at SQL Saturday Raleigh and was asked a question that I didn’t know the answer to.
The question was, “can you change the location of named volumes in docker?”
This is one of the things that I love about presenting, being asked questions that I don’t know the answer to. They give me something to go away and investigate (many thanks to Dave Walden (b|t) for his help!)
N.B. – I’ve previously written about persisting data using named volumes here
First let’s have a look at a named volume. To create one, run: –
docker volume create sqlserver
And now let’s have a look at it: –
docker volume inspect sqlserver
You can see above where the named volume lives on the host. But what if we want to change that location?
Well, in order to do so we need to use a docker volume plugin. Which unfortunately means that this functionality is not available on Windows or on MACs (as plugins aren’t supported on those platforms). The workaround is to run the plugin from a container but I would just mount a volume from the host (see here).
The plugin that I’m going to use is the Local Persist Plugin
Really simple to install: –
curl -fsSL https://raw.githubusercontent.com/CWSpear/local-persist/master/scripts/install.sh | sudo bash
And we are good to go!
Ok, let’s create a directory to point our named volume to: –
mkdir /sqlserver
And now we can create our named volume: –
docker volume create -d local-persist -o mountpoint=/sqlserver --name=sqlserver2
Let’s have a look at it: –
docker volume inspect sqlserver2
And there you have it, the named volume pointing to a custom location.
Thanks for reading!