Moving node.js to a docker container, receiving a cannot file module error

Viewed 1724

I am trying to create a node.js docker container for an api layer transpiled from typescript copied from a scr folder to a dist folder. The node.js container seems to exit immediately and when I issue docker-compose logs I get "Error: Cannot find module '/usr/src/app/webapp.js'", however, when I bring up the container with the -d option, I can see where the file and directory structure is in the correct hierarchical structure. What could be wrong here? Why is node not able to locate the webapp.js file?

Dockerfile:

FROM node:latest

# install our dependencies and nodejs
RUN mkdir -p /usr/src
RUN mkdir -p /tmp/dist

ADD Account/package.json /tmp/package.json
RUN cd /tmp && npm install --production
ADD Account/dist /tmp/dist
RUN mkdir -p /tmp/node_modules/mongodb-repository

ADD Account/node_modules/mongodb-repository /tmp/node_modules/mongodb-repository
RUN mkdir -p /usr/src/app && cp -a /tmp/node_modules /usr/src/app && cp -a /tmp/package-lock.json /usr/src/app
RUN cp -a /tmp/dist/application /usr/src/app && cp -a /tmp/dist/config /usr/src/app && cp -a /tmp/dist/domain /usr/src/app && cp -a /tmp/dist/infrastructure /usr/src/app
RUN cp -a /tmp/dist/routes /usr/src/app && cp -a /tmp/dist/types /usr/src/app && cp -a /tmp/dist/webapp.js /usr/src/app && cp -a /tmp/package.json /usr/src/app
RUN chmod 755 /usr/src/app/webapp.js
RUN mkdir -p /usr/src/app/bin

COPY Account/bin /usr/src/app/bin
COPY Account/.env /usr/src/app/.env

WORKDIR /usr/src/app

CMD [ "node", "webapp.js" ]
EXPOSE 3000

docker-compose.yml

version: '2'
services:
 mongo:
    container_name: "app_mongo"
    tty: true
    image: mongo:latest
    environment:
      - MONGO_DATA_DIR=/data/db
      - MONGO_LOG_DIR=/dev/null
      - MONGO_INITDB_ROOT_USERNAME=********
      - MONGO_INITDB_ROOT_PASSWORD=********
    volumes:
      - /data/dbDocker:/data/db
    ports:
        - 27017:27017
    command: "mongod --smallfiles --auth"
 rabbitmq:
    container_name: "app_rabbitmq"
    tty: true
    image: rabbitmq:management
    ports:
      - "15672:15672"
      - "15671:15671"
      - "5672:5672"
    volumes:
      - /rabbitmq/lib:/var/lib/rabbitmq
      - /rabbitmq/log:/var/log/rabbitmq
      - /rabbitmq/conf:/etc/rabbitmq/
 group:
    container_name: "app_group"
    build:
      context: .
      dockerfile: ./Account/Dockerfile
    volumes:
      - ./Account:/usr/src/app/
    ports:
      - "3000:3000"
    depends_on:
      - mongo
      - rabbitmq
1 Answers

During your image build, you install some things into /usr/src/app, and then you set it as the working directory. Here are the lines from Dockerfile where that happens:

RUN mkdir -p /usr/src
RUN mkdir -p /usr/src/app && cp -a /tmp/node_modules /usr/src/app && cp -a /tmp/package-lock.json /usr/src/app
RUN cp -a /tmp/dist/application /usr/src/app && cp -a /tmp/dist/config /usr/src/app && cp -a /tmp/dist/domain /usr/src/app && cp -a /tmp/dist/infrastructure /usr/src/app
RUN cp -a /tmp/dist/routes /usr/src/app && cp -a /tmp/dist/types /usr/src/app && cp -a /tmp/dist/webapp.js /usr/src/app && cp -a /tmp/package.json /usr/src/app
RUN chmod 755 /usr/src/app/webapp.js
RUN mkdir -p /usr/src/app/bin

COPY Account/bin /usr/src/app/bin
COPY Account/.env /usr/src/app/.env

WORKDIR /usr/src/app

After the build phase, all of that is baked into your image and ready to be used. But at runtime, you told docker-compose:

volumes:
  - ./Account:/usr/src/app/

This is an overlay mount. Whatever is built into the image at /usr/src/app is completely ignored and replaced by the contents of ./Account from the directory where docker-compose.yml is located.

I don't know enough about your project to tell you how to properly fix this, but that's where your error is probably coming from. All the work done during build to construct /usr/src/app is being undone by mounting another directory on top of it at runtime.

If you remove that volume mount, all of /usr/src/app is still there and ready to use. But that may have other side effects that you will need to account for to make your app do its job.

Related