简体   繁体   中英

Install private npm package in Docker via SSH

I have a main repository which contains a NPM package that loads another private NPM package, both sitting on the same organization in Gitlab.

I've been researching for hours on this and found many ways it doesn't work. First off here is the Dockerfile that contains the, like I think, most common way to add my SSH key.

FROM node:10.15.1-alpine as image
WORKDIR /usr/src/app
RUN apk add --update --no-cache openssh git

COPY package.json ./
ARG SSH_PRIVATE_KEY
RUN mkdir /root/.ssh/ && \
    echo "$SSH_PRIVATE_KEY" > /root/.ssh/id_rsa && \
    chmod 600 /root/.ssh/id_rsa && \
    touch /root/.ssh/known_hosts && \
    ssh-keyscan gitlab.com > /root/.ssh/known_hosts
RUN npm install

FROM image as build
COPY . .
ADD https://github.com/ufoscout/docker-compose-wait/releases/download/2.2.1/wait /wait
RUN chmod +x /wait
CMD /wait && npm run start
EXPOSE 4000

I am calling it via docker build --build-arg SSH_PRIVATE_KEY="$(cat ~/.ssh/id_rsa)" -t test --squash .

My package.json contains "shared": "git+ssh://git@gitlab.com:ORGA/PROJECT" where ORGA/PROJECT is ofc the real name of my organization and project.

Thing is I am always always always getting git@gitlab.com: Permission denied (publickey). .

In Docker:

  • /root/.ssh/id_rsa : contains the correct SSH key that is also registered in Gitlab and works locally on my own Mac.
  • /root/.ssh/known_hosts : contains entries for gitlab.com ssh-rsa , gitlab.com ecdsa-sha2-nistp256 , gitlab.com ssh-ed-25519 .

ls -lah /root/.ssh prints this:

-rw-------    1 root     root        3.2K Feb 26 14:05 id_rsa
-rw-r--r--    1 root     root         656 Feb 26 14:05 known_hosts

Also I tried adding npm install to the same RUN command.

I feel like my git client doesn't have access to the SSH agent or something like that. Do you have an idea?

With docker 1809+ you can use the new Dockerfile syntax to directly mount your ssh folder into the container.

There is an example very similar to your need in the documentation .

Copied from there and adapted to your use case :

# syntax=docker/dockerfile:experimental
FROM node:10.15.1-alpine as image

WORKDIR /usr/src/app

RUN apk add --update --no-cache openssh-client git \
 && mkdir -p -m 0600 ~/.ssh && ssh-keyscan github.com >> ~/.ssh/known_hosts

COPY package.json ./
RUN --mount=type=ssh npm install

# [...snip...]

Then : docker build --ssh default -t test --squash .

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