Search code examples
docker

Dockerfile: How do I reflect the right path?


I keep having an issue where I get the error: Cannot find module '/mfa/main.js'.

However, the main.js is inside of /mfa/dist/apps/api

This is the latest configuration of Dockerfile I have:

FROM node:14
WORKDIR /mfa/

COPY package.json .
COPY decorate-angular-cli.js .
COPY yarn.lock .

# Configure NPM with the group access token
ENV GROUP_NPM_TOKEN="asdfghjkiuy"
RUN npm config set @my-web:registry http://git.hoosiers.com/api/v4/packages/npm
RUN npm config set //git.hoosiers.com/api/v4/packages/npm/:_authToken=${GROUP_NPM_TOKEN}
RUN npm config set //git.hoosiers.com/api/v4/packages/projects/:_authToken=${GROUP_NPM_TOKEN}

RUN yarn add typescript

RUN yarn install --frozen-lockfile
COPY ./dist .

CMD ["node", "apps/api/main.js"]

So now docker run <image-hash> runs just fine, but when I attempt docker-compose up is when I once again get Cannot find module '/mfa/main.js'.

This is my docker-compose.yml file:

version: '3.9'
services:
  web-app:
    build:
      context: .
      dockerfile: mostly-failed-apps.Dockerfile
    ports:
      - "3000:3000"

Solution

  • You have define your WORKDIR is /mfa and you execute your main.js in apps/api/main.js

    And tip for copy it's not mandatory to write /mfa/ you can just write dot (.) because your in WORKDIR

    FROM node:14
    # Go on /mfa if is doesn't exist WORKDIR create it and go in
    WORKDIR /mfa/
    
    # Copy of package.json where we are so we are with the dot so in /mfa/ it's the same for all copy
    COPY package.json .
    COPY decorate-angular-cli.js .
    COPY yarn.lock .
    
    # Configure NPM with the group access token
    ENV GROUP_NPM_TOKEN="token"
    RUN npm config set @my-web:registry http://git.hoosiers.com/api/v4/packages/npm
    RUN npm config set //git.hoosiers.com/api/v4/packages/npm/:_authToken=${GROUP_NPM_TOKEN}
    RUN npm config set //git.hoosiers.com/api/v4/packages/projects/:_authToken=${GROUP_NPM_TOKEN}
    
    RUN yarn add typescript
    
    RUN yarn install --frozen-lockfile
    COPY ./dist .
    
    # You have create your docker with /mfa/ so you need to execute it in /mfa/
    CMD ["node", "/mfa/dist/apps/api/main.js"]