Docker container that pulls from private gilab repository - node.js

I'm building a Docker container for my Node.js + Vue application.
Since I have a global css library in another repository I have added this line in my package.json file:
"lib-css": "git+ssh://git#git.lib.com:9922/username/lib-css.git#development",
That way when I run npm install I install also my CSS library. The problem is that on my local env it asks for my password and I can insert it, but in the Docker build the process fails with the following error:
Step 7/10 : RUN npm install
---> Running in db10ca83586d
npm WARN deprecated babel-preset-es2015#6.24.1: 🙌 Thanks for using Babel: we recommend using babel-preset-env now: please read babeljs.io/env to update!
npm ERR! Error while executing:
npm ERR! /usr/bin/git ls-remote -h -t ssh://git#git.lib.com:9922/username/lib-css.git
npm ERR!
npm ERR! Host key verification failed.
npm ERR! fatal: Could not read from remote repository.
npm ERR!
npm ERR! Please make sure you have the correct access rights
npm ERR! and the repository exists.
npm ERR!
npm ERR! exited with error code: 128
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2017-12-11T08_49_11_152Z-debug.log
This is my current Dockerfile:
FROM node:carbon
WORKDIR /usr/src/app
RUN mkdir -p /root/.ssh
COPY .secrets /root/.ssh/id_rsa
RUN chmod 700 /root/.ssh && chmod 600 /root/.ssh/*
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm#5+)
COPY package*.json ./
RUN npm install
# If you are building your code for production
# RUN npm install --only=production
# Bundle app source
COPY . .
EXPOSE 8081
CMD [ "npm", "run dev" ]
~
My .secrets file contains my private key associated to the repository.
How can I make this works?

Related

Installing a local npm package in Dockerfile

I have a local package that I packed into an npm package using npm pack.
I then install this package to some nodejs apps that are external to this local package using npm install path/to/package.tgz. This works like a charm, but now I want to dockerize the apps and I can't manage to install the local package.
My structure looks like this:
-my_package
|-package.tgz
-app1
|-app1.js
|-Dockerfile
-app2
|-app2.js
|-Dockerfile
My Dockerfile looks like this:
FROM node:16
WORKDIR /app
# Copy and download dependencies
COPY package.json .
RUN npm install
# Copy the source files into the image
COPY . .
EXPOSE 4002
CMD ["npm", "start"]
And the error I'm getting when running docker build . -t js/app1 is:
npm WARN tarball tarball data for #my_package#file:/package.tgz (sha512-u9tY/j1VOzO1y1RpcCgYteDOEsh7TaSMYwmR2Rs7hoJopE11qa1XcnrrMKNx1/H/aHsZ3Gr0bOMx1SygYTf/rg==) seems to be corrupted. Trying again.
npm WARN tarball tarball data for #my_package#file:/package.tgz (sha512-u9tY/j1VOzO1y1RpcCgYteDOEsh7TaSMYwmR2Rs7hoJopE11qa1XcnrrMKNx1/H/aHsZ3Gr0bOMx1SygYTf/rg==) seems to be corrupted. Trying again.
npm notice
npm notice New major version of npm available! 8.19.2 -> 9.2.0
npm notice Changelog: <https://github.com/npm/cli/releases/tag/v9.2.0>
npm notice Run `npm install -g npm#9.2.0` to update!
npm notice
npm ERR! code ENOENT
npm ERR! syscall open
npm ERR! path /my_package/package.tgz
npm ERR! errno -2
npm ERR! enoent ENOENT: no such file or directory, open '/my_package/package.tgz'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2022-12-09T04_54_04_868Z-debug-0.log
The command '/bin/sh -c npm ci' returned a non-zero code: 254
Some times I get null instead of the sha512 stuff, otherwise the error is the same.
Is there anything I can do to fix this?
Thanks!
The documentation says:
COPY obeys the following rules:
The path must be inside the context of the build; you cannot
COPY ../something /something, because the first step of a docker build
is to send the context directory (and subdirectories) to the docker
daemon.
The easiest fix is to paste the file(s) you need into the same folder that your Dockerfile is in.

unable to run npm install in docker image (getting auth error)?

I have a very simple Dockerfile
FROM node:17.3.1 as build
WORKDIR /app
COPY package*.json .
RUN npm install
COPY . .
RUN npm run build
FROM node:17.3.1
WORKDIR /app
COPY package.json .
RUN npm install --only=production
COPY --from=build /app/dist ./dist
CMD npm run start:prod
When running docker build -t nestjs-hello-world . I am getting the following error. I don't understand why it is needing to login to npm. It is using the default registry. Even tried specifying the default registry as part of the npm install command, just to make sure, but same error..
=> ERROR [build 4/6] RUN npm install 8.3s
------
> [build 4/6] RUN npm install:
#8 8.230 npm notice
#8 8.231 npm notice New minor version of npm available! 8.1.2 -> 8.3.1
#8 8.231 npm notice Changelog: <https://github.com/npm/cli/releases/tag/v8.3.1>
#8 8.232 npm notice Run `npm install -g npm#8.3.1` to update!
#8 8.233 npm notice
#8 8.235 npm ERR! code E401
#8 8.237 npm ERR! Unable to authenticate, your authentication token seems to be invalid.
#8 8.238 npm ERR! To correct this please trying logging in again with:
#8 8.239 npm ERR! npm login
#8 8.251
#8 8.252 npm ERR! A complete log of this run can be found in:
#8 8.252 npm ERR! /root/.npm/_logs/2022-01-20T00_08_48_935Z-debug.log
Any ideas why this is happening for me please?
Thanks
Thanks to Phil I had the same problem and I managed to unblock my problem thanks to your comment
Here is my Dockerfile
FROM node:16
# Create app directory
WORKDIR /usr/src/app
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm#5+)
# copying packages first helps take advantage of docker layers
COPY package.json .
RUN npm install prettier -g
# If you are building your code for production
RUN npm install
# Bundle app source
COPY . .
RUN npm run build
EXPOSE 4000
CMD [ "node", "dist/server.js" ]

How can I run jshint in my CI using Docker

I am trying to create a task in our Azure pipeline to validate our javascript.
We have a node container which performs an npm install when spun up:
node:
image: node:12-alpine
user: "node"
working_dir: /home/node/app
environment:
- NODE_ENV=development
volumes:
- ./:/home/node/app
expose:
- "8081"
command: "npm install"
To perform my task I have created a make command in the Makefile:
js-check: ## Run Jshint
docker-compose run node npm install && npm run jshint
Which I then call in the build job as follows:
- script: make js-check
displayName: 'Run JSHint'
Locally when I call the make js-check it performs the install, followed by the jshint which outputs 0 vulnerabilities found. However when the pipeline reaches this task remotely it fails claiming missing write access to /home/node/app
npm WARN checkPermissions Missing write access to /home/node/app
npm ERR! code EACCES
npm ERR! syscall access
npm ERR! path /home/node/app
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, access '/home/node/app'
npm ERR! [Error: EACCES: permission denied, access '/home/node/app'] {
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'access',
npm ERR! path: '/home/node/app'
npm ERR! }
npm ERR!
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR!
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator.
Your Makefile runs two commands; the shell interprets the && marker before it gets to Docker. That command is equivalent to:
js-check: ## Run Jshint
docker-compose run node npm install
npm run jshint # (without Docker)
It looks like your environment already has Node installed. You need to resolve the permissions issues (generally the CI system will check out source trees as a user that can run commands), and then you can use this native Node:
js-install: package.json
npm install
js-check: js-install
npm run jshint
This has the advantage of only depending on normal Javascript development tools; you don't need the extra docker-compose.yml file or administrator privileges just to run your unit tests.
If you really need to run this in Docker, you can either run this as two separate commands, or make the single container command be a shell that can interpret the && itself:
js-install1: package.json docker-compose.yml
docker-compose run node npm install
js-check1: js-install1
docker-compose run node npm run jshint
js-check2: package.json docker-compose.yml
docker-compose run node \
sh -c "npm install && npm run jshint"
Above error is because /home/node directory is owned by the node user in the default node image. The /app directory is created and owned by root. See this open issue about above error for more information.
I reproduced the same error with your compose file. When i changed the user to root. The error was gone.
So you can try changing the user to root instead of node in your compose file.
node:
image: node:12-alpine
user: "root"
working_dir: /home/node/app
As David pointed out, you also need to change your makefile to docker-compose run node sh -c "npm install && npm run jshint". if you want to run the commands in docker.
Another workaround is to build and run your container from a dockerfile instead of the compose file. See below simple example dockerfile.
from node:12-alpine
ENV NODE_ENV=development
RUN mkdir -p /home/node/app
RUN chown -R node:node /home/node/app
USER node
WORKDIR /home/node/app
COPY . ./
RUN npm install
CMD [ "npm", "run", "jshint" ]
Then change the Makefile like example:
js-check: ## Run Jshint
docker build --tag nodejshint:1.0 . && docker run --detach --name jshintContainer nodejshint:1.0

Docker image creation throwing "no such file or directory, access '/node_modules/sails'" when dockerizing a sails.js app

Below is the Dockerfile
FROM node:latest
RUN npm install -g sails#0.12.13
ADD . / ./
RUN npm install
EXPOSE 80
CMD (sails lift)
Image creation fails with following log:
ending build context to Docker daemon 70.03MB
Step 1/6 : FROM node:latest
---> 60bea5b86079
Step 2/6 : RUN npm install -g sails#0.12.13
---> Using cache
---> 3f3c7fcdb090
Step 3/6 : ADD . / ./
---> Using cache
---> 78700b41cf26
Step 4/6 : RUN (npm install)
---> Running in d49423611a77
npm info it worked if it ends with ok
npm info using npm#5.3.0
npm info using node#v8.4.0
npm info lifecycle ecs-notification#0.0.0~preinstall: ecs-notification#0.0.0
npm WARN checkPermissions Missing write access to /node_modules/sails
npm ERR! path /node_modules/sails
npm ERR! code ENOENT
npm ERR! errno -2
npm ERR! syscall access
npm ERR! enoent ENOENT: no such file or directory, access '/node_modules/sails'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2017-09-08T13_00_06_956Z-debug.log
The command '/bin/sh -c (npm install)' returned a non-zero code: 254
Even if I try to create the directory, or change the permission to 777 using:
RUN (mkdir -p /node_modules/sails; chmod 777 /node_modules/sails)
it still fails with the same error:
Sending build context to Docker daemon 70.03MB
Step 1/7 : FROM node:latest
---> 60bea5b86079
Step 2/7 : RUN npm install -g sails#0.12.13
---> Using cache
---> 3f3c7fcdb090
Step 3/7 : RUN (mkdir -p /node_modules/sails; chmod 777 /node_modules/sails)
---> Using cache
---> c7f1784c24c8
Step 4/7 : ADD . / ./
---> Using cache
---> 334017659dde
Step 5/7 : RUN (npm install)
---> Running in 833e3ef6a010
npm info it worked if it ends with ok
npm info using npm#5.3.0
npm info using node#v8.4.0
npm info lifecycle ecs-notification#0.0.0~preinstall: ecs-notification#0.0.0
npm WARN checkPermissions Missing write access to /node_modules/sails
npm ERR! path /node_modules/sails
npm ERR! code ENOENT
npm ERR! errno -2
npm ERR! syscall access
npm ERR! enoent ENOENT: no such file or directory, access '/node_modules/sails'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2017-09-08T13_13_55_258Z-debug.log
The command '/bin/sh -c (npm install)' returned a non-zero code: 254
Docker version: 17.06.2-ce-mac27 (19124)
Any pointers around how I can debug this?
Change your Dockerfile to below
FROM node:latest
RUN npm install -g sails#0.12.13
WORKDIR /usr/app
COPY package.json ./
RUN npm install
COPY . ./
EXPOSE 80
CMD sails lift
You should only copy package.json first and the do npm install and then copy code. Also your ADD statement was wrong. There was an extra space
Next make sure you have .dockerignore which ignores the node_modules. Else node_modules will be overwritten by the copy operation

Docker running test with nodejs and volume

I'm learning docker and I'm having some troubles with the volume in a development environment with Nodejs.
I having the following simple dockerfile that aims to start my unit tests from a NodeJS parent image:
FROM node:4-onbuild
VOLUME ["/usr/src/app"]
CMD [ "npm", "test" ]
I'm running my container this way:
docker run -v /C/Users/myUser/dockertest:/usr/src/app notest
But I keep receiving the following error:
npm info it worked if it ends with ok
npm info using npm#2.15.9
npm info using node#v4.5.0
npm ERR! Linux 4.4.15-moby
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "test"
npm ERR! node v4.5.0
npm ERR! npm v2.15.9
npm ERR! path /usr/src/app/package.json
npm ERR! code ENOENT
npm ERR! errno -2
npm ERR! syscall open
npm ERR! enoent ENOENT: no such file or directory, open '/usr/src/app/package.json'
npm ERR! enoent This is most likely not a problem with npm itself
npm ERR! enoent and is related to npm not being able to find a file.
npm ERR! enoent
npm ERR! Please include the following file with any support request:
npm ERR! /usr/src/app/npm-debug.log
I don't understand why I'm not able to find the package.json file inside of the /usr/src/app folder inside of my container.
The fact is that if I'm using the following docker file :
FROM node:4-onbuild
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
CMD [ "npm", "test" ]
My tests will be running in the container, but only after building step (so I have to build each time I make a modification...) . I need to play these tests any time I want, by launching a docker command (restart ? exec ?) quickly
Thanks for your help
You want to COPY everything build relevant and link your codebase on containerstart.
FROM node:4.4
# Enviroment variables
ENV HOMEDIR /data
RUN mkdir -p ${HOMEDIR}
WORKDIR ${HOMEDIR}
# install all dependencies
COPY package.json ./
RUN npm install
# add node content initially
COPY . .
CMD ["npm", "test"]
And then link it on start:
version: '2'
services:
myApp:
build:
context: .
dockerfile: Dockerfile
image: "myApp"
volumes:
- .:/data
You should now be able to change your codebase without restarting/rebuilding your container.

Resources