How to copy docker volume from one machine to another?

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

docker create volume postgres-data 

Then I used this volume and launched docker.

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

After that, I performed some database operations that were automatically saved in postgres-data. Now I want to copy this volume from my local machine to another remote machine. How to do the same.

Note. The database size is very large.

+5
docker postgresql
Mar 23 '17 at 10:26
source share
2 answers

If SSH is enabled on the second computer, you can use the Alpine container on the first computer to map the volume, bind it, and send it to the second computer.

It will 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 can try using this helper script https://github.com/gdiepen/docker-convenience-scripts

Hope this helps.

+8
Mar 23 '17 at 12:07 on
source share

I had exactly the same problem, but in my case both volumes were in separate VPCs and could not expose SSH for the outside world. In the end, I created dvsync, which uses ngrok to create a tunnel between them, and then used rsync via SSH to copy the data. In your case, you can run 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 run 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 

NGROK_AUTHTOKEN can be found on the ngrok dashboard, and DVSYNC_TOKEN shows dvsync-server in its standard dvsync-server .

As soon as the dvsync-client synchronization, the dvsync-client container stops.

+3
Jul 10 '18 at 19:32
source share



All Articles