Vigilante
Vigilante

Reputation: 131

Pass ssh-agent to dockerfile to install private repository modules

I am trying to automate a docker build in Jenkins pipeline. In my dockerfile, I basically build a node application. In my npm install, I have some private git repositories which need os bindings and so have to be installed in the container. When I run this manually, I transfer my ssh keys (id_rsa) to dockerfile which is used for doing npm install. Now, my problem is when running this task in jenkins pipeline, I will be configuring a ssh-agent(Jenkins plugin). It will not be possible to extract private key from ssh-agent. How should I pass my ssh-agent to my dockerfile.

EDIT 1:

I got it partially working by this:

Docker Build Command:
DOCKER_BUILDKIT=1 docker build --no-cache -t $DOCKER_REGISTRY_URL/$IMAGE_NAME:v$BUILD_NUMBER --ssh default . &&

Then in Docker file:

This works fine:

RUN --mount=type=ssh GIT_SSH_COMMAND="ssh -vvvT -o StrictHostKeyChecking=no" 
git clone [email protected]:****

Weird thing is this doesn't work:

RUN --mount=type=ssh GIT_SSH_COMMAND="ssh -vvvT -o StrictHostKeyChecking=no" npm install git+ssh//[email protected]:****

I feel this is something to do with StrictHostKeyChecking=no

Upvotes: 3

Views: 2586

Answers (1)

Vigilante
Vigilante

Reputation: 131

I finally got it working by using ROOT user in Dockerfile and setting the npm cache to root. The problem was that git was using the /root/.ssh folder while npm was using a different path - /home/.ssh as it's npm cache was set on /home/.ssh

For anyone still struggling, this is the config I used

Docker Build Command:

DOCKER_BUILDKIT=1 docker build --no-cache -t test --ssh default .

Dockerfile:

USER root

RUN apt-get update && \
    apt-get install -y \
        git \
        openssh-server \
        openssh-client
RUN mkdir -p -m 600 /root/.ssh && ssh-keyscan github.com >> /root/.ssh/known_hosts && echo "Host *\n  StrictHostKeyChecking no" > /root/.ssh/config
RUN echo "Check ssh_config" && cat /root/.ssh/config
RUN rm -rf node_modules
RUN npm config set cache /root
RUN --mount=type=ssh GIT_SSH_COMMAND="ssh -vvvT" npm install

Upvotes: 8

Related Questions