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.stack.imgur.com/4Fifu.png

1 answer

  • answered 2021-09-22 16:37 Sachith Muhandiram

    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"]
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum