Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy docker volume from one machine to another?

I have created a docker volume for postgres on my local machine.

docker create volume postgres-data

Then I used this volume and run a docker.

docker run -it -v postgres-data:/var/lib/postgresql/9.6/main postgres

After that I did some database operations which got stored automatically in postgres-data. Now I want to copy that volume from my local machine to another remote machine. How to do the same.

Note - Database size is very large

like image 583
murli2308 Avatar asked Mar 23 '17 10:03

murli2308


People also ask

How do I move docker volume to another computer?

First stop the docker service. Then the volumes can be moved from the default location at /var/lib/docker to the new location. Next the configuration of the docker daemon is edited to point to the new location of the volumes. The next step may not be necessary, but it is a good habit to do it anyway.

Can you export docker volumes?

Description. The docker export command does not export the contents of volumes associated with the container. If a volume is mounted on top of an existing directory in the container, docker export will export the contents of the underlying directory, not the contents of the volume.

Can two docker containers share the same volume?

Multiple containers can run with the same volume when they need access to shared data. Docker creates a local volume by default.


2 Answers

If the second machine has SSH enabled you can use an Alpine container on the first machine to map the volume, bundle it up and send it to the second machine.

That would look like this:

docker run --rm -v <SOURCE_DATA_VOLUME_NAME>:/from alpine ash -c \
    "cd /from ; tar -cf - . " | \
    ssh <TARGET_HOST> \
    'docker run --rm -i -v <TARGET_DATA_VOLUME_NAME>:/to alpine ash -c "cd /to ; tar -xpvf - "'

You will need to change:

  • SOURCE_DATA_VOLUME_NAME
  • TARGET_HOST
  • TARGET_DATA_VOLUME_NAME

Or, you could try using this helper script https://github.com/gdiepen/docker-convenience-scripts

Hope this helps.

like image 131
Simon I Avatar answered Oct 13 '22 17:10

Simon I


I had an exact same problem but in my case, both volumes were in separate VPCs and couldn't expose SSH to outside world. I ended up creating dvsync which uses ngrok to create a tunnel between them and then use rsync over SSH to copy the data. In your case you could start the dvsync-server on your machine:

$ docker run --rm -e NGROK_AUTHTOKEN="$NGROK_AUTHTOKEN" \
  --mount source=postgres-data,target=/data,readonly \
  quay.io/suda/dvsync-server

and then start the dvsync-client on the target machine:

docker run -e DVSYNC_TOKEN="$DVSYNC_TOKEN" \
  --mount source=MY_TARGET_VOLUME,target=/data \
  quay.io/suda/dvsync-client

The NGROK_AUTHTOKEN can be found in ngrok dashboard and the DVSYNC_TOKEN is being shown by the dvsync-server in its stdout.

Once the synchronization is done, the dvsync-client container will stop.

like image 31
suda Avatar answered Oct 13 '22 17:10

suda