I'm building a Micro-services E-commerce project, I need to create a docker image for each server in my project and run them inside K8s cluster. After successfully creating images for all back-end server I tried creating a docker image for my React front-end app, every time I try creating the image this error happened.
Here is my docker configuration:
FROM node:alpine
WORKDIR /src
COPY package*.json ./
RUN npm install --silent
COPY . .
CMD ["npm ","start"];
Here is the error:
Error: Cannot find module '/src/npm '
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
at node:internal/main/run_main_module:17:47 {
code: 'MODULE_NOT_FOUND',
requireStack: []
}
Sometimes it throws an error like this:
webpack output is served from content not from webpack is served from content not from webpack is served from /app/public docker
This is a community wiki answer posted for better visibility. Feel free to expand it.
To resolve described issues, steps below need to be done.
Upgrade Dockerfile:
WORKDIR /src
COPY package*.json ./
RUN npm install --silent
COPY . .
CMD ["npm","start"];
Use version 3.4.0 for react-scripts
Add stdin_open: true to docker-compose file
Related
I'm using Docker Desktop and I'm working with a Nest.js app, when I build my project with docker-compose build everything is fine since there are no errors in console, however when I run docker-compose up -d my container keeps on failing because it can't find the build directory of my app. The strange thing is that this works perfectly fine in my Windows computer, but my macOS laptop is the one that's failing
Error: Cannot find module '/tmp/dist/main'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:885:15)
at Function.Module._load (internal/modules/cjs/loader.js:730:27)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at internal/main/run_main_module.js:17:47 {
code: 'MODULE_NOT_FOUND',
requireStack: []
}
This is my dockerfile:
FROM node:14.17.0-alpine
WORKDIR /tmp
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
EXPOSE 3005
ENV NODE_TLS_REJECT_UNAUTHORIZED=0
# Run it
ENTRYPOINT ["node", "/tmp/dist/main"]
This is my docker-compose file and the folder structure of the project is pretty basic, after I run the npm run build command a dist folder is created at the root of my project.
version: '3'
services:
my-api:
build: ./my-api
container_name: 'my-api'
restart: always
environment:
NODE_ENV: "docker-compose"
APP_PORT: 3005
ports:
- "3005:3005"
- 9229:9229
depends_on:
- redis
- mysql
As you set WORKDIR /tmp you are currently executing commands in this directory. https://docs.docker.com/engine/reference/builder/#workdir
And from what you provided there is no another tmp directory in you current working directory.
Try to change last command to
ENTRYPOINT ["node", "/dist/main"]
I'm getting the following error and I'm struggling to find the issue
After successfully building my image using the following Dockerfile
Command used
docker build -t my-app:1.0 .
FROM node:13-alpine
# run something in the docker image
RUN mkdir -p /home/app
# this allows you to copy something from the host into docker image
COPY . /home/app
# inside the container it runs the command node server.js
# cmd is an entry point command, whereas run as used later
CMD ["node","/home/app/server.js"]
I have run the following command to run the container
docker run my-app:1.0
it throws the following error:
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
192:docker_compose_demo tech$ docker run my-app:1.0
internal/modules/cjs/loader.js:965
throw err;
^
Error: Cannot find module '/home/app/server.js'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:962:15)
at Function.Module._load (internal/modules/cjs/loader.js:838:27)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47 {
code: 'MODULE_NOT_FOUND',
requireStack: []
}
I changed the path in the Dockerfile and yet is not finding it.
Can someone tell what I'm doing wrong?
The first thing is, to use the WORKDIR instead of running mkdir.
Why use WORKDIR ?
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
You can refer the below Dockerfile -
FROM node:13-alpine
# Creates directory and sets it as current working directory
WORKDIR /home/app
# Copy everything from local current directory to docker/image current directory
COPY . .
# inside the container it runs the command node server.js
# cmd is an entry point command, whereas run as used later
CMD ["node","server.js"]
I have a NodeJS service located on serviceA. In this folder I have a Dockerfile I created and the entry point for this service which is a file called app.js.
On the app.js file I have this:
var auth = require('../common/authentication/authentication.js');
When I try to deploy, I do this from serviceA folder:
docker build -t serviceA .
docker run -p 8080:8080 serviceA
But then I get:
Error: Cannot find module '../common/authentication/authentication.js'
Require stack:
- /usr/src/app/app.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:999:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (/usr/src/app/app.js:46:12)
at Module._compile (node:internal/modules/cjs/loader:1099:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:975:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [ '/usr/src/app/app.js' ]
}
If I modify my Dockerfile to include:
RUN cp -R ../common /usr/src
Then I get this when running docker build:
> [2/7] RUN cp -R ../common /usr/src:
#4 0.327 cp: cannot stat '../common': No such file or directory
This is my current Dockerfile:
# Use the official lightweight Node.js 12 image.
# https://hub.docker.com/_/node
FROM node:17-slim
RUN cp -R ../common /usr/src
# Create and change to the app directory.
WORKDIR /usr/src/app
# Copy application dependency manifests to the container image.
# A wildcard is used to ensure both package.json AND package-lock.json are copied.
# Copying this separately prevents re-running npm install on every code change.
COPY package*.json ./
# Install dependencies.
# If you add a package-lock.json speed your build by switching to 'npm ci'.
# RUN npm ci --only=production
RUN npm install --production
# Copy local code to the container image.
COPY . ./
WORKDIR /usr/src/app
# Run the web service on container startup.
CMD ["node", "app.js"]
I'm not familiar with Dockerfiles but I believe the problem here is that in my container the common folder is not present and I couldn't find a way to include that.
How can I deploy serviceA which makes a reference to ../common folder?
Thanks!
When building, docker sends off a copy of your local directory. If your Dockerfile is at the level of app.js, then it is clear that .. is not part of the Docker context. You can try to move the Dockerfile one level up. That should then send the common directory along (and the copy should work then).
Christian
It's been a few days since I'm trying to resolve this issue which blocks the deployment of my application.
A few days ago, I noticed an error in my CI/CD process due to an exited container issue which comes from this error:
Error: No valid exports main found for '/app/node_modules/graphql-tools'
at resolveExportsTarget (internal/modules/cjs/loader.js:618:9)
at applyExports (internal/modules/cjs/loader.js:499:14)
at resolveExports (internal/modules/cjs/loader.js:548:12)
at Function.Module._findPath (internal/modules/cjs/loader.js:650:22)
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:948:27)
at Function.Module._load (internal/modules/cjs/loader.js:854:27)
at Module.require (internal/modules/cjs/loader.js:1023:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Object.<anonymous> (/app/node_modules/#graphql-modules/utils/src/get-schema-directive-from-directive-resolver.ts:1:1)
at Module._compile (internal/modules/cjs/loader.js:1128:30) {
code: 'MODULE_NOT_FOUND'
}
The thing is when I run my application locally everything works. So I went inside the container to see if this folder exists which does exist. I tried to reproduce the error locally by using npm instead of yarn or use the same node version but I couldn't reproduce it.
Here is my dockerfile.dev which is quite simple:
FROM node:13.3.0-alpine AS base
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY .babelrc ./
COPY src ./src
CMD ["npm", "run", "dev"]
Resolved
The error was due to using the node:13.3.0-alpine image, a change to node:lts-alpine resolved the issue.
I have a Node express app that I'm trying to turn into a Docker image and run in a container. The problem I'm having is that it seems in the containerized version of my app, it can't find my local modules av-request and aml-request, but it has no issue finding the installed modules.
app.js
var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var av = require('./modules/av-request')
var aml = require('./modules/aml-request')
var app = express();
...
dockerfile
FROM node:carbon
# Create container's working directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
# Install app dependencies
COPY ./app/package.json /usr/src/app/
RUN npm install
# Bundle app source
COPY ./app/* /usr/src/app/
# Verify directory contents
RUN ls /usr/src/app/
EXPOSE 8080
CMD ["npm", "start"]
Here is my output from the build and run process. Notice that in step 7 when we run ls /usr/src/app/ it shows all the files that were copied over but not any directory structure or anything. Based on how ls normally works I would expect it to list directories as well but none are shown. This gives me the impression that all my files from my application exist at the root directory level for some reason. Am I copying my application correctly? The correct file structure is below, but I don't see anything at all like this in the output from my build process.
- app/
- package.json
- app.js
- modules/
- node_modules/
- private/
- public/
- views/
- dockerfile
- .dockerignore
And this is the output when I build the image.
docker build -t webapp .
Step 1/9 : FROM node:carbon
...
Step 7/9 : RUN ls /usr/src/app/
---> Running in 9951fa3289da
accepts
acorn
acorn-globals
ajv
align-text
amdefine
aml-request.js
app.js
array-flatten
...
Successfully built f3d877bc94da
Successfully tagged webapp:latest
SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have
'-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.
I can verify the image exists when I run docker images
Then I run docker run -d -p 8080:80 webapp which spits out the container ID. Now when I run docker ps -l it shows the container status as Exited (1) so I go check out the logs.
docker logs b3fcc5a6f545
> app#0.0.0 start /usr/src/app
> node app.js
module.js:538
throw err;
^
Error: Cannot find module './modules/av-request'
at Function.Module._resolveFilename (module.js:536:15)
at Function.Module._load (module.js:466:25)
at Module.require (module.js:579:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/usr/src/app/app.js:5:10)
at Module._compile (module.js:635:30)
at Object.Module._extensions..js (module.js:646:10)
at Module.load (module.js:554:32)
at tryModuleLoad (module.js:497:12)
at Function.Module._load (module.js:489:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! app#0.0.0 start: `node app.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the app#0.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.