Copy data from one data volume to another
Recently I've played around with Docker and CoreOS I made some mistakes in the beginning. When I discovered that my data volume is based on the same image as the container which is supposed to use it (therefore wasting disk space) I wanted to move my data from the old one to the new, smaller one. Here's how you do it:
Create plain data volume
Note: you don't have to create it if you already have a container/volume you want your data to be copied into.
$ docker run -v /ghost_override --name ghost-data busybox \
echo "Data-only container for Ghost"
Where:
/ghost_overrideis the dir where volume will be mounted (inside of the container)ghost-datais the friendly name for this containerbusyboxis a very small (about 2.5MB) image great for data volumes
Copy data from old container to the host system
$ docker run --rm -v /tmp/ghost_override:/host:rw --volumes-from ghost-old busybox \
cp -r /ghost_override/ /host
What we do here:
--rmwill remove this container after it's finished-v /tmp/ghost_override:/host:rwwill mount host's/tmp/ghost_overridedir to/hostdir inside the container in read-write mode--volumes-from ghost-oldwill use the old container's volumes we want to copycp -r /ghost_override/ /hostwill copy contents of old volume to our host dir
Copy data from the host system to the new container
$ docker run --rm -v /tmp/ghost_override:/host:ro --volumes-from ghost-data busybox \
cp -a /host/ghost_override/ /ghost_override
This will:
-v /tmp/ghost_override:/host:romount host's/tmp/ghost_overridedir to/hostdir inside the container in read-only mode--volumes-from ghost-datause new container's volumecp -a /host/ghost_override/ /ghost_overridecopy data to our new container
Then you can remove /tmp/ghost_override dir, ghost-old container (docker rm ghost-old) and start using new container's volume (--volumes-from ghost-data).