How do I install npm package into Cypress container? - docker

I'm trying to install randomstring package into Cypress container because my tests are using it.
Dockerfile:
FROM cypress/base:17.8.0
RUN npm install randomstring#1.2.2
RUN npm install cypress#7.0.0
COPY . /e2e
RUN npm cypress run
error output I'm getting:
1 error occurred:
* Status: The command '/bin/sh -c npm install randomstring#1.2.2 cypress#7.0.0' returned a non-zero code: 1, Code: 1
I'm aware that Cypress/base image comes with operating system and dependencies required in order to run Cypress but I'm not sure if npm is included.
What is the right way to install packages into Cypress container ?

Any particular reason to use base image?
Have you tried with cypress/included:7.0.0, which already has cypress + browsers installed.
Furthermore it's better to keep all your dependencies (randomstring included) in package.json file where they belong, copy it over and call npm install or npm ci.

Related

How to resolve "The cypress npm package is installed, but the Cypress binary is missing."

I'm trying to download and install Cypress within GitLab CI runner and getting this error output:
The cypress npm package is installed, but the Cypress binary is missing.
We expected the binary to be installed here: /root/.cache/Cypress/4.8.0/Cypress/Cypress
Reasons it may be missing:
- You're caching 'node_modules' but are not caching this path: /root/.cache/Cypress
- You ran 'npm install' at an earlier build step but did not persist: /root/.cache/Cypress
Properly caching the binary will fix this error and avoid downloading and unzipping Cypress.
Alternatively, you can run 'cypress install' to download the binary again.
I ran the suggested command cypress install but it didn't help.
Next it says You're caching 'node_modules' but are not caching this path: /root/.cache/Cypress
I don't understand how you can cache the modules and leave out the path to it.
Next is You ran 'npm install' at an earlier build step but did not persist I did have npm install in earlier builds so I replaced it with npm ci as it's recommended in Cypress official docs in such cases.
No resolution though.
Here are relevant lines where the error occurs:
inside of Dockerfile:
COPY package.json /usr/src/app/package.json
COPY package-lock.json /usr/src/app/package-lock.json
RUN npm ci
inside the test runner:
docker-compose -f docker-compose-prod.yml up -d --build
./node_modules/.bin/cypress run --config baseUrl=http://localhost
inside the package.json:
{
"name": "flask-on-docker",
"dependencies": {
"cypress": "^4.8.0"
}
}
Can anyone point me in a right direction ?
You probably are running npm install and cypress run in two different stages. In this case, cypress cache could not be persisted, So it is recommended to use CYPRESS_CACHE_FOLDER option while running install and as well as cypress run/open. The command will looks like this,
CYPRESS_CACHE_FOLDER=./tmp/Cypress yarn install
CYPRESS_CACHE_FOLDER=./tmp/Cypress npx cy run [--params]
This helped me (Windows):
.\node_modules\.bin\cypress.cmd install --force
Or if you're using a UNIX system:
./node_modules/.bin/cypress install --force
https://newbedev.com/the-cypress-npm-package-is-installed-but-the-cypress-binary-is-missing-591-code-example
yarn cypress install --force before running of tests worked for me
I had the same problem
I run this code to grant jenkins user to be the owner of my cypress project folder
and after that everything was ok.
sudo chown -R jenkins: /your cypress project path/

"npm install" fails with ECONNREFUSED while building Docker image

I'm trying to build a Docker image that contains NPM and installs some tools, but when I issue the install command, as in:
RUN npm install -g sfdx-cli
The build hangs for a while, and then a lot of errors are thrown. And it is the same error:
npm ERR! fetch failed http://10.252.156.164:4880/#babel%2fcode-frame/-/code-frame-7.0.0.tgz
npm WARN retry will retry, error on last attempt: Error: connect ECONNREFUSED 10.252.156.164:4880
Every other resource I find on the web by searching this error results in an answer/article about using NPM behind a proxy, but that is not the case here. I'm not behind a proxy.
What can I do to make this error stop?
Running RUN echo "${http_proxy}" && echo "${HTTP_PROXY}" in my Dockerfile while building I get the following output:
Step 8/16 : RUN echo "${http_proxy}" && echo "${HTTP_PROXY}"
---> Running in 09bfc89592ae
Removing intermediate container 09bfc89592ae
I am able to build the docker image successfully, here is my Dockerfile,
FROM alpine:3.8
RUN apk add --no-cache nodejs npm
RUN npm install -g sfdx-cli
If you do not want to alpine as a base image then share your Dockerfile.
Or also you can run RUN npm config set registry https://registry.npmjs.com/
Try docker build . --network="host"
(Ref: https://github.com/verdaccio/docker-examples/issues/33)

aspnetcore - angular universal in a docker container

I am trying to create a docker image that I can pack an aspnetcore2.0/angular-universal application and due to my insufficient docker experience I been keep running into issues. I could really use some help.
Here is the dockerfile content:
FROM microsoft/aspnetcore-build:2.0 AS build-env
WORKDIR /app
COPY *.csproj ./
RUN npm cache clean --force
RUN npm install npm#latest
RUN npm install #angular/cli#latest
RUN npm install #ngtools/webpack#next
RUN node -v
RUN dotnet restore
COPY . ./
RUN dotnet publish -c Release -o out
FROM microsoft/microsoft/aspnetcore:2.0
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT [ "dotnet", "net-streetStyleCrew.dll" ]
since the aspnetcore-build:2.0 comes with a too old npm/node it has to be updated. And it have not go to angular-cli part, but i assume that need to be fresh as well of course.
And here is the trouble that I am running into now, and i have no clue how to resolve what appears to be a network issue inside the container when attempts to update:
Step 5/15 : RUN npm install npm#latest
---> Running in 72531196fc83
npm ERR! Windows_NT 10.0.16299
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "install" "npm#latest"
npm ERR! node v6.13.0
npm ERR! npm v3.10.10
npm ERR! code ENOTFOUND
npm ERR! errno ENOTFOUND
npm ERR! syscall getaddrinfo
npm ERR! network getaddrinfo ENOTFOUND registry.npmjs.org registry.npmjs.org:443
npm ERR! network This is most likely not a problem with npm itself
npm ERR! network and is related to network connectivity.
npm ERR! network In most cases you are behind a proxy or have bad network settings.
npm ERR! network
npm ERR! network If you are behind a proxy, please make sure that the
npm ERR! network 'proxy' config is set properly. See: 'npm help config'
npm ERR! Please include the following file with any support request:
npm ERR! C:\app\npm-debug.log
The command 'cmd /S /C npm install npm#latest' returned a non-zero code: 1
I am trying to run this on a windows container, because I don't have a whole lot of experience with Linux either.
I am absolutely open to any suggestion as well that could improve my approach to the basic concept. Thanks in advance.
Firstly, I think the specific issue is not a Docker related issue. This is a network related issue. Maybe this SO thread will help.
Regarding your Dockerfile:
The official best practice recommends reducing the number of layers. Each RUN command creates a layer. At the same time, you may want to have multiple RUN command to improve readability and take the benefit of caching. So, you need to find a balance between the two. In this particular case, I think you should chain the npm commands in a single RUN statement.
Also, I think instead of using the latest version, you should specify the exact version. The latest will always download the latest at the time of image creation and you don't know whether this new version is having a bug that breaks your app. So, the idea is to test in a specific version and use that same version in production. If you want to upgrade later to more recent version, you need to first test your app with the new version and then update your Dockerfile with the new version
Here is an e.g.
RUN mkdir /home/aus/.npm; \
npm config set prefix /home/aus/.npm; \
npm install --quiet --no-progress -g webpack#3.11.0; \
npm install --quiet --no-progress -g #angular/cli#1.7.2; \
npm install --quiet --no-progress;

Compile webpack on docker production server

I am setting up docker for my React/Redux app, and I was wondering how to set it up in such way, that in production, on container setup, webpack compiles my whole code with production configuration, and then it removes itself, or something like that. Because the only thing I will need for my project is production code, and a simple node server that will serve it.
I'm not sure if I explained it well, since docker and webpack are still new things for me.
EDIT:
Alternatively I can even serve everything with an apache server, but I want everything to compile and setup just when I run docker-compose.
If I understand correctly, you want to trash your node dev dependencies from your image after your npm run build during the docker build.
You can do it but there is a little trick you must be aware of.
Each line in your Dockerfile result in a new step in the image and is pushed with the image.
So, if you execute in your Dockerfile :
RUN npm install # Install dev and prod deps
RUN npm run build # Execute your webpack build
RUN npm prune --production # Trash all devDependencies from your node_modules folder
Your image size will contains :
The first npm install
The npm run build
The result of the npm prune
Your image will be bigger than just :
RUN npm install # Install dev and prod deps
RUN npm run build # Execute your webpack build
Wich contains :
The first npm install
The npm run build
To avoid this problem you must do in your dockerfile :
RUN npm install && npm run build && npm prune --production
That way you will get a minimalistic image. With :
The npm run build
The result of the npm prune
Your final Dockerfile will be some sort of :
FROM node:7.4.0
ADD . /src
RUN cd /src && npm install && npm run build && npm prune --production # You can even use npm prune without the --production flag
ENV NODE_ENV production

Run webpack build during docker-compose build process

I am trying to build a production docker container with a minified version of the js files.
In my Dockerfile, after installing the npm packages, I am trying to build the webpack compilation.
RUN npm install -g n # upgrading the npm version
RUN n stable
ADD ./webpack/package.json /package.json
RUN npm install --production
RUN npm run build-production # <<< Fails here
The docker build process will fail during the last command RUN npm run build-production with npm complaining that it can't find the installed packages (NODE_PATH is set).
However, when I add the call npm run build-production to my ENTRYPOINT script, it works fine and compiles everything as expected. However, it runs the webpack build everything I start the container, which isn't desired.
Why can't the last docker build step find the packages installed in the previous steps? But why does it work through the entrypoint script?
What is the best way to add the webpack build to the docker build in my Dockerfile?
Please use
RUN bash -l -c 'npm run build-production'
instead of your
RUN npm run build-production # <<< Fails here
this should help
The problem could be that build-production requires a devDependencies, which are not being installed.
A great way to keep your production images small is to use a tool like dobi. It makes it easy to run the build tasks in a dev container, then pack everything into a production image, all with a single command.
Below is an example dobi.yaml that might work for you.
meta:
project: some-project-name
image=builder:
image: myproject-dev
context: dockerfiles/
dockerfile: Dockerfile.build
image=production:
image: user/prod-image-name
context: .
depends: [build]
mount=source:
bind: .
path: /code
run=build:
use: builder
mounts: [source]
command: "npm run build-production"
artifact: path/to/minified/assets
Running dobi production will run any of the tasks that are stale. If none of the source files have changed the tasks are skipped. The depends: [build] ensures that the build step always runs first.

Resources