Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker build + private NPM (+ private docker hub)

I have an application which runs in a Docker container. It requires some private modules from the company's private NPM registry (Sinopia), and accessing these requires user authentication. The Dockerfile is FROM iojs:latest.

I have tried:

1) creating an .npmrc file in the project root, this actually makes no difference and npm seems to ignore it 2) using env variables for NPM_CONFIG_REGISTRY, NPM_CONFIG_USER etc., but the user doesn't log in.

Essentially, I seem to have no way of authenticating the user within the docker build process. I was hoping that someone might have run into this problem already (seems like an obvious enough issue) and would have a good way of solving it.

(To top it off, I'm using Automated Builds on Docker Hub (triggered on push) so that our servers can access a private Docker registry with the prebuilt images.)

Are there good ways of either: 1) injecting credentials for NPM at build time (so I don't have to commit credentials to my Dockerfile) OR 2) doing this another way that I haven't thought of ?

like image 620
GTF Avatar asked Jun 01 '15 12:06

GTF


People also ask

Can you make an NPM package private?

On the npm website, go to the package page. On the package page, click Admin. Under "Package Access", select "Is Package Private?" Click Update package settings.

How do I create a docker image and press to Docker Hub?

To push an image to Docker Hub, you must first name your local image using your Docker Hub username and the repository name that you created through Docker Hub on the web. You can add multiple images to a repository by adding a specific :<tag> to them (for example docs/base:testing ).

Where can I host npm private packages?

If you want to host a private NPM package but do not want to pay US$ 7 per user, per month to host it directly at https://www.npmjs.com/ this post is for you. Here I will share a very practical way you can host it privately for free at Github Packages Registry + NPM.


1 Answers

I found a somewhat elegant-ish solution in creating a base image for your node.js / io.js containers (you/iojs):

  1. log in to your private npm registry with the user you want to use for docker
  2. copy the .npmrc file that this generates

Example .npmrc:

registry=https://npm.mydomain.com/
username=dockerUser
[email protected]
strict-ssl=false
always-auth=true
//npm.mydomain.com/:_authToken="someAuthToken"
  1. create a Dockerfile that copies the .npmrc file appropriately.

Here's my Dockerfile (based on iojs:onbuild):

FROM iojs:2.2.1

MAINTAINER YourSelf

# Exclude the NPM cache from the image
VOLUME /root/.npm

# Create the app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# Copy npm config
COPY .npmrc /root/.npmrc

# Install app
ONBUILD COPY package.json /usr/src/app/
ONBUILD RUN npm install
ONBUILD COPY . /usr/src/app

# Run
CMD [ "npm", "start" ]
  1. Make all your node.js/io.js containers FROM you/iojs and you're good to go.
like image 151
GTF Avatar answered Sep 18 '22 21:09

GTF