Docker Container Not Able to Find Updated Node Modules
Answer a question
- I updated a node module in my project [1] (a NestJs server)
- My
package.jsonand mypackage-lock.jsonboth look correct [2] - When I run the server locally it works
- 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
npm install aws-sdkpackage.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.)
更多推荐



所有评论(0)