Answer a question

  1. I updated a node module in my project [1] (a NestJs server)
  2. My package.json and my package-lock.json both look correct [2]
  3. When I run the server locally it works
  4. I rebuilt the docker image, and after the server failed, tried to rebuild docker build --no-cache -t myApp .

What could be the reason that when I run the docker image docker-compose up I am still seeing the error:

Cannot find module 'aws-sdk' or its corresponding type declarations.
1 import { config } from 'aws-sdk';

my dockerfile

FROM node:12.13-alpine as development

WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install --only=development
COPY . .

RUN npm run build

FROM node:12.13-alpine as production
ARG NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}

WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install --only=production
COPY . .
COPY --from=development /usr/src/app/dist ./dist
CMD ["node", "dist/main"]

my docker-compose.yml

version: '3.7'

services:
  main:
    container_name: main
    build:
      context: .
      target: development
    volumes:
      - .:/usr/src/app
      - /usr/src/app/node_modules
    ports:
      - ${SERVER_PORT}:${SERVER_PORT}
      - 9229:9229
    command: npm run start:dev
    env_file:
      - .env
    networks:
      - webnet
    depends_on:
      - ${POSTGRES_DOCKER_HOST}
  main_db:
    container_name: ${POSTGRES_DOCKER_HOST}
    image: postgres:12
    restart: always
    networks:
      - webnet
    environment:
      POSTGRES_DB: ${POSTGRES_DB}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_USER: ${POSTGRES_USER}
      PG_DATA: /var/lib/postgresql/data
    ports:
      - '${POSTGRES_PORT}:${POSTGRES_PORT}'
    volumes:
      - pgdata:/var/lib/postgresql/data
networks:
  webnet:
volumes:
  pgdata:

Footnotes

  1. npm install aws-sdk
  2. package.json
"dependencies": {
  "@types/aws-sdk": "^2.7.0",
  "aws-sdk": "^2.792.0",
}

Answers

Your docker-compose.yml file specifies

volumes:
  - /usr/src/app/node_modules

This creates an anonymous volume that holds the node_modules directory. The very first time your application runs, the volume is populated from the node_modules directory in the image. After that, the contents of the volume take precedence over the contents of the image. This line essentially tells Docker to ignore changes in the image's node_volumes directory; it is not a pass-through to the image to ignore the outer bind mount.

In the Dockerfile you show, you're not really getting any benefits from the volume mounts at all, and I'd recommend deleting the entire volumes: block. (The node dist/main command will not do live reloading, and since you're running the built dist tree, edits in src/main.js won't appear without an explicit rebuild anyways.)

Logo

云原生社区为您提供最前沿的新闻资讯和知识内容

更多推荐