I have jest automation with puppeteer that needs to run as a docker container. but after the build when I try to run it. I get this error Error: Jest: Got error running globalSetup - /usr/src/app/node_modules/jest-environment-puppeteer/setup.js, reason: Failed to launch the browser process! /usr/src/app/node_modules/puppeteer/.local-chromium/linux-884014/chrome-linux/chrome: error while loading shared libraries: libxshmfence.so.1: cannot open shared object file: No such file or directory
I'm quit new for the docker stuff not sure what I'm doing wrong here
jest.config.ts
module.exports = {
setupFiles:['dotenv/config'],
preset: "jest-puppeteer",
roots: ['<rootDir>/src'],
transform: {
'^.+\\.tsx?$': 'ts-jest'
},
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
}
jest-puppeteer.config.js
module.exports = {
launch: {
headless: true, //Specify whether to launch UI
defaultViewport: null,
args: ['--start-maximized', '--disable-gpu',
'--disable-dev-shm-usage', '--disable-setuid-sandbox',
'--no-sandbox'],
},
testEnvironmentOptions: { resources: 'usable' },
};
Dockerfile
FROM node:16.14.0
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
RUN chown -R node /usr/src/app
USER node
CMD npm run test
I installed chrome separately into docker and tried to run it but that didn't work.
I use this docker image and it fixed the issue
https://hub.docker.com/r/alekzonder/puppeteer/
Related
I have a docker image that I am trying to use pm2 on. For some reason I am getting a 404 error if I run it with the following command in my dockerfile. Am I calling this correctly?
CMD ["node_modules/.bin/pm2-runtime", "node_modules/.bin/next", "start"]
Thanks in advance!
I was able to figure this out. I added an ecosystem.config.js, added ENV to point to node_modules in my Dockerfile
COPY ecosystem.config.js ./
ENV PATH=/app/node_modules/.bin:$PATH
CMD ["pm2-runtime", "start", "ecosystem.config.js"]
ecosystem.config.js
module.exports = {
apps: [
{
name: 'app-name',
script: 'yarn start',
},
],
};
and it started working
I have the following simple server in express, with the following docker file
axiostest.mjs
import axios from "axios"
import express from "express"
const app = express();
app.get("/", (request, response) => {
axios.get(`http://localhost:8888/admin_issues`).then(res => {
console.log(res.data);
response.send(res.data)
}).catch(err => {
console.log("ERRRROR")
console.log(err);
response.send(err)
})
});
app.listen(1112, () => {
console.log("Listen on the port 1112...");
});
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+)
COPY package*.json ./
RUN npm install
# If you are building your code for production
# RUN npm ci --only=production
# Bundle app source
COPY . .
EXPOSE 1112
CMD [ "node", "axiostest.mjs" ]
If I run the server normally with
node axiostest.mjs, and then I do a postman call to localhost:1112
It works just fine
But If I build the docker container
docker build . -t me/express-test
and then i run it
docker run -p 49160:1112 -d me/express-test
If i do a postman call to localhost:49160
It says
"message": "connect ECONNREFUSED 127.0.0.1:8888"
Because axios is failing to connect to 127.0.0.1:8888
How can I fix this?
I was trying to dockerize my existing simple vue app , following on this tutorial from vue webpage https://v2.vuejs.org/v2/cookbook/dockerize-vuejs-app.html. I successfully created the image and the container. My problem is that when I edit my code like "hello world" in App.vue it will not automatically update or what they called this hot reload ? or should I migrate to the latest Vue so that it will work ?
docker run -it --name=mynicevue -p 8080:8080 mynicevue/app
FROM node:lts-alpine
# install simple http server for serving static content
RUN npm install -g http-server
# make the 'app' folder the current working directory
WORKDIR /app
# copy both 'package.json' and 'package-lock.json' (if available)
COPY package*.json ./
# install project dependencies
RUN npm install
# copy project files and folders to the current working directory (i.e. 'app' folder)
COPY . .
# build app for production with minification
# RUN npm run build
EXPOSE 8080
CMD [ "http-server", "serve" ]
EDIT:
Still no luck. I comment out the npm run build. I set up also vue.config.js and add this code
module.exports = {
devServer: {
watchOptions: {
ignored: /node_modules/,
aggregateTimeout: 300,
poll: 1000,
},
}
};
then I run the container like this
`docker run -it --name=mynicevue -v %cd%:/app -p 8080:8080 mynicevue/app
when the app launches to browser I get this error in terminal and the browser is whitescreen
"GET /" Error (404): "Not found"
Can someone help me please of my Dockerfile what is wrong or missing so that I can play my vue app using docker ?
Thank you in advance.
Okay I tried your project in my local and here's how you do it.
Dockerfile
FROM node:lts-alpine
# bind your app to the gateway IP
ENV HOST=0.0.0.0
# make the 'app' folder the current working directory
WORKDIR /app
# copy both 'package.json' and 'package-lock.json' (if available)
COPY package*.json ./
# install project dependencies
RUN npm install
# copy project files and folders to the current working directory (i.e. 'app' folder)
COPY . .
EXPOSE 8080
ENTRYPOINT [ "npm", "run", "dev" ]
Use this command to run the docker image after you build it:
docker run -v ${PWD}/src:/app/src -p 8080:8080 -d mynicevue/app
Explanation
It seems that Vue is expecting your app to be bound to your gateway IP when it is served from within a container. Hence ENV HOST=0.0.0.0 inside the Dockerfile.
You need to mount your src directory to the running container's /app/src directory so that the changes in your local filesystem directly reflects and visible in the container itself.
The way in Vue to watch for the file changes is using npm run dev, hence ENTRYPOINT [ "npm", "run", "dev" ] in Dockerfile
if you tried previous answers and still doesn't work , try adding watch:{usePolling: true} to vite.config.js file
import { defineConfig } from 'vite'
import vue from '#vitejs/plugin-vue'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue()],
server: {
host: true,
port: 4173,
watch: {
usePolling: true
}
}
})
Dockerfile
FROM node:alpine
COPY ./ ./
RUN npm install
CMD ["npm", "start"]
index.js
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hi there');
});
app.listen(8080, () => {
console.log("Listening on post 8080");
});
package.json
{
"dependencies": {
"express": "*"
},
"scripts": {
"start": "node index.js"
}
}
docker build .
npm ERR! could not detect node name from path or package
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2020-11-23T23_36_35_301Z-debug.log
The command '/bin/sh -c npm install' returned a non-zero code: 1
I cannot understand why this does not work.
Set a WORKDIR then copy your files to that directory:
FROM node:alpine
WORKDIR /app
COPY ./ /app
RUN npm install
CMD ["npm", "start"]
As explained in the docs:
The WORKDIR instruction sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile. If the WORKDIR doesn’t exist, it will be created even if it’s not used in any subsequent Dockerfile instruction.
On npm's Github repo, a similar error was mentioned in this comment as part of the issue on npm not working when run outside a package directory. It seems related only to new versions of node and npm (node v15.x, npm v7.0.x as of the bug report):
Fresh install of Node 15.1.0 and npm i -g npm#7.0.9, that for some (probably unrelated) reason don't work properly.
...
24 verbose stack TypeError: could not detect node name from path or package
...
28 verbose node v15.1.0
29 verbose npm v7.0.8
30 error could not detect node name from path or package
But in your case, you do have a package.json, so it's probably because the build command can't find it without setting a proper WORKDIR. So, rather than to work it around with npm, just make sure to always set a WORKDIR. It's recommended anyway as mentioned in the WORKDIR section of Docker's best practices:
For clarity and reliability, you should always use absolute paths for your WORKDIR. Also, you should use WORKDIR instead of proliferating instructions like RUN cd … && do-something, which are hard to read, troubleshoot, and maintain.
I am working on sails application In my application I have used mysql and mongo adpater to connect with different database. Both db are hosted on somewhere on internet. Application is working fine in my local environment. I am facing issue once I add project to docker container. I am able to generate docker image and run docker container. When I call simple routers where DB connection is not exists it's working fine but when I call Testcontroller which is return data from mongodb. it give me ReferenceError: Test is not define. here Test is mongodb's entity.
DockerFile:
FROM node:latest
ENV NODE_ENV production
WORKDIR /usr/src/app
COPY ["package.json", "./"]
RUN npm install --verbose --force && mv node_modules ../
COPY . .
EXPOSE 80
CMD npm start
TestController
/**
* TestController
* #description :: Server-side actions for handling incoming requests.
*
* #help :: See https://sailsjs.com/docs/concepts/actions
*/
module.exports = {
index: async function(req, res) {
var data = await Test.find(); // Here I am getting error Test is not define.
res.json(data);
}
};
Routes.js
'GET /test': {controller:'test', action:'index'}
I found issue I am moving node_modules to previous directory that is the issue.
below Configuration works for me.
FROM node:latest
ENV NODE_ENV production
WORKDIR /usr/src/app
COPY ["package.json", "./"]
RUN npm install --verbose --force
COPY . .
EXPOSE 80
CMD npm start