This issue often stems from volume mounting overwriting the container’s node_modules. When you mount your host directory to /worker/ in the container, it masks the installed modules.
A solution is to use a named volume for node_modules:
This keeps node_modules in the container separate from your host. Also, consider moving ‘npm install’ to your entrypoint script instead of the Dockerfile. This ensures fresh installs on container start.
Remember to rebuild your image after changes. If problems persist, check your .dockerignore file isn’t excluding necessary files.
I’ve encountered this issue before, and it can be quite frustrating. One thing that worked for me was tweaking the Dockerfile to use a multi-stage build. Here’s what I did:
Created a ‘builder’ stage to install dependencies
Copied only the necessary files to the final stage
This approach ensures that node_modules are properly installed and included in the final image, regardless of volume mounts. Here’s a simplified example:
FROM node:14 AS builder
WORKDIR /app
COPY package*.json ./
RUN npm install
FROM node:14
WORKDIR /app
COPY --from=builder /app/node_modules ./node_modules
COPY . .
CMD ["npm", "start"]
This method has the added benefit of reducing the final image size. Give it a try and see if it resolves your issue. If not, double-check your .dockerignore file to ensure it’s not excluding important files.