rp346
rp346

Reputation: 7028

how to copy dir from remote host to docker image

I am trying to build a docker image, I have dockerfile with all necessary commands. but in my build steps I need to copy one dir from remote host to docker image. But if I put scp command into dockerfile, i'll have to provide password also into dockerfile, which I dont have to.

Anyone has some better solution to do this. any suggestion would be appreciated.

Upvotes: 3

Views: 4416

Answers (2)

hengsti
hengsti

Reputation: 350

You can copy a directory into a (even running) container at post build time

  1. On remote machine: Copy from remote host to docker host with

    scp -r /your/dir/ <user-at-docker-host>@<docker-host>:/some/remote/directory

  2. On docker machine: Copy from docker host into docker container

    docker cp /some/remote/directory <container-name>:/some/dir/within/docker/

Of course you can do step 1 also from your docker machine if you prefere that by simply adapting the source and target of the scp command.

Upvotes: 1

konradstrack
konradstrack

Reputation: 3002

I'd say there are at least options for dealing with that:

Option 1: If you can execute scp before running docker build this may turn out to be the easiest option:

  1. Run scp -r somewhere:remote_dir ./local_dir
  2. Add COPY ./local_dir some_path to your Dockerfile
  3. Run docker build

Option 2: If you have to execute scp during the build:

  1. Start some key-value store such as etcd before the build
  2. Place a correct SSH key (it cannot be password-protected) temporarily in the key-value store
  3. Within a single RUN command (to avoid leaving secrets inside the image):
    • retrieve the SSH key from the key-value store;
    • put it in ~/.ssh/id_rsa or start an ssh-agent and add it;
    • retrieve the directory with scp
    • remove the SSH key
  4. Remove the key from the key-value store

The second option is a bit convoluted, so it may be worth creating a wrapper script that retrieves the required secrets, runs any command, and removes the secrets.

Upvotes: 3

Related Questions