11

I have a running container on a remote machine. I am connected to the machine via ssh. Now i would like to download a certain file from the container. Can somebody give me some tips how to achieve that ? Thanks.

Andrej Hucko
  • 510
  • 1
  • 5
  • 15

3 Answers3

17

If you want to download files from a docker container to your local machine, you can do it with Docker itself (no need for SSH):

docker cp <containerId>:/path/to/file /host/target/path

Update: I just read that you are connected to a remote container. Then you can use SCP for that:

scp user@host:/path/to/file /local/path
Michael Altenburger
  • 1,275
  • 10
  • 17
  • 8
    I think this is not the correct answer to the question. The first command just copy a file from a container to a remote machine and the second command copy a file from a remote machine to local. The question is about how to copy a file from a remote container to a local machine. – Barpa Apr 14 '21 at 10:28
6

sounds like a great application for docker context.

  • create a context: docker context create myserver --docker "host=ssh://myserver.com"
  • copy files as usual: docker --context myserver cp /local/path/file.txt containername:/path/in/container/

This works in reverse, too, to get files from the remote docker container to your local file system. Best to have key-based access defined for your remote server.

Another solution is to create a volume, start a docker container temporarily that exports this volume to the world (ftp, smb, nfs, you name it). Once that is done, you can then mount that volume to your target container and have access to the files. This one, too, obviously works just fine in reverse.

leggewie
  • 242
  • 2
  • 9
2

Let's assume that when you connect to a docker container you must first SSH to the remote host using public key, sudo to root with password, and use docker exec to get a shell in the container. This is inconvenient but it is the sort of thing we get handed and we can work around the constraints.

When you are connecting you need the following and so any command doing the copy will require this information:

  1. ssh_host: Name of docker host
  2. ssh_user: SSH user on docker host
  3. sudo_password: root password on the host
  4. container_name: Name of docker container
  5. file_dir: Directory containing the file on the remote host
  6. file_name: Filename to transfer

First, let's test the authentication from the localhost. The following command should return the hostname of the docker container.

echo '<sudo_password>' | ssh '-oRequestTTY=no' <ssh_user>@<ssh_host> --  \
   "sudo -p '' -S docker exec <container_name> hostname -f "

With the above working the next command will transfer a single file from the docker container to the localhost. We pipe the password to SSH that forwards the pipe to sudo. docker exec will launch tar that archives the file and opens a pipe to SSH that forwards the pipe to tar on the local host.

echo '<sudo_password>' | ssh '-oRequestTTY=no' <ssh_user>@<ssh_host> \
   "sudo -p '' -S -- docker exec <container_name> tar cf - -C <file_dir> <file_name>" \
   | tar xvf - 

One problem with this approach is that the root password will show in the process list on the docker host. We can work around this if we have SSH forward an environment variable with the password.

For example we can choose an environment variable that we know gets forwarded based on the configuration in /etc/ssh/sshd_config and set this in ~/.ssh/config.

Host ssh_host
    SetEnv LC_IDENTIFICATION=<sudo_password>

We can then modify the previous command to use the new variable.

ssh '-oRequestTTY=no' <ssh_user>@<ssh_host> \
   "echo \$LC_IDENTIFICATION | sudo -p '' -S -- docker exec <container_name> tar cf - -C <file_dir> <file_name>" \
   | tar xvf -
Chad Juliano
  • 1,195
  • 9
  • 4
  • I found a similar but simpler solution at https://medium.com/opsops/how-to-copy-a-file-to-a-remote-docker-container-39b928f63ea8 cd /directory/with/your/file && tar c yourfile.txt | ssh username@servername docker cp - 123123123123:/path/in/the/container Note that 123123123123 needs to match the container id (get that from `docker ps`) – Confidant Jan 06 '23 at 21:13
  • The second code fragment that @Chad Juliano wrote copies a file from the docker container to localhost. Would somebody be able write a version which copies a file from localhost to the docker container? – waykiki Jan 19 '23 at 13:44