npm install freezing during Docker build process

Help! My Docker build is stuck on npm install

I’m trying to build my Strapi app in Docker on my Ubuntu server, but it’s getting stuck at the end of the npm install step. Here’s what’s happening:

  1. I’ve updated my Strapi app
  2. The build process starts fine
  3. npm install runs for about 13 minutes
  4. It shows a message about adding packages
  5. Then it just hangs on ‘npm info ok’

I’ve tried running the app outside Docker on my local Ubuntu machine, and it works perfectly. But in Docker, it’s a no-go.

Here’s a simplified version of my Dockerfile:

FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
EXPOSE 1337
CMD ['npm', 'start']

Any ideas why this might be happening? Could it be a version conflict or some kind of Docker-specific issue? I’m really stumped here!

have u tried clearing npm cache before building? sometimes that helps. also, check ur node version in docker matches ur local setup. if all else fails, maybe try using a multi-stage build to reduce image size and speed things up. good luck!

I’ve encountered similar issues with npm installs freezing during Docker builds. One thing that’s worked for me is increasing the available memory for the build process. Try adding the ‘–memory’ and ‘–memory-swap’ flags when running your Docker build command, like this:

docker build --memory=4g --memory-swap=4g -t your-image-name .

This allocates more RAM to the build, which can help with large npm installs. Another approach is to use yarn instead of npm, as it tends to be more reliable in Docker environments. You’d need to modify your Dockerfile to use yarn commands instead.

If those don’t work, you might want to check your package-lock.json file for any potential conflicts or outdated dependencies. Sometimes a clean install with a fresh package-lock can resolve hanging issues.

I’ve dealt with this exact issue before, and it can be super frustrating. One thing that worked for me was adding a .npmrc file to my project root with the following content:

network-timeout=100000
fetch-retries=3
fetch-retry-factor=2
fetch-retry-mintimeout=10000
fetch-retry-maxtimeout=60000

This increases the network timeout and adds retry logic, which can help if the hang is due to network issues during package fetching. Also, make sure you’re not running out of disk space on your Docker host - I once spent hours debugging a similar issue only to find out my disk was full!

If that doesn’t work, you might want to try building with verbose output (npm install --verbose) to see exactly where it’s hanging. Sometimes this can reveal unexpected issues with specific packages.