Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Module Not found after attaching volume in docker

This is my dockerfile

FROM node:15
# sets the folder structure to /app directory
WORKDIR /app
# copy package.json to /app folder
COPY package.json .
RUN npm install
# Copy all files from current directory to current directory in docker(app)
COPY . ./
EXPOSE 3000
CMD ["node","index.js"]

I am using this command in my powershell to run the image in a container

docker run -v ${pwd}:/app -p 3000:3000 -d --name node-app node-app-image

${pwd} returns the current directory. But as soon as I hit enter, somehow node_modules isn't being installed in the container and I get "express not found" error in the log. [![Docker log][1]][1] I can't verify if node_modules isn't being installed because I can't get the container up to run the exec --it command.


I was following a freecodecamp tutorial and it seems to work in his pc and I've tried this command in command prompt too by replacing ${pwd} by %cd%.


This used to work fine before I added the volume flag in the command. [1]: https://i.sstatic.net/4Fifu.png

like image 631
S. Karki Avatar asked Oct 20 '25 11:10

S. Karki


1 Answers

Your problem was you build your image somewhere and then try to map another folder to it.

|_MyFolder/
  |_ all-required-files
  |_ all-required-folders
  |_ Dockerfile

docker build -t node-app-image .

docker run -p 3000:3000 -d --name node-app node-app-image

Simplified Dockerfile

FROM node:15
# sets the folder structure to /app directory
WORKDIR /app

# Copy all files from current directory to current directory in docker(app)
COPY . ./

RUN npm install

EXPOSE 3000
CMD ["node","index.js"]
like image 89
Sachith Muhandiram Avatar answered Oct 23 '25 02:10

Sachith Muhandiram