Use github private repo to deploy key inside docker build step for npm install

My use case is that I have multiple express microservices that use the same middleware and I would like to create a different repo in npm module format for each middleware.

Each repo is a private repo and can have a deployment key attached (can be different keys or the same)

This all works fine locally. However, when I try to use this with a docker compose setup, it fails in the npm install stage in the build stage.

Dockerfile

FROM node:alpine
RUN npm install --production
CMD npm start

      

Docker-compose.yml

services:
   node-api:
        build:
            context: .
            dockerfile: Dockerfile

      

I realize this doesn't work because I don't have a deployment key that I use on my local system in the Docker context.

I was looking for a solution and none of them seem very lightweight / not hacky

My question is the most efficient safe possible solution, automatic (minimum manual steps for file users)? Exercise timing is not a concern. I try to avoid checking any sensitive data by letting other people run this locally.

+3


source to share


1 answer


Experiment with this new feature: Docker multistage build

You can selectively copy artifacts from one stage to another, leaving behind anything you don't want in the final image.

The idea is to create a temporary base image and then run the build again, only taking what you want from the previous image. It uses multiple FROMs in the same Dockerfile:



FROM node as base-node-modules
COPY your_secret_key /some/path
COPY package.json /somewhere
RUN npm install <Wich use your key>

FROM node #yes again!
...
...
COPY --from=base-node-modules /somewhere/node_modules /some/place/node_modules
...
... # the rest of your Dockerfile
...

      

Docker will discard anything you don't keep from the first FROM.

+6


source







All Articles