I have an extremely simple Docker app that uses json-server as a fake backend, but I can't get it to work for some bizarre reason. Here's the Docker file:
The directory I'm building this in contains a Dockerfile, server.js, and db.json.
FROM node:latest
RUN npm install json-server -g
WORKDIR /app
ADD . /app
EXPOSE 3004
CMD ["node", "server.js"]
The json-server itself is dead simple:
const jsonServer = require('json-server')
const server = jsonServer.create()
const router = jsonServer.router('db.json')
const middlewares = jsonServer.defaults()
server.use(middlewares)
server.use(router)
server.listen(3004, () => {
console.log('JSON Server is running')
})
Docker is able to build this image. But when I try to run it, I get the following error:
node:internal/modules/cjs/loader:1042
throw err;
^
Error: Cannot find module 'json-server'
Require stack:
- /app/server.js
at Module._resolveFilename (node:internal/modules/cjs/loader:1039:15)
at Module._load (node:internal/modules/cjs/loader:885:27)
at Module.require (node:internal/modules/cjs/loader:1105:19)
at require (node:internal/modules/cjs/helpers:103:18)
at Object.<anonymous> (/app/server.js:1:20)
at Module._compile (node:internal/modules/cjs/loader:1218:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1272:10)
at Module.load (node:internal/modules/cjs/loader:1081:32)
at Module._load (node:internal/modules/cjs/loader:922:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:82:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [ '/app/server.js' ]
}
Why can't it find the json-server module, especially when I just installed it globally? I'm so confused by this since it couldn't be simpler.
The node image doesn't set the NODE_PATH environment variable, so node can't find globally installed modules.
Since containers are only meant to run a single process, it doesn't really make sense to install things globally.
If you install the module non-globally in your /app directory, it works:
FROM node:latest
WORKDIR /app
RUN npm install json-server
ADD . /app
EXPOSE 3004
CMD ["node", "server.js"]
Related
I am trying to build a vue3 project with vite.js. I want to build it in a Dockerfile, but I get the following error.
vite v2.9.5 building for production...
✓ 0 modules transformed.
Could not resolve entry module (index.html).
error during build:
Error: Could not resolve entry module (index.html).
at error (/panda-planner/frontend-planner/node_modules/rollup/dist/shared/rollup.js:198:30)
at ModuleLoader.loadEntryModule (/panda-planner/frontend-planner/node_modules/rollup/dist/shared/rollup.js:22480:20)
at async Promise.all (index 0)
Error response from daemon: The command '/bin/sh -c npm run build' returned a non-zero code: 1
Failed to deploy '<unknown> Dockerfile: Dockerfile': Can't retrieve image ID from build stream
I have been looking for information on rollup but I don't understand what it is. Also my command npm run build works perfectly on my computer.
Can someone help me, please?
My vite.config.js
import { defineConfig } from "vite";
import vue from "#vitejs/plugin-vue";
import eslintPlugin from "vite-plugin-eslint";
const path = require("path");
// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue(), eslintPlugin()],
resolve: {
alias: {
"#": path.resolve(__dirname, "src"),
},
},
});
My Dockerfile
# Build backend application
FROM node:14.19.1-alpine AS builder
WORKDIR /panda-planner/backend-planner/
COPY /backend-planner/package*.json .
RUN npm install
COPY . .
RUN npm run build
EXPOSE 1337
CMD ["npm", "run", "start" ]
# Build frontend application
FROM builder as frontend
WORKDIR /panda-planner/frontend-planner/
COPY /frontend-planner/package*.json .
RUN npm install --legacy-peer-deps
COPY . .
RUN npm run build
# Setup nginx server for frontend
FROM nginx:stable-alpine as nginx
COPY --from=frontend /frontend-planner/dist /usr/share/nginx/html
#COPY ./default.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;" ]
My bad -_-!
I made a mistake in the COPY path in the frontend.
This solution works:
# Build backend application
FROM node:14.19.1-alpine AS builder
WORKDIR /panda-planner/backend-planner/
COPY /backend-planner/package*.json .
RUN npm install
COPY /backend-planner/ .
RUN npm run build
EXPOSE 1337
CMD ["npm", "run", "start" ]
# Build frontend application
FROM builder AS frontend
WORKDIR /panda-planner/frontend-planner/
COPY /frontend-planner/package*.json .
RUN npm install --legacy-peer-deps
COPY /frontend-planner/ .
RUN npm run build
# Setup nginx server for frontend
FROM nginx:stable-alpine AS nginx
COPY --from=frontend /panda-planner/frontend-planner/dist/ /usr/share/nginx/html/
#COPY ./default.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;" ]
I'm trying to set up a hot-reload enabled dev server for vue on Docker, however, there seems to be some issue with the quasar dependency.
Here is my Dockerfile:
FROM node:14.5.0-alpine3.12
WORKDIR /app
COPY package*.json ./
RUN npm install -g #vue/cli \
&& npm install -g #quasar/cli \
&& npm install
EXPOSE 8080
CMD ["npm", "run", "serve"]
I build and run the image by:
docker image build -t test .
docker container run -it -p 8080:8080 -v $pwd:/app test
But then, the error shows up:
ERROR TypeError: Cannot read property 'quasar' of undefined
TypeError: Cannot read property 'quasar' of undefined
at module.exports (/app/node_modules/vue-cli-plugin-quasar/index.js:13:29)
at /app/node_modules/#vue/cli-service/lib/Service.js:78:7
at Array.forEach (<anonymous>)
at Service.init (/app/node_modules/#vue/cli-service/lib/Service.js:76:18)
at Service.run (/app/node_modules/#vue/cli-service/lib/Service.js:215:10)
at Object.<anonymous> (/app/node_modules/#vue/cli-service/bin/vue-cli-service.js:36:9)
at Module._compile (internal/modules/cjs/loader.js:1201:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1221:10)
at Module.load (internal/modules/cjs/loader.js:1050:32)
at Function.Module._load (internal/modules/cjs/loader.js:938:14)
I also tried to use yarn, but the same result occurred.
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
QUESTION: (edited: solution is added at the end of this post)
I have VueJS project (developed in webpack), which I want to docker-size.
My Dockerfile looks like:
FROM node:8.11.1 as build-stage
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
CMD ["npm", "run", "dev"]
which is basically following the flow from this post.
I also have a .dockerignore file, where I copied the same files from my .gitignore and it looks like:
.DS_Store
node_modules/
/dist/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Editor directories and files
.idea
.vscode
*.suo
*.ntvs*
*.njsproj
*.sln
.git/
I have created a docker image with the command:
docker build -t test/my-image-name .
and then run it into a container with the command:
docker run -it -p 8080:8080 --rm --name my-container-name test/my-image-name
as a result of this last command, I got the same output in the terminal (which is normally showing in cases of debugging with webpack / vuejs) as when I run the app locally:
BUT: at the end, in the browser window the app is not loaded
If I run the commands docker images and docker ps I can see that the image and the container are there, and while creating them, I did not got any error messages.
I found this post and had a few tries for changing the Dockerfile as:
Option 1
FROM node:8.11.1 as build-stage
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
ENTRYPOINT ["ng", "serve", "-H", "0.0.0.0"]
Option 2
FROM node:8.11.1 as build-stage
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
ENTRYPOINT ["ng", "serve", "-H", "0.0.0.0"]
EXPOSE 8080
CMD ["npm", "run", "dev"]
But it seems none of them is working.
btw. my package.json file looks like:
"scripts": {
"dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js",
"start": "npm run dev",
"build": "node build/build.js"
}
So I'm wondering: how to make the app to be opened in the browser from the docker image?
SOLUTION: not, sure if this was the reason for the fix, but I did two things. As mentioned, I'm working with the VueJS and webpack, so inside of the file named config/index.js, which initially looked like:
module.exports = {
dev: {
// Paths
assetsSubDirectory: 'static',
assetsPublicPath: '/',
proxyTable: {},
// Various Dev Server settings
host: 'localhost', // <---- this one
port: 8080,
I changed the host property from 'localhost' into '0.0.0.0', removed the EXPOSE 8080 line from the Dockerfile (the initial Docker file from my question above) since I noticed that the port from the config file is used by default and also restarted the installed Docker tool on my local machine.
Here is my docker image
FROM node:8-alpine
ADD oracle-instantclient*.rpm /tmp/
COPY . /app
WORKDIR /app
RUN npm install --production --no-optional
ENV LD_LIBRARY_PATH=/usr/lib/oracle/12.2/client64/lib/
CMD ["node", "."]
I am getting error
1540183793466 Registered plugin: #axway/api-builder-plugin-fn-swagger
Failed to load connector sub directory module; skipping it:
Error: NJS-045: cannot load the oracledb add-on binary for Node.js 8.11.2 (linux, x64)
Node.js require() error was:
DPI-1047: 64-bit Oracle Client library cannot be loaded: "Error loading shared library libclntsh.so: No such file or directory". See https://oracle.github.io/odpi/doc/installation.html#linux for help
Node.js require() mapped to /app/node_modules/#axway/api-builder-plugin-dc-oracle/node_modules/oracledb/build/Release/oracledb.node
Node-oracledb installation instructions: https://oracle.github.io/node-oracledb/INSTALL.html
You must have 64-bit Oracle client libraries in LD_LIBRARY_PATH, or configured with ldconfig.
If you do not have Oracle Database on this computer, then install the Instant Client Basic or Basic Light package from
http://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html
at Object. (/app/node_modules/#axway/api-builder-plugin-dc-oracle/node_modules/oracledb/lib/oracledb.js:65:13)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object. (/app/node_modules/#axway/api-builder-plugin-dc-oracle/node_modules/oracledb/index.js:1:80)
at Module._compile (module.js:652:30)
Failed to load connector sub directory module; skipping it:
Adding the .rpm to your image is not sufficient to install the Oracle client.
You need to install it through a package manager.
That might not be easy, since you are using node:8-alpine as a base image.
I would suggest using the normal node image and installing the Oracle instantclient like so in your image:
FROM node:8
ADD oracle-instantclient*.rpm /tmp/
RUN yum -y install /tmp/oracle-instantclient*.rpm && \
rm -rf /var/cache/yum && \
rm -f /tmp/oracle-instantclient*.rpm && \
echo /usr/lib/oracle/12.2/client64/lib > /etc/ld.so.conf.d/oracle-instantclient12.2.conf && \
ldconfig
ENV PATH=$PATH:/usr/lib/oracle/12.2/client64/bin
COPY . /app
WORKDIR /app
RUN npm install --production --no-optional
CMD ["node", "."]
For reference check here