murli2308
murli2308

Reputation: 3002

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

Upvotes: 16

Views: 11833

Answers (2)

Simon I
Simon I

Reputation: 3586

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.

Upvotes: 17

suda
suda

Reputation: 2644

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.

Upvotes: 4

Related Questions