Duplicate Volumes in Docker

Quick Tip

Sometimes when you are working with database containers that store data in Docker Volumes you may need to duplicate them because you want to clone a container or because you have to create a data safe backup before some testing.
Let see how we can duplicate a volume:

Create the New Volume

First we need to create a new volume:

pato@patocontainer ~ $ docker volume create --name patovolsave
patovolsave
Duplicate by Copying Contents

In order to do the copy we are going to run a temporary container --rm using lightweight alpine linux. There we are mounting the source and the new volumes and copying all the contents from one directory to the other:

pato@patocontainer ~ $ docker run --rm -i -t -v patovoldb:/origen -v patovolsave:/destino alpine sh -c "cp -avr /origen/* /destino"
'/origen/aria_log.00000001' -> '/destino/aria_log.00000001'
'/origen/aria_log_control' -> '/destino/aria_log_control'
...

pato@patocontainer ~ $ docker volume ls
DRIVER              VOLUME NAME
local               patovoldb
local               patovolsave

That’s it, you now have a new volume duplicated from the original