How to install npm in docker container - docker

I have mistakenly uninstalled npm from my docker container and unable to install again.
wodby#php.container:/var/www/html $ node -v
v15.10.0
wodby#php.container:/var/www/html $ npm -v
/bin/sh: npm: not found
wodby#php.container:/var/www/html $ npm i npm#latest -g
/bin/sh: npm: not found
Even I have tried with root user.
wodby#php.container:/var/www/html $ sudo -i
1f1d27edb2a3:~# npm i npm#latest -g
-ash: npm: not found
Thanks in Advance!

Related

Docker Node image generates files with wrong user permissions

Problem
I'm using docker to generate my npm files. The problem is I get permissions issues when I do this. I have been using second alpine image to fix the permission issues. I want to speed up the process by generating the files with the correct permissions.
Attempt 1
Using the node user seemed to work at first as it persisted the user permissions on my device. When I tried it on a different device I got this error which I could not solve. The /local directory is where I'm mounting my npm directory. I also tried adding the group to the node user and got the same error.
Code
docker run -u "node" --rm -v $PWD:/local node:19.3.0 bash -c "cd local/mobile && npm install --legacy-peer-deps && npx ionic build && npx ionic cap copy"
Output
npm ERR! path /local/mobile/node_modules
npm ERR! errno -13 npm ERR!
Error: EACCES: permission denied, mkdir '/local/mobile/node_modules'
npm ERR! [Error: EACCES: permission denied, mkdir
'/local/mobile/node_modules']
{ npm ERR! errno: -13,
npm ERR!
code: 'EACCES', npm ERR! syscall: 'mkdir',
npm ERR! path:
'/local/mobile/node_modules' npm ERR! }
Attempt 2
I also tried using my local id and group id to run the dockerfile. I got a different error when using this technique.
Code
docker run -u "$(id -u):$(id -g)" --rm -v $PWD:/local node:19.3.0 bash -c "cd local/mobile && npm install --legacy-peer-deps && npx ionic build && npx ionic cap copy"
Output
npm ERR! code EACCES
npm ERR! syscall mkdir
npm ERR! path /.npm npm
ERR! errno -13
You can try specifying the user node. By default, the user "node" haven not have written permission the host machine
You try the following method
docker run --rm -v $PWD:/local node:19.3.0 bash -c "cd local/mobile && npm install --legacy-peer-deps && npx ionic build && npx ionic cap copy"

Docker: $[npm,: not found

I'm running a cypress test in a Docker container with the DockerFile below and my code is throwing the error:
#12 0.662 /bin/sh: 1: $[npm,: not found
I searched previous post about similar error, but I couldn't find any answer to my problem. How can I fix the issue?
DockerFile:
FROM cypress/base:16.13.0
RUN mkdir /app
WORKDIR /app
COPY . /app
RUN npm install
RUN $(npm bin)/cypress verify
RUN $["npm", "run", "cypress:e2e"]
.dockerignore:
node_modules
You must install npm to use npm. so you could do this
RUN apt install sudo curl
RUN curl -fsSL https://deb.nodesource.com/setup_17.x | sudo -E bash -
then you can use npm after this

gulp not running in Dockerfile: Local gulp not found in /

This is my Dockerfile:
RUN mkdir /mcvitty
COPY . /mcvitty
RUN cd /mcvitty &&\
npm install -g gulp#3.9.1 &&\
npm link gulp --force &&\
npm install jshint#2.9.5 &&\
npm install gulp-jshint#2.0.4 &&\
npm install gulp-sass#3.1.0 &&\
npm install gulp-concat#2.6.1 &&\
npm install gulp-uglify#3.0.0 &&\
npm install gulp-rename#1.2.2 &&\
npm install gulp-minify-css#1.2.4 &&\
npm install gulp-image-resize#0.13.0
RUN gulp
RUN gulp resize-images
Whenever I run my container I get the following error:
[08:35:34] Local gulp not found in /
[08:35:34] Try running: npm install gulp
The command '/bin/sh -c gulp' returned a non-zero code: 1
Adding
npm install gulp#3.9.1
to have a local gulp, as suggested by someone does not work either.
Any suggestion?
I have Gulp running in a container. The main differences are that I set a working directory:
RUN mkdir /mcvitty
WORKDIR /mcvitty
COPY . /mcvitty
I don't run npm link gulp, either.
If that doesn't work, remove the RUN gulp commands and add them to your docker run command, e.g.
docker run -it --rm --entrypoint '/bin/sh' myimage -c 'gulp && gulp images'
I fixed the problem by CD back into my dir before running gulp:
RUN cd /mcvitty && gulp

Docker container run exits immediately when mounting paths

I am running docker on windows 10. When I run this command without the -v flag to mount the host drive and file path to the container path, then the container runs fine and I can connect to it. However, when I provide the flag to mount the paths the container exits immediately. This is my command which runs without any error
docker container run -v c:/container-fs:/usr/src/app --publish 8001:8080 --detach --name bboard-ubuntu bulletinboard:Ubuntu
when I run the command docker container ls --all, I see that the container named bboard-ubuntu has exited almost immediately after it started up.
When trying to exec into the container using the command docker exec -it bboard-ubuntu /bin/bash, I get the error message as below:
Error response from daemon: Container
26a2d3361dfc0c890xxxxxxxxxxxxxxx97be532ab6e8771652e5b is not running
When I remove the mount flags and run it like this below, there are no issues and I can exec into the container file system.
docker container run --publish 8001:8080 --detach --name bboard-ubuntu bulletinboard:Ubuntu
How do I trace and fix this issue caused by providing the mount flag?
Edit
This is the Dockerfile
FROM ubuntu:18.04
WORKDIR /usr/src/app
COPY package.json .
# replace shell with bash so we can source files
RUN rm /bin/sh && ln -s /bin/bash /bin/sh
RUN apt-get update \
&& apt-get install -y curl \
&& apt-get -y autoclean
RUN apt-get install -y apt-utils
RUN apt-get -y install nano
# nvm environment variables
ENV NVM_DIR /usr/local/nvm
ENV NODE_VERSION 13.9.0
# install nvm
# https://github.com/creationix/nvm#install-script
RUN curl --silent -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.2/install.sh | bash
# install node and npm
RUN source $NVM_DIR/nvm.sh \
&& nvm install $NODE_VERSION \
&& nvm alias default $NODE_VERSION \
&& nvm use default
# add node and npm to path so the commands are available
ENV NODE_PATH $NVM_DIR/v$NODE_VERSION/lib/node_modules
ENV PATH $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH
# confirm installation
RUN node -v
RUN npm -v
RUN npm install
EXPOSE 8080
CMD [ "npm", "start" ]
COPY . .
Here is the error after removing detach
npm ERR! code ENOENT npm ERR! syscall open npm ERR! path
/usr/src/app/package.json npm ERR! errno -2 npm ERR! enoent ENOENT: no
such file or directory, open '/usr/src/app/package.json' 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/2020-02-26T19_02_33_143Z-debug.log
I am running these commands on windows host. Where do I find the /root/.npm/ log folder?

Unable to install appium on ubuntu 16.04 with npm

Showing below error while trying to install appium.
While doing sudo npm install -g appium.
npm ERR! Error: Invalid name: "#gulp-sourcemaps/map-sources"
npm ERR! at ensureValidName (/usr/lib/nodejs/normalize-package-data/lib/fixer.js:284:15)
npm ERR! at Object.module.exports.fixNameField (/usr/lib/nodejs/normalize-package-data/lib/fixer.js:199:5)
npm ERR! at /usr/lib/nodejs/normalize-package-data/lib/normalize.js:29:38
npm ERR! at Array.forEach (native)
npm ERR! at normalize (/usr/lib/nodejs/normalize-package-data/lib/normalize.js:28:15)
npm ERR! at final (/usr/lib/nodejs/read-package-json/read-json.js:310:33)
npm ERR! at then (/usr/lib/nodejs/read-package-json/read-json.js:124:33)
npm ERR! at /usr/lib/nodejs/read-package-json/read-json.js:234:40
npm ERR! at fs.js:268:14
npm ERR! at /usr/lib/nodejs/graceful-fs/graceful-fs.js:103:5
npm ERR! If you need help, you may report this log at:
npm ERR! <http://github.com/isaacs/npm/issues>
npm ERR! or email it to:
npm ERR! <npm-#googlegroups.com>
npm ERR! System Linux 3.13.0-137-generic
npm ERR! command "/usr/bin/nodejs" "/usr/bin/npm" "install" "-g" "appium"
npm ERR! cwd /home/mosheko
npm ERR! node -v v0.10.25
npm ERR! npm -v 1.3.10
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /home/mosheko/npm-debug.log
npm ERR! not ok code 0
It might be helpful. Steps to get appium working on ubuntu pc:
Install ruby: Paste the below command at terminal and hit enter
sudo apt-get install build-essential curl git m4 ruby texinfo libbz2-dev libcurl4-openssl-dev libexpat-dev libncurses-dev zlib1g-dev
Install linux brew:
Paste the below command at terminal and hit enter
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/linuxbrew/go/install)"
set path for brew: Type: gedit .bashrc at terminal and copy paste the following into the .bashrc file
export PATH="$HOME/.linuxbrew/bin:$PATH"
export MANPATH="$HOME/.linuxbrew/share/man:$MANPATH"
export INFOPATH="$HOME/.linuxbrew/share/info:$INFOPATH"
Install node: Paste the below commands one by one at terminal and hit enter
brew update
brew install node
brew link node
Install Appium:
npm install -g appium npm install wd
To start Appium: Paste the below command at terminal and hit enter
appium
Thanks
Install Java and check the Java version
Step 1 : Java -version
Install Node.js and npm from the Ubuntu repository.
Step 2 : sudo apt update
Step 3 : sudo apt install nodejs
Step 4 : nodejs --version
Out Put : v8.10.0 (version of the Node)
Step 5 : sudo apt install npm
Step 6 : npm --version (npm version).
Step 7 : 3.5.2
Step 8 : sudo npm install -g appium
Step 9 : npm install wd
Step 10 : npm install -g appium-doctor
To verify “Appium” installation we can use appium-doctor. For this, we have to install appium doctor using npm.
Step 11 : appium-doctor
Step 12 : Type “appium” into the terminal and press enter button.
Appium will start.

Resources