1

We are trying to create a container running node.js, using docker (docker-compose, as we intend to add mongodb afterwards as well).

We are copying the package.json in Dockerfile and then mounting a volume in docker-compose.yml which contains the source code to be executed.

Our project structure is as follow.

Project structure

We want source folder to be mounted as volume.

Our package.json file

{
  "name": "node_package",
  "version": "1.0.0",
  "description": "node_package inside node_dir for node_service running on node_container",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node source/index.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.0"
  }
}

Our Dockerfile

FROM node:10-alpine
WORKDIR /node_dir
COPY package*.json ./
RUN npm install

Our docker-compose.yml

version: "3"
services:

  node_service:

     build: .

     container_name: node_container

     user: node

     working_dir: "/node_dir"

     ports:
     - "8080:3000"

     volumes:
     - ./source:/node_dir/source

     command: npm start

Now when we run this on macOS, it works. It mounts the source folder as volume and then executes command npm start. But when we try the same on windows we get the following error

Cannot find module 'node_dir/source/index.js'

Why it is working on mac but not on windows?

Here is our index.js file

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => res.send('Hello World!'))

app.listen(port, () => {console.log(`Example app listening on port ${port}!`)})

When we copy source folder in Dockerfile then it works on Windows. But if we don't copy source folder in Dockerfile and mount it as volume in docker-compose.yml then command npm start does not work.

5
  • Have you tried removing the node_modules folder and running 'npm install' again on the windows machine? Commented May 22, 2019 at 12:27
  • You mean that I should run 'npm install' on host windows machine or on container image? Commented May 22, 2019 at 12:41
  • Well you say 'we try the same on windows' - so I assume you have a bash shell running on windows (or something like that), and you're using some svn (git etc) to grab the repo, so by default you'd have to run npm install anyway, right? I'm just suggesting deleting the node_modules folder and running it again. Commented May 22, 2019 at 12:44
  • No we aren't using any repo. Its just a simple setup project. We just copied files manually. We are copying package.json and running directly npm install on the docker image. Commented May 22, 2019 at 13:08
  • We also tried removing modules folder and running it again. It didn't changed anything because its throwing error on the index.js file mounted as a volume on docker (running on windows) Commented May 22, 2019 at 13:09

1 Answer 1

1

because some node module need to compile so when run npm i npm get Compatible module with OS i.e. the node_module folder in Windows is different with Mac.

best solution: isolate node_module from bind mount in docker-compose. so edit docker-compose.yml from:

volumes:
   - ./source:/node_dir/source

to:

volumes:
   - /node_dir/node_modules # isolate node_modules for compatible os
   - ./source:/node_dir/source
Sign up to request clarification or add additional context in comments.

8 Comments

Isn't node_modules is already isolated in our current scenario? because docker-compose.yml is mounting source folder from host to container inside node_dir directory. But node_modules is in the node_dir directory (generated by npm install on the container using Dockerfile, one level higher than mounted source folder). I don't understand how declaring "/node_dir/node_modules" in volume will isolate it for compatible OS?
no, when you bind node_dir directory to docker container, file system is same host OS not linux file system. when isolate node_modules by above technique node_modules move to a temporary volume that file system is base on linux file system and is completely compatible.
Well it's still not working. Error is same even after isolating the /node_dir/node_modules.
oh, now focus on the code know index.js not mount (or copy) in container so you must copy index.js to container /node_dir to solve problem.
I can help you in telegram messager. my username: masihjahangiri
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.