Thursday, October 28, 2021

[SOLVED] How to copy SSH from JENKINS host into a DOCKER container?

Issue

I can't copy the file from the host into the container using the Dockerfile, because i'm simply not allowed to, as mentioned in Docker Documentation:

The path must be inside the context of the build; you cannot COPY ../something /something, because the first step of a docker build is to send the context directory (and subdirectories) to the docker daemon.


I'm also unable to do so from inside jenkins job, because the job commands run inside the shell of the docker container, there is not way to talk to the parent(which is the jenkins host).


This jenkins plugin could have been a life saver, but as mentioned in the first section: distribution of this plugin has been suspended due to unresolved security vulnerabilities.


Solution

This is how I copy files from host to docker image using Dockerfile

I have a folder called tomcat

Inside that, I have a tar file and Dockerfile

Commands to do the whole process just for understanding

$ pwd
/home/user/Documents/dockerfiles/tomcat/

$ ls
apache-tomcat-7.0.84.tar.gz  Dockerfile

Sample Docker file:

FROM ubuntu_docker
COPY apache-tomcat-7.0.84.tar.gz /home/test/
...

Docker commands:

$ docker build -it testserver .

$ docker run -itd --name test1  testserver

$ docker exec -it bash

Now you are inside docker container

# ls
apache-tomcat-7.0.84.tar.gz

As you can see I am able to copy apache-tomcat-7.0.84.tar.gz from host to Docker container.

Notice the Docker Documentation first line which you have shared

The path must be inside the context of the build;

So as long as the path is reachable during build you can copy.

Another way of doing this would be using volume

docker run -itd -v $(pwd)/somefolder:/home/test  --name test1  testserver

Notice -v parameter

You are telling Docker to mount Current_Directory/somefolder to Docker's path at /home/test

Once the container is up and running you can simply copy any file to $(pwd)/somefolder and it will get copied inside container at /home/test



Answered By - MyTwoCents