can’t find module after creating container from image docker from github respository

  containers, docker, docker-compose, image, node.js

I am new to docker and now i try to create image from github reposistory (app nodejs and mongodb) and create container from that image.

Step:

  • create image with

    docker build -t from-api-mkf (url the repo)

  • create container with (docker compose)

    version: "3.4"                                                                    
    services:
      mongodb:
          image: mongo:4.2.3-bionic
          container_name: mongodb
          restart: always
          ports:
              - 27018:27017
          environment:
              - MONGO_INITDB_DATABASE=testdb
              - MONGO_INITDB_ROOT_USERNAME=
              - MONGO_INITDB_ROOT_PASSWORD=
      api-mkf:
          image: from-api-mkf
          build:
              context: /home/wuku/sources/api-mkf/ (source from github repo with clone)
              dockerfile: /home/wuku/sources/api-mkf/Dockerfile (source from github repo with clone)
          container_name: api-mkf
          restart: always
          ports: 
              - 8080:80
          volumes:
              - /home/wuku/setup:/setup
              - /home/wuku/sources/api-mkf:/var/www/app
              - /etc/hosts:/etc/hosts
          working_dir: /var/www/app
          links: 
              - mongodb
    
  • Dockerfile

FROM node:14

RUN mkdir -p /var/www/app

# Create app directory
WORKDIR /var/www/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available ([email protected]+)
COPY package*.json ./

# Bundle app source
COPY . .

RUN npm install
# If you are building your code for production
# RUN npm ci --only=production

EXPOSE 8080
CMD [ "node", "index.js" ]
  • package.json
{
  "name": "rest-api",
  "version": "1.0.0",
  "description": "Rest API",
  "main": "index.js",
  "scripts": {
    "test": "echo "Error: no test specified" && exit 1",
    "start": "node index.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "dotenv": "^10.0.0",
    "express": "^4.17.1",
    "mongoose": "^5.12.10",
    "pm2": "^4.5.6",
    "tunnel-ssh": "^4.1.4",
    "morgan": "^1.9.1",
    "winston": "^3.2.1"
  },
  "devDependencies": {
    "morgan": "^1.9.1",
    "nodemon": "^2.0.7",
    "winston": "^3.2.1"
  },
  "engines": {
    "node": "14.x"
  }
}
  • error ( docker logs –tail 50 –follow –timestamps api-mkf)

    2021-06-09T01:38:52.499894700Z Error: Cannot find module ‘winston’
    2021-06-09T01:38:52.499905200Z Require stack:
    2021-06-09T01:38:52.499963000Z – /var/www/app/utils/logger.js
    2021-06-09T01:38:52.499980700Z – /var/www/app/utils/index.js
    2021-06-09T01:38:52.499991300Z – /var/www/app/index.js
    2021-06-09T01:38:52.500002200Z at Function.Module._resolveFilename
    (internal/modules/cjs/loader.js:885:15) 2021-06-09T01:38:52.500021400Z
    at Function.Module._load (internal/modules/cjs/loader.js:730:27)
    2021-06-09T01:38:52.500032400Z at Module.require
    (internal/modules/cjs/loader.js:957:19) 2021-06-09T01:38:52.500044700Z
    at require (internal/modules/cjs/helpers.js:88:18)
    2021-06-09T01:38:52.500056500Z at Object.
    (/var/www/app/utils/logger.js:1:46) 2021-06-09T01:38:52.500068400Z
    at Module._compile (internal/modules/cjs/loader.js:1068:30)
    2021-06-09T01:38:52.500078900Z at Object.Module._extensions..js
    (internal/modules/cjs/loader.js:1097:10)
    2021-06-09T01:38:52.500117700Z at Module.load
    (internal/modules/cjs/loader.js:933:32) 2021-06-09T01:38:52.500127400Z
    at Function.Module._load (internal/modules/cjs/loader.js:774:14)
    2021-06-09T01:38:52.500136700Z at Module.require
    (internal/modules/cjs/loader.js:957:19) {
    2021-06-09T01:38:52.500145700Z code: ‘MODULE_NOT_FOUND’,
    2021-06-09T01:38:52.500157100Z requireStack: [
    2021-06-09T01:38:52.500167500Z ‘/var/www/app/utils/logger.js’,
    2021-06-09T01:38:52.500176700Z ‘/var/www/app/utils/index.js’,
    2021-06-09T01:38:52.500185800Z ‘/var/www/app/index.js’
    2021-06-09T01:38:52.500195400Z ] 2021-06-09T01:38:52.500204000Z }

I using window 10 and docker dekstop.

as you can see, it’s can’t find module winston.
is the package.json dependency not installed ??

thanks for your help

Source: Docker Questions

LEAVE A COMMENT