I have simple node.app, with this package.json:
{
"name": "docker_web_app",
"version": "1.0.0",
"description": "Node.js on Docker",
"author": "xxxxx",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"dependencies": {
"express": "^4.16.1",
"myuser-deploy": "git+ssh://git#github.com:myuser/deploy.git"
}
}
Normally when I run "npm i", it installs all dependencies, and also the one form the github. I have SSH that I previously setup with the github. All works fine so far.
Now I added Docker support for my application, like this
Dockerfile:
FROM node:carbon
# 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+)
COPY package*.json ./
RUN npm install
# If you are building your code for production
# RUN npm install --only=production
# Bundle app source
COPY . .
EXPOSE 8080
CMD [ "npm", "start" ]
docker-compose.yml:
version: "3"
services:
test-service:
build: .
image: test-service
container_name: test-service
working_dir: /usr/src/app
user: node
restart: always
ports:
- '3001:3000'
When I run docker-compose up, it gives me this error:
> npm ERR! Error while executing: npm ERR! /usr/bin/git ls-remote -h -t
> ssh://git#github.com/myuser/deploy.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:
What can I do with the docker, so it uses my SSH keys?
Related
I am new to Docker and I am trying to run a container following this tutorial and was running into some issues while using the --volume or -v as shown below:
docker run -v <absolute path to host directory>:<absolute path to container working directory> <image id>
So mine looks like this:
docker run -p 3000:3000 -v /usr/app/node_modules -v "$(pwd)":/usr/app 900a3497fc39
The error thrown is this:
npm ERR! code EACCES
npm ERR! syscall open
npm ERR! path /usr/app/package.json
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, open '/usr/app/package.json'
npm ERR! [Error: EACCES: permission denied, open '/usr/app/package.json'] {
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'open',
npm ERR! path: '/usr/app/package.json'
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.
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2020-11-12T13_39_38_626Z-debug.log
I have built the image without any issues:
docker build -f Dockerfile.dev .
Sending build context to Docker daemon 23.55kB
Step 1/6 : FROM node
---> f1974cfde44f
Step 2/6 : WORKDIR /usr/app
---> Using cache
---> 120da5dd5fa4
Step 3/6 : COPY ./package.json ./
---> Using cache
---> 040ba41014c8
Step 4/6 : RUN npm install
---> Using cache
---> 96315e12e5d0
Step 5/6 : COPY . .
---> db93b8725037
Step 6/6 : CMD [ "npm", "run", "dev" ]
---> Running in 9c9d58318420
Removing intermediate container 9c9d58318420
---> 900a3497fc39
Successfully built 900a3497fc39
My Dockerfile.dev looks like this:
FROM node
WORKDIR /usr/app
COPY ./package.json ./
RUN npm install
COPY . .
CMD [ "npm", "run", "dev" ]
And the package.json looks like this:
{
"name": "vite-counter",
"scripts": {
"dev": "vite",
"build": "vite build"
},
"dependencies": {
"vue": "^3.0.0-beta.15"
},
"devDependencies": {
"vite": "^1.0.0-beta.3",
"#vue/compiler-sfc": "^3.0.0-beta.15"
}
}
From what I understand, I do not have sufficient permissions in the container to run this. This is exactly what I was following in the tutorial. What am I doing wrong and how do I fix this?
I'm running a very simple Node.js Koa "hello world" application on Docker 2.4.0 on Windows. If the base image is node:10-alpine, it works well:
FROM node:10-alpine
RUN mkdir -p /home/node/app/node_modules && chown -R node:node /home/node/app
WORKDIR /home/node/app
COPY package*.json ./
USER node
RUN npm install
COPY --chown=node:node . .
EXPOSE 8080
CMD [ "node", "index.js" ]
But if I change the version to FROM node:15.0.1-alpine3.10
Step 6/9 : RUN npm install
Removing intermediate container 6ff212db1484
---> bfde467f103d
Step 6/9 : RUN npm install
---> Running in 37ef90ef4039
npm ERR! code EACCES
npm ERR! syscall open
npm ERR! path /home/node/app/package-lock.json
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, open '/home/node/app/package-lock.json'
npm ERR! [Error: EACCES: permission denied, open '/home/node/app/package-lock.json'] {
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'open',
npm ERR! path: '/home/node/app/package-lock.json'
npm ERR! }
........
npm install will try to update the package-lock.json file with the current actual versions of the packages it's installed. For installation in a Docker image, you don't really want this; use npm ci instead to avoid this.
The other problem you're encountering is that COPY by default makes files owned by the root user, but you've switched to an alternate "node" user. You probably want the application code in your image to be owned by root, and then run as an alternate user: if there's some sort of security issue, this gives you an extra layer of protection against the code in the container getting modified.
If you do both of these things, the corrected Dockerfile would roughly look like:
FROM node:15.0.1-alpine3.10
# WORKDIR also creates the directory. It will be owned by root,
# which is probably what you want. (So no `RUN mkdir ...`.)
WORKDIR /home/node/app
# Stay as the root user for now.
# Install packages:
COPY package*.json ./
RUN npm ci # not `npm install`
# Copy in the rest of the application (still owned by root):
COPY . .
# Declare runtime metadata. Only now switch to the "node" user.
# This will not be able to modify the source code (good).
USER node
EXPOSE 8080
CMD ["node", "index.js"]
I am trying inject my ssh on build process
docker build --ssh default=C:\Users\***\.ssh\id_rsa .
// package.json
"airbase-common": "git+ssh://git#bitbucket.org/******/airbase-common.git"
// error
npm ERR! Error while executing:
npm ERR! /usr/bin/git ls-remote -h -t ssh://git#bitbucket.org/******/airbase-common.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.
Thanks in advance
I found solution:
If anyone as i am bulding his image in windows environment you should to keep .ssh folder as neighbor your Dockerfile
// Dockerfile
FROM node:12.16 AS builder
WORKDIR /app
COPY ./package.json ./
RUN mkdir /root/.ssh/
COPY .ssh /root/.ssh
RUN chmod 700 /root/.ssh/
RUN chmod 644 /root/.ssh/id_rsa.pub
RUN chmod 600 /root/.ssh/id_rsa
VOLUME [ "/root/.ssh" ]
RUN npm install
.... etc
I am working on a Nest.js application and this is the Dockerfile that we have. When I run this I am getting an error on the npm run build step in docker.
This is the build task in package.json.
"build": "nest build"
sh: nest: not found
npm ERR! code ELIFECYCLE
npm ERR! syscall spawn
npm ERR! file sh
npm ERR! errno ENOENT
npm ERR! atom-qbuilder-api#0.0.1 build: `nest build`
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the atom-qbuilder-api#0.0.1 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
Dockerfile
# Build
FROM node:8-alpine as builder
COPY package.json /usr/src/atom/package.json
COPY package-lock.json /usr/src/atom/package-lock.json
WORKDIR /usr/src/atom
# Install dependencies
RUN npm install --production --loglevel warn
ARG APPLICATION
ARG BRANCH
ARG BUILD_NUMBER
ARG SHA
ENV NODE_ENV qa
ENV VERSION 1.0.0
## Copy app directory in container and set workdir
COPY . /usr/src/atom
# Build & Test Coverage
RUN set -x \
&& npm run build \
&& echo "{ \"status\": \"Ok\", \"result\": { \"version\": \"$VERSION\", \"branchName\": \"$BRANCH\", \"SHA\": \"$SHA\", \"buildDate\": \"$(date)\", \"buildNumber\": \"$BUILD_NUMBER\", \"environment\": \"$NODE_ENV\" } }" > build.json
# Release
FROM node:8-alpine
ENV PORT 3000
COPY --from=builder /usr/src/atom/node_modules /usr/src/atom/node_modules
COPY --from=builder /usr/src/atom/dist /usr/src/atom/dist
COPY --from=builder /usr/src/atom/package.json /usr/src/atom/package.json
COPY --from=builder /usr/src/atom/build.json /usr/src/atom/build.json
WORKDIR /usr/src/atom
EXPOSE $PORT
## Run supervisor as foreground process
CMD bash -c "npm run start:prod && /usr/bin/supervisord"
#nestjs/cli (where the nest command comes from) is by default in devDependencies. Unless you've modified your deps in package.json, running npm i --production will not install #nestjs/cli which will lead to the error you currently have. You can either use a multi-staged dockerfile or move #nestjs/cli to your dependencies instead of devDeps.
I appreciate your help!
I have the following Dockerfile:
FROM node:slim
RUN mkdir -p /project
WORKDIR /project
# workdir is empty
RUN ls -a
COPY . /project
# workdir is full
RUN ls -a
# this command create node_modules folder on it own
RUN npm install
# I have node_modules folder with packages
RUN ls -a
RUN ls -d node_modules/*
Last outputs are:
Step 8/9 : RUN ls -a
---> Running in 90d8793f491a
.
..
.babelrc
.editorconfig
.eslintignore
.eslintrc.js
.git
.gitignore
.postcssrc.js
Dockerfile-dev
README.md
build
config
docker-compose.dev.yml
index.html
node_modules
package-lock.json
package.json
src
static
test
Removing intermediate container 90d8793f491a
---> b563d272b270
Step 9/9 : RUN ls -d node_modules/*
---> Running in a14bb4024e5e
node_modules/#babel
node_modules/#types
node_modules/abab
node_modules/abbrev
node_modules/accepts
node_modules/acorn
But when I look at the container's files from the following command:
docker-compose -f docker-compose.dev.yml run project ls
I have no node_modules folder (folder with dependencies) and my container cannot start because of it:
docker-compose -f docker-compose.dev.yml up
sh: 1: webpack-dev-server: not found
npm ERR! file sh
npm ERR! code ELIFECYCLE
npm ERR! errno ENOENT
npm ERR! syscall spawn
npm ERR! project#1.0.0 dev: `webpack-dev-server --inline --progress --config build/webpack.dev.conf.js`
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the project#1.0.0 dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm WARN Local package.json exists, but node_modules missing, did you mean to install?
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2018-04-11T19_09_35_669Z-debug.log
The docker-compose is simple:
version: '3'
services:
project:
container_name: project
build:
context: .
dockerfile: Dockerfile-dev
command: npm run dev
volumes:
- .:/project
ports:
- 8080:8080
The questions are: why it happens? How to solve the problem?
why it happens?
https://github.com/moby/moby/issues/4361#issuecomment-167156818
How to solve the problem?
https://github.com/moby/moby/issues/4361#issuecomment-210231967