Search code examples
dockerdocker-stackdocker-cp

How to `docker cp` ssh key to docker container before its entrypoint is executed


Say I have this right now:

docker run -v /root/.ssh:/root/.ssh:ro my_image

and the ENTRYPOINT for the above image is:

ENTRYPOINT ["echo", "foo"]

instead I want to do something like this:

docker run -d --name c my_image   # problem: this will likely exit early :(
docker cp /root/.ssh c:/root/.ssh
docker exec c echo foo

the problem is: how do I keep the container alive so that it waits for me to copy the ssh key into it and then run the echo foo command?

Maybe I can keep it alive by telling it to wait for stdin? But how exactly?


Solution

  • you need first to create the container:

    docker create my_image
    

    then Copy the files:

     docker cp /root/.ssh MY_CREATED_CON:/root/.ssh
    

    start the container normally:

    docker start MY_CREATED_CON