简体   繁体   中英

Copy file from remote docker container

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. 🙂

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

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 -

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM