PNPM docker non-root user Permission Denied - javascript

I just found about pnpm today and it helped solve my issue with npm timing out on my installation which is amazing.
I've a problem with pnpm tho in the docker image.
Previously with just npm I had unprivileged user like so
FROM node:14.17.3-slim
# build args
ARG NPM_AUTH_TOKEN
ARG HOME=/home/app
ARG NPMRC_PATH=$HOME/.npmrc
# setup unprivileged user
RUN useradd -Umrd $HOME app
WORKDIR $HOME
USER app
# copy configuration
COPY --chown=app:app "bin" "bin"
COPY --chown=app:app "package.json" "package-lock.json" "webpack.config.js" ".babelrc" ./
RUN ./bin/write_npmrc && \
npm ci --production=false
ENV NODE_ENV=development
VOLUME ["$HOME/config", "$HOME/log", "$HOME/src"]
CMD ["npm", "start"]
EXPOSE 9000
But if I switch to the pnpn I'm no longer able to proceed with building the image due to Permission Denied and I need to use root user.
FROM node:14.17.3-slim
# build args
ARG NPM_AUTH_TOKEN
ARG HOME=/home/app
ARG NPMRC_PATH=$HOME/.npmrc
RUN apt-get update && apt-get install -y curl \
&& rm -rf /var/lib/apt/lists/*
RUN curl -f https://get.pnpm.io/v6.16.js | node - add --global pnpm
WORKDIR $HOME
# copy configuration
COPY "bin" "bin"
COPY "package.json" "pnpm-lock.yaml" "webpack.config.js" ".babelrc" ./
RUN ./bin/write_npmrc && \
pnpm install --frozen-lockfile
ENV NODE_ENV=development
VOLUME ["$HOME/config", "$HOME/log", "$HOME/src"]
CMD ["pnpm", "start"]
EXPOSE 9000
Is there a way so I can keep
# setup unprivileged user
RUN useradd -Umrd $HOME app
WORKDIR $HOME
USER app
With pnpm instead?

You just need to change to your non-privileged user after installing the system packages.
Example:
FROM node:14.17.3-slim
# build args
ARG NPM_AUTH_TOKEN
ARG HOME=/home/app
ARG NPMRC_PATH=$HOME/.npmrc
RUN apt-get update && apt-get install -y curl \
&& rm -rf /var/lib/apt/lists/*
RUN curl -f https://get.pnpm.io/v6.16.js | node - add --global pnpm
# setup unprivileged user
RUN useradd -Umrd $HOME app && \
chown -R app:app $HOME
WORKDIR $HOME
USER app
# copy configuration
COPY --chown=app:app "bin" "bin"
COPY --chown=app:app "package.json" "pnpm-lock.yaml" "webpack.config.js" ".babelrc" ./
RUN ./bin/write_npmrc && \
pnpm install --frozen-lockfile
ENV NODE_ENV=development
VOLUME ["$HOME/config", "$HOME/log", "$HOME/src"]
CMD ["pnpm", "start"]
EXPOSE 9000

Related

Docker - _moduleAliases not working in docker

i was created docker file, and when I am runing the image , I have a error "Error: Cannot find module 'mobilePath"
The package.json :
"_moduleAliases": {
"apiPath": "./api.js",
"sourcesPath": "./sources.js",
"mobilePath": "./test/Mobile.js"
}
docker file :
FROM node:14.15.3-buster
RUN npm install -g npm#latest
RUN npm --version
RUN npm install -g yarn#latest --force
RUN yarn --version
RUN apt-get update
RUN apt-get install -y fonts-liberation libappindicator3-1 xdg-utils
RUN mkdir -p /usr/src/app/ccv
WORKDIR /usr/src/app/ccv
COPY package*.json .
RUN npm install
COPY . /usr/src/app/ccv
RUN chmod +x suites/script.sh
ENTRYPOINT ["suites/script.sh"]
in local is working fine, why docker image cannot find _moduleAliases?

Chrome refuses to run inside of docker container on M1 when doing nightwatch tests

I have tried running a docker image, that previously worked on a non-M1 platform, to run nightwatch tests using Chromedriver and chromium. When I switched to an M1 mac, the first assertion of the test would fail, which implied that Chrome wasn't even starting up in the first place. I tried Chromium and google-chrome-stable, both of which had the same failure. I also tried running a VNC server inside of the docker container to see what is going on. When I got in and tried to start chrome, nothing happened. When I tried to start Chromium I got a message that it is not supported on the current hardware.
This is how I build my docker container :
FROM node:10-buster-slim
WORKDIR /home/node
# Installing some missing but critical items:
# Chromium info: https://www.chromium.org/
RUN \
apt-get update && \
useradd apps && \
mkdir -p /home/apps && \
chown apps:apps /home/apps && \
apt-get install -y \
libglib2.0-0 \
libnss3 \
libx11-6 \
wget \
x11vnc \
xvfb \
fluxbox \
wmctrl \
gnupg2 && \
apt-get clean && \
wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - && \
echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list && \
apt-get update && apt-get -y install google-chrome-stable
COPY docker-startup.sh /
RUN chmod 777 /docker-startup.sh
CMD '/docker-startup.sh'
# Copy only package.json and package-lock to start.
# Allows packages to to be cached in Docker layers, preventing the need to
# repeatedly re-install packages for any update to ui-tests.
COPY package.json package-lock.json ./
# Install testing dependencies (Nightwatch, Chromedriver)
# Use npm ci to install using package-lock.json, and not replace its contents.
# FYI: npm i chromedriver uses the version # from package.json
RUN \
npm ci && \
npm i -g nightwatch#0.9.21 && \
npm i -g chromedriver#latest --unsafe-perm=true --allow-root
# Copies code over to the default working directory:
# The below COPY assumes you are copying /ui-tests to /ui-tests
COPY . .
# Sets variable used in Nightwatch Reporter:
ENV TEST_ENV=docker
ENV TERM=xterm-256color
# Resolves socket error / ECON reset
ENV DBUS_SESSION_BUS_ADDRESS=/dev/null
There is a .sh script called docker-startup.sh that just sets up and runs the VNC server for me to be able to see what is going on, that is not too important in this case.
The command used to run the docker container is this
docker run --privileged -p 5900:5900 --user apps -e VNC_SERVER_PASSWORD=password --platform linux/amd64 -e APPBRANCH=$(git rev-parse --abbrev-ref HEAD) -v ~/work/dev/services/beast-portal/ui-tests/screenshots:/home/node/screenshots -v ~/work/dev/services/beast-portal/ui-tests/reports:/home/node/reports --env-file ./nightwatch.env nightwatch:stable nightwatch --env {--EnvironmentToTestIn--} --test {--TestToRun--.js}
This runs the docker container and then runs the nightwatch test with a given environment and a given test file.
The command used to build the docker file is this :
cd ~/work/dev/services/beast-portal/ui-tests && docker build --platform linux/amd64 -t \"nightwatch:stable\" .
I have also tried messing with the versions of Chromedriver and nightwatch but that still gave the same error where it fails to verify the first assertion as chrome isn't even started from the looks of it. This feels like an M1 architecture issue but I can't figure it out.

package.json not found in node:13-alpine docker container

I made 2 docker images from 2 folder in this repo. The dockerfile was:
FROM node:13-alpine
EXPOSE 5000
RUN mkdir /app
RUN mkdir -p /home/app
COPY . /home/app
RUN npm install
CMD ["npm","run","start"]
But the docker logs showed that the package.json file was not found and hence the containers closed, I can't even enter the terminal of the containers to see the available files.
The error output is in the below image.
To reproduce the error, clone the repo and run sudo docker-compose -f docker-compose.yaml up
You didn't specify the working directory in your dockerfile and that's why the package.json cannot be found.
Ref: https://docs.docker.com/engine/reference/builder/#workdir
add WORKDIR before RUN npm install
WORKDIR /home/app
RUN npm install

Install yarn in a docker container says missing dependency

I'm using the node:6.7.0 image as my docker container and then follow the installation guide for yarn
sudo apt-key adv --keyserver pgp.mit.edu --recv D101F7899D41F3C3
echo "deb http://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
Then I do
apt-get update && apt-get install yarn
But at this point I get an error message which says
yarn : Depends: nodejs (>= 4.0.0) but it is not going to be installed
I've echoed node -v before the install and it also says 6.7.0
Anything that I'm missing?
robertklep is right - if you check the Dockerfile for Node you'll see they install Node by downloading the TAR, not through APT. You can check this by running an interactive container:
> docker run -it node:6.7.0 bash
root#465fa07437c9:/# dpkg -s nodejs
dpkg-query: package 'nodejs' is not installed and no information is available
You can use NPM in your Dockerfile instead:
FROM node:6.7.0
RUN npm install -g yarn

How to cache node_modules on Docker build?

I've been trying for some time to cache node_modules on a Docker build. I've tried several approaches including the one here, but without success.
My main reason to cache is because it takes 30+ minutes to build my image, which is way too much.
My Dockerfile:
# This image will be based on the oficial nodejs docker image
FROM node:4.2.1
RUN npm install -g jspm#0.17.0-beta.7 && \
npm install -g gulp && \
npm install -g tsd
# Use changes to package.json to force Docker not to use the cache
# when we change our application's nodejs dependencies:
ADD package.json /src/package.json
RUN cd /src && npm install
# Put all our code inside that directory that lives in the container
ADD . /src
# Set in what directory commands will run
WORKDIR /src
# Install dependencies
RUN cd /src && \
tsd reinstall -so && \
jspm install && \
gulp build -p
# Tell Docker we are going to use this port
EXPOSE 3000
# The command to run our app when the container is run
CMD ["npm", "run", "start-production"]
I do not have a .dockerignore file. I added one before but it still didn't cache my node_modules.
So, how to I cache my node_modules? Feel free to suggest modifications to the Dockerfile.
Thanks!
I'm not sure whether it is the root of the error, but try to specify the destination folder in the ADD command and not the destination file.
ADD package.json /src
Moreover, you can use COPY instead of ADD (ADD can work with url and archives but you don't need it here).
You can also specify your working directory earlier in the file.
Try with this code :
# This image will be based on the official nodejs docker image
FROM node:4.2.1
RUN npm install -g jspm#0.17.0-beta.7 && \
npm install -g gulp && \
npm install -g tsd
# Set in what directory commands will run
WORKDIR /src
# Use changes to package.json to force Docker not to use the cache
# when we change our application’s nodejs dependencies:
COPY package.json ./
RUN npm install
# Put all our code inside that directory that lives in the container
COPY . ./
# Install dependencies
RUN tsd reinstall -so && \
jspm install && \
gulp build -p
# Tell Docker we are going to use this port
EXPOSE 3000
# The command to run our app when the container is run
CMD ["npm", "run", "start-production"]

Categories