Copying between docker, ssh, and your computer

·

2 min read

Sometimes there is a sudden and somewhat incomprehensible need to copy a large number of files from a remote server, and on top of that we have a docker on that server. We therefore have three layers. Your computer, the remote host, the docker on the remote host.

Connecting to ssh

Enter the remote host(configure the connection in the ~/.ssh/config file) :

ssh remotehost

Assume you are in the /home/ourproject directory.

Copying from docker on the remote host

We check the name of the docker we want to copy from on the remote host first.

docker ps

Suppose the container name phpapp pops up. We locate the directory we want in the container. E.g. docker exec -it phpapp bash, cd /var/www/mysite, ls. We notice the vendor directory. There are a lot of files there. It is convenient to pack everything into one file. Thus:

tar -czvf vendor.tar.gz vendor/

After creating the file, we type exit. It remains to extract the packed file from the docker to the directory we are in.

Copying from the remote host to our computer

docker cp phpapp:/var/www/htmlmysite/vendor.tar.gz vendor.tar.gz

Now the file is on the remote host. Exit exit. Now it's on our computer:

scp remotehost:/home/ourproject/vendor.tar.gz vendor.tar.gz

And unzip

tar -xvf vendor.tar.gz -C vendor/

Summary

It is easy to get lost between layers on different systems. I've written down this handful of commands here for my own use, but if you use this article - I'll be happy to.

Also remember that disk space capacity is not unlimited and clean up your files in the docker, on the remote host and on your own: rm vendor.tar.gz.

Sources: