Error is a socket timeout issue during the npm install process, possibly related to network connectivity or proxy settings in a Dockerized application

I am encountering an issue while working on my Dockerized application and would greatly appreciate your assistance in resolving it.

Issue Type: Socket Error during npm install

OS Version/Build: (Docker container)

OS: Windows 10 Home
Device : Intel i5 10300H 8GB RAM 1TB HDD

Steps to Reproduce:

  1. Clone the project
  2. Navigate to the project directory.
  3. Run docker-compose -f docker-compose-development.yml up --build to start the application.

Error Log:

ERROR [app development 2/9] RUN npm install                                                                                                       90.9s
------
 > [app development 2/9] RUN npm install:
32.20 npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
35.71 npm WARN deprecated querystring@0.2.0: The querystring API is considered Legacy. new code should use the URLSearchParams API instead.
56.94 npm WARN deprecated har-validator@5.1.3: this library is no longer supported
57.55 npm WARN deprecated flatten@1.0.2: flatten is deprecated in favor of utility frameworks such as lodash.
68.38 npm WARN deprecated bson@1.0.9: Fixed a critical issue with BSON serialization documented in CVE-2019-2391, see https://bit.ly/2KcpXdo for more details
82.80 npm notice
82.80 npm notice New major version of npm available! 8.5.0 -> 10.1.0
82.80 npm notice Changelog: <https://github.com/npm/cli/releases/tag/v10.1.0>
82.80 npm notice Run `npm install -g npm@10.1.0` to update!
82.80 npm notice
82.80 npm ERR! code ERR_SOCKET_TIMEOUT
82.80 npm ERR! network Socket timeout
82.80 npm ERR! network This is a problem related to network connectivity.
82.80 npm ERR! network In most cases you are behind a proxy or have bad network settings.
82.80 npm ERR! network
82.80 npm ERR! network If you are behind a proxy, please make sure that the
82.80 npm ERR! network 'proxy' config is set properly.  See: 'npm help config'
82.82
82.82 npm ERR! A complete log of this run can be found in:
82.82 npm ERR!     /root/.npm/_logs/2023-10-01T18_14_14_246Z-debug-0.log
------
failed to solve: process "/bin/sh -c npm install" did not complete successfully: exit code: 1

Docker Compose File (docker-compose-development.yml):

(version: '3.4'
services:
  mongo:
    image: mongo:4.4
    volumes:
      - dbdata:/data/db
  app:
    build:
      context: ./
      dockerfile: Dockerfile
      target: development
    environment:
      - MONGO_URL=mongodb://mongo:27017/p5js-web-editor
    volumes:
      - .:/usr/src/app
      - /usr/src/app/node_modules
    ports:
      - '8000:8000'
      - '8002:8002'
    depends_on:
      - mongo
volumes:
  dbdata:

Dockerfile:

FROM node:16.14.2 as base
ENV APP_HOME=/usr/src/app \
  TERM=xterm
RUN mkdir -p $APP_HOME
WORKDIR $APP_HOME
EXPOSE 8000
EXPOSE 8002

FROM base as development
ENV NODE_ENV development
COPY package.json package-lock.json ./
RUN npm install
RUN npm rebuild node-sass
COPY .babelrc index.js nodemon.json ./
COPY ./webpack ./webpack
COPY client ./client
COPY server ./server
COPY translations/locales ./translations/locales
COPY public ./public
CMD ["npm", "start"]

FROM development as build
ENV NODE_ENV production
RUN npm run build

FROM base as production
ENV NODE_ENV=production
COPY package.json package-lock.json index.js ./
RUN npm install --production
RUN npm rebuild node-sass
COPY --from=build $APP_HOME/dist ./dist
CMD ["npm", "run", "start:prod"]

Issue Description:
I am currently working on a Dockerized Node.js application, and I’m facing a socket error during the npm install process. The error message suggests that it’s related to network connectivity and may be due to a proxy or network configuration issue.

Error Details:

  • The npm install command runs into a socket timeout error.
  • The error message suggests that it may be related to network connectivity and proxy settings.
  • The application is containerized using Docker, and it seems to be affecting the npm installation process.

Request for Help:
I’m seeking guidance on how to resolve this socket timeout error. If you have encountered a similar issue or have expertise in Docker and npm, your insights would be greatly appreciated. Please let me know if you need any additional information or logs to assist in diagnosing and resolving this problem.

Thank you in advance for your help!

1 Like

This is inconsistently occuring to a project of mine as well. It is a C# .NET 8 Blazor project.

Windows 10 Pro version 22H2
11th Gen Intel(R) Core™ i9-11900H @ 2.50GHz 2.50 GHz
Docker Desktop version 4.29.0
VS 2022 version 17.9.6

Did you find any good resolutions to your issue? Intermittently, cleaning / rebuilding works. Other times, restarting docker service works. It doesn’t time out in our gitlab cicd pipeline, but locally I’d say it’s ~50% of the time now.

docker-compose.yml:

version: '3.4'

services:
  servicenamehere:
    image: ${DOCKER_REGISTRY-}appnamehere
    build:
      context: .
      dockerfile: projectnamehere/Dockerfile
    ports:
        - 7000:80
        - 7001:443

docker-compose.override.yml:

version: '3.4'

services:
  servicenamehere:
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_URLS=https://+:443;http://+:80
    ports:
      - 7000:80
      - 7001:443
    volumes:
      - ${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro
      - ${APPDATA}/ASP.NET/Https:/root/.aspnet/https:ro
    networks:
      default:
        aliases:
          - aliasednetworknamehere

networks:
  default:
    name: networknamehere
    external: true

Dockerfile:

# Define base images
ARG IMAGE_DOTNET_SDK=mcr.microsoft.com/dotnet/sdk:8.0-alpine
ARG IMAGE_DOTNET=mcr.microsoft.com/dotnet/aspnet:8.0-alpine

# Build stage
FROM $IMAGE_DOTNET_SDK AS build
WORKDIR /app

# Install Node.js and npm in a single RUN command
RUN apk add --no-cache nodejs npm

# Copy csproj file and restore .NET dependencies
COPY ["projectnamehere/projectnamehere.csproj", "projectnamehere/"]
RUN dotnet restore "projectnamehere/projectnamehere.csproj"

# Copy the rest of the source code
COPY . .

# Build the application in the same WORKDIR and layer to utilize caching
WORKDIR "/app/projectnamehere"
RUN dotnet build "projectnamehere.csproj" -c Release -o /app/build \
    # Install npm dependencies and build JS and CSS in the same RUN command
    && npm install \
    && npm run build:css \
    && npm run build:js:prod

# Publish stage, utilizing the same base as build stage to avoid extra layers
FROM build AS publish
RUN dotnet publish "projectnamehere.csproj" -c Release -o /app/publish /p:UseAppHost=false

# Final stage
FROM $IMAGE_DOTNET
WORKDIR /app
EXPOSE 80 443

# Copy only the necessary build artifacts
COPY --from=publish /app/publish .

ENTRYPOINT ["dotnet", "projectnamehere.dll"]