I have an npm package hosted on github. It's working fine while working on the local machine. But it's not installing while the docker build process.
Dockerfile
FROM node:14-alpine as base
EXPOSE 8080
ARG LIB_GITHUB_TOKEN
ENV NODE_ENV=production \
LIB_GITHUB_TOKEN="${LIB_GITHUB_TOKEN}"
WORKDIR /home/node/app
RUN chown -R node:node /home/node/app
COPY --chown=node:node package.json package-lock*.json tsconfig.json .npmrc ./
RUN npm install --only=development
RUN ls -a **every pkgs installed except gihub pvt pkg**
RUN cd node_modules/ && ls
RUN npm run build
.npmrc file
registry=https://registry.npmjs.org/
#github_user:registry=https://npm.pkg.github.com/
//npm.pkg.github.com/:_authToken=${LIB_GITHUB_TOKEN}
Every packages are installed except my GitHub private package.
Same working properly with docker-compose.
If I add npm ci --only=production in dockerfile. it gives -
npm ERR! code E401
npm ERR! Unable to authenticate, need: Basic realm="GitHub Package Registry"
I tried using environment variables with .npmrc and I ran into the same 401 error you're experiencing. Instead of copying .npmrc into the image I added it to .dockerignore
And then do this in your Dockerfile
ARG github_token
ENV GITHUB_TOKEN=$github_token
RUN echo registry=https://registry.npmjs.org/ >> ~/.npmrc
RUN echo #github_user:registry=https://npm.pkg.github.com/ >> ~/.npmrc
RUN echo //npm.pkg.github.com/:_authToken=$GITHUB_TOKEN >> ~/.npmrc
And to build:
docker build . --build-arg github_token=${GITHUB_TOKEN}
Related
Environments files are in git ignore just because of confidential info so I need create those angular environments file while building docker image.
I am getting the below error while running the docker build.
an unhandled exception occurred: The /usr/src/app/ng/src/environments/environment.prod.ts path in file replacements does not exist.
Docker file
FROM node:16 AS ng-build
WORKDIR /usr/src/app
COPY frontend/ ./frontend/
RUN cd frontend && npm install --legacy-peer-deps && npm run build --aot --output-hashing=none
FROM node:16 AS node
WORKDIR /usr/src/app
COPY backend/ ./backend/
COPY --from=ng-build /usr/src/app/frontend/dist/ ./backend/public
RUN cd backend && npm ci --only=production --omit=dev && npm cache clean --force
COPY backend/server.js ./backend/
EXPOSE 3000
CMD ["node", "./backend/server.js"]
Please suggest to me the right way to achieve it.
Background:
I'm writing code in node.js, using npm and docker. I'm trying to get my docker file to use cache when I build it so it doesn't take too long.
We have a "common" repo that we use to keep logic that is used in a variety of repositories and this gets propagated is npm packages.
The problem:
I want the docker file NOT use the cache on my "common" package.
Docker file:
FROM node:12-alpine as X
RUN npm i npm#latest -g
RUN mkdir /app && chown node:node /app
WORKDIR /app
RUN apk add --no-cache python3 make g++ tini \
&& apk add --update tzdata
USER node
COPY package*.json ./
COPY .npmrc .npmrc
RUN npm install --no-optional && npm cache clean --force
ENV PATH /app/node_modules/.bin:$PATH
COPY . .
package.json has this line:
"dependencies": {
"#myorg/myorg-common-repo": "~1.0.13",
I have tried adding these lines in a variety of places and nothing seems to work:
RUN npm uninstall #myorg/myorg-common-repo && npm install #myorg/myorg-common-repo
RUN npm update #myorg/myorg-common-repo --force
Any ideas on how I can get docker to build and not use the cache on #myorg/myorg-common-repo ?
So I finally managed to solve this using this answer:
What we want to do is invalidate the cache for a specific block in the Docker file and then run our update command. This is done by adding a build argument to the command (CLI or Makefile) like so:
docker-compose -f docker-compose-dev.yml build --build-arg CACHEBUST=0
And then Adding this additional block to the Docker file:
ARG CACHEBUST=1
USER node
RUN npm update #myorg/myorg-common-repo
This does what we want.
The ARG CACHEBUST=1 invalidates the cache and the npm update command runs without it.
How does Docker build containers? I can't figure it out. I want:
build a container
pass local folder in it
install npm in the container (using dockerfile) in the volume folder( so, I can see it on my local drive)
run a command in my yaml config file
I've tried to list content of folders with ls command, but the /src/ is always empty (prints: src)
My docker-compose.yml:
version: '3'
services:
node:
build:
context: .
dockerfile: Dockerfile.node
volumes:
- ./src:/src
command: run develop
tty: true
My Dockerfile.node:
FROM node:12
WORKDIR /src
COPY ./src/package*.json ./src/
RUN ls
RUN cd ./src
RUN ls
RUN npm install
RUN ls
On the RUN npm install command I got this error:
npm WARN saveError ENOENT: no such file or directory, open '/src/package.json'
I start project with command docker-compose up --build
My folder structure is:
/
src
--package.json
docker-compose.yml
Dockerfile.node
Please help, thank you in advance.
cd ./src only available in the current RUN command, as Dockerfile each command run in a separate shell, so when it comes to run npm install at this time your working is WORKDIR that is /src not the one you are expecting using cd .src which should be /src/src.
RUN pwd
#/src
RUN cd ./src #here /src/src
RUN ls
#/src <-- back to WORKDIR, while you are expecting /src/src
RUN npm install
In short, there is WORKDIR in dockerfile not cd.
You have to option, change command
RUN cd ./src && npm i
or change the copy command and leave the rest as it is.
COPY ./src/package*.json .
Trying to build angular application in docker and run as container in my local using Node js.
I have used build image using below Dockerfile, but i am not sure what i am missing while running. Can someone point me out?
Dockerfile:
FROM node:10.15.3
ENV HOME=/home
WORKDIR $HOME
RUN npm config set strict-ssl false \
&& npm config set proxy http://proxy.xxxxxx.com:8080
COPY package.json .
RUN npm install
Image created with below command successfully
docker build -t example .
I am trying to run the image using below command, but it is not helping
docker run -p 4201:4200 example
your Dockerfile does not run/serve your application, in order to do that you have to:
install angular/cli
copy the app
run/serve the app
FROM node:10.15.3
RUN npm config set strict-ssl false \
&& npm config set proxy http://proxy.xxxxxx.com:8080
# get the app
WORKDIR /src
COPY . .
# install packages
RUN npm ci
RUN npm install -g #angular/cli
# start app
CMD ng serve --host 0.0.0.0
hope this helps.
Container need a foreground process running, then it will not exit. If not, the container will directly exit.
For your case, you need to COPY your nodejs project to container when docker build, and also start the project in CMD like CMD [ "npm", "start" ]. As the web server not exit, then your container will not exit.
A good article here for your reference on how to dockerizing a Node.js web app.
Just update your Dockerfile to achieve your goal for more options see here:
# base image
FROM node:12.2.0
RUN npm config set strict-ssl false \
&& npm config set proxy http://proxy.xxxxxx.com:8080
# install chrome for protractor tests
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'
RUN apt-get update && apt-get install -yq google-chrome-stable
# set working directory
WORKDIR /app
# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH
# install and cache app dependencies
COPY package.json /app/package.json
RUN npm install
RUN npm install -g #angular/cli#7.3.9
# add app
COPY . /app
# start app
CMD ng serve --host 0.0.0.0
Give a shot for the following Dockerfile as well!
FROM node:alpine
# get the app
WORKDIR /src
# install packages
RUN npm ci
RUN npm install -g #angular/cli
COPY package.json .
RUN npm install
COPY . .
# start app
CMD ["ng", "serve", "-o"]
I have a simple node app with the following Dockerfile:
FROM node:8-alpine
WORKDIR /home/my-app
COPY package.json .
COPY ./app ./app
COPY ./server.js ./
RUN rm -rf node_modules
RUN npm install \
npm run build
EXPOSE 3000
When I build the image with: docker build -t my-app:latest ., I attempt to run the app and it complains that some modules are missing.
When I go into the container via docker run -i -t my-app:latest /bin/sh I can see that the packages have not been installed. After manually running npm install in the container, it seems to work.
I can only conclude that from this RUN npm install not being executed correctly inside the container.