'Docker blocked on NPM install - Socket Timeout
I have a simple Dockerfile which is running node, this is the configuration:
FROM node:latest
WORKDIR /usr/src/auth-starter-server
COPY ./ ./
RUN npm install
CMD ["sh"]
Which is being used on a docker-compose.yml as such:
version: "3.8"
services:
# Node Server
auth-starter-server:
container_name: server
restart: unless-stopped
build: ./
command: npm run start:live
working_dir: /usr/src/auth-starter-server
ports:
- "5000:5000"
volumes:
- ./:/usr/src/auth-starter-server
From a day to the other happened I couldn't build the image any longer. It starts without problems,
Creating network "react-auth-starter_default" with the default driver
Building auth-starter-server
Step 1/5 : FROM node:latest
---> 6f7f341ab8b8
Step 2/5 : WORKDIR /usr/src/auth-starter-server
---> Using cache
---> b25f7c08d3eb
Step 3/5 : COPY ./ ./
---> Using cache
---> 2a06e76bab32
Step 4/5 : RUN npm install
---> Running in 0034d6afa38e
but when building with docker-compose up --build
(or simply docker build -t auth-starter-server .
) it just times out on 'npm install' and it returns the following error:
npm notice
npm notice New minor version of npm available! 7.0.15 -> 7.3.0
npm notice Changelog: <https://github.com/npm/cli/releases/tag/v7.3.0>
npm notice Run `npm install -g [email protected]` to update!
npm notice
npm ERR! code ERR_SOCKET_TIMEOUT
npm ERR! errno ERR_SOCKET_TIMEOUT
npm ERR! request to https://registry.companyregistry.com/yargs-parser/-/yargs-parser-13.1.2.tgz failed, reason: Socket timeout
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2020-12-21T15_37_42_509Z-debug.log
ERROR: Service 'auth-starter-server' failed to build : The command '/bin/sh -c npm install' returned a non-zero code: 1
Something I noticed is that's not using the official npm registry, even though that hasn't been set up as the default registry. Here the npm configuration (got by running npm config ls -l
):
cli configs
long = true
metrics-registry = "https://registry.npmjs.org/"
scope = ""
user-agent = "npm/6.14.9 node/v15.4.0 darwin x64"
; builtin config undefined
prefix = "/usr/local"
; default values
access = null
allow-same-version = false
also = null
always-auth = false
audit = true
audit-level = "low"
auth-type = "legacy"
before = null
bin-links = true
browser = null
ca = null
cache = "/Users/ale917k/.npm"
cache-lock-retries = 10
cache-lock-stale = 60000
cache-lock-wait = 10000
cache-max = null
cache-min = 10
cafile = undefined
cert = null
cidr = null
color = true
commit-hooks = true
depth = null
description = true
dev = false
dry-run = false
editor = "vi"
engine-strict = false
fetch-retries = 2
fetch-retry-factor = 10
fetch-retry-maxtimeout = 60000
fetch-retry-mintimeout = 10000
force = false
format-package-lock = true
fund = true
git = "git"
git-tag-version = true
global = false
global-style = false
globalconfig = "/usr/local/etc/npmrc"
globalignorefile = "/usr/local/etc/npmignore"
group = 20
ham-it-up = false
heading = "npm"
https-proxy = null
if-present = false
ignore-prepublish = false
ignore-scripts = false
init-author-email = ""
init-author-name = ""
init-author-url = ""
init-license = "ISC"
init-module = "/Users/ale917k/.npm-init.js"
init-version = "1.0.0"
json = false
key = null
legacy-bundling = false
link = false
local-address = undefined
loglevel = "notice"
logs-max = 10
; long = false (overridden)
maxsockets = 50
message = "%s"
; metrics-registry = null (overridden)
node-options = null
node-version = "15.4.0"
noproxy = null
offline = false
onload-script = null
only = null
optional = true
otp = null
package-lock = true
package-lock-only = false
parseable = false
prefer-offline = false
prefer-online = false
; prefix = "/usr/local/Cellar/node/15.4.0" (overridden)
preid = ""
production = false
progress = true
proxy = null
read-only = false
rebuild-bundle = true
registry = "https://registry.npmjs.org/"
rollback = true
save = true
save-bundle = false
save-dev = false
save-exact = false
save-optional = false
save-prefix = "^"
save-prod = false
scope = ""
script-shell = null
scripts-prepend-node-path = "warn-only"
searchexclude = null
searchlimit = 20
searchopts = ""
searchstaleness = 900
send-metrics = false
shell = "/bin/zsh"
shrinkwrap = true
sign-git-commit = false
sign-git-tag = false
sso-poll-frequency = 500
sso-type = "oauth"
strict-ssl = true
tag = "latest"
tag-version-prefix = "v"
timing = false
tmp = "/var/folders/2t/36_q44_s62d1b57hnl0l8khw0000gn/T"
umask = 18
unicode = true
unsafe-perm = true
update-notifier = true
usage = false
user = 0
; user-agent = "npm/{npm-version} node/{node-version} {platform} {arch} {ci}" (overridden)
userconfig = "/Users/ale917k/.npmrc"
version = false
versions = false
viewer = "man"
Can it be the reason why it stopped building is because it's pointing to the wrong registry? And if so, how can that be fixed?
Bit on a side, I noticed as well that warning regarding the npm update; How can I update the npm package inside a docker image?
Any answer is highly appreciated, so thank you for your inputs!
Solution 1:[1]
Found out my company's server went down, bringing down related registries and similar.
That resulted in not being able to process the package installation, as for some reason I seemed to have dependency leftovers from the company custom registry.
In order to fix this, I:
Firstly created different npmrc profiles, to make sure we use the appropriate registry when working on a specific project - Ref can be found here
Have then deleted
node_modules
andpackage-lock.json
to cleanup leftoversSet the profile pointing to the open-source registry (https://registry.npmjs.org/) with
npmrc <profile>
(Optional): Check active registry profile by simply running
npmrc
Reinstalled all packages with
npm install
Rebuild docker-image with
docker-compose up --build
and that made the job
Hope this may help other people in the future
Solution 2:[2]
I have been encountering this same issue and later realized that I had a bad network connection which caused the ERR_SOCKET_TIMEOUT.
Its not due to
Run npm install -g [email protected]
to update!`. As you can see below its still up and running
Solution 3:[3]
How i solved the problem.
I noticed the issue start's acting up on after the package-lock.json
patched this way.
When you try installing locally on the PC you get:
pm WARN old lockfile
npm WARN old lockfile The package-lock.json file was created with an old version of npm,
npm WARN old lockfile so supplemental metadata must be fetched from the registry.
npm WARN old lockfile
npm WARN old lockfile This is a one-time fix-up, please be patient...
npm WARN old lockfile
....
# installs packages and finish
On docker npm ci
keeps timing out.
Solution
NB: Ensure your running the latest npm
and node
version on your PC.
sudo apt install npm nodejs
doesn't cut it.
Upgrade the node version:
- Install packages:
sudo apt install npm nodejs
- Run:
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
- Then:
sudo apt-get install -y nodejs
- Then Upgrade NPM :
sudo npm install -g npm@latest
After upgrading reinstall the packages like below:
- Remove
package-lock.json
- Remove node_modules
- Re-install the packages with
npm install
Now run your docker.
Solution 4:[4]
Try add tty: true, something like this
backend:
build:
context: ./backend
dockerfile: Dockerfile
tty: true
Solution 5:[5]
i have the same issue with Dockerfile node base image version 16, so downgrade to 14 solve the problem instead of "node:latest" use "node:14"
Solution 6:[6]
I was facing the same issue and none of the above solutions worked. I resolved it by package-lock.json file in Dockerfile before RUN npm run install command. So modifying the above Dockerfile as below solved it.
FROM node:latest
WORKDIR /usr/src/auth-starter-server
COPY package.json ./
COPY package-lock.json ./
RUN npm install
CMD ["sh"]
I guess adding package-lock.json before tremendously reduces the time to download packages and it's dependencies.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | ale917k |
Solution 2 | Shanka Somasiri |
Solution 3 | xaander1 |
Solution 4 | Rafael |
Solution 5 | Khyari Hamza |
Solution 6 | Naveen Kumar |