Express js npm install fails reason: getaddrinfo EAI_AGAIN - node.js

I have a Node.JS / Express.JS server-side program, which I run inside a Node.JS docker container. It was working for the last ~2 years, now it produces a strange error upon installing.
I use docker-compose to install / run the program.
The Dockerfile:
FROM node:10
ARG PORT
ENV PORT ${PORT}
# Create app directory
ADD backend /app
WORKDIR /app
RUN npm ci --only=production
EXPOSE $PORT
CMD ["npm", "run", "start-prod"]
When I try to install it (with docker-compose, it fails every time, with this message:
Building backend
Step 1/10 : FROM node:10
---> a8441ebf4e4d
Step 2/10 : ARG PORT
---> Using cache
---> d985b4141806
Step 3/10 : ENV PORT ${PORT}
---> Using cache
---> e053f5bb49fa
Step 4/10 : ADD backend /app
---> Using cache
---> 409d0ab160ea
Step 5/10 : WORKDIR /app
---> Using cache
---> b7aad109486b
Step 6/10 : RUN cp environment-prod-docker.js environment.js
---> Using cache
---> 218df5ff1cff
Step 7/10 : RUN cat environment.js
---> Using cache
---> 87965e20e57e
Step 8/10 : RUN npm ci --only=production
---> Running in 9352da8c367e
npm WARN prepare removing existing node_modules/ before installation
npm ERR! code EAI_AGAIN
npm ERR! errno EAI_AGAIN
npm ERR! request to https://registry.npmjs.org/bcryptjs/-/bcryptjs-2.4.3.tgz failed, reason: getaddrinfo EAI_AGAIN registry.npmjs.org registry.npmjs.org:443
I have read that it might be possible that this is a proxy error. I have tried to change to my home wifi, my home cable, and mobile data: still produced the same error. If I copy the link, I can download the said .tgz file from the browser.

This problem disappeared after two days (same network, same computer etc.)
It is probably related to what the comments indicated.

Simple DNS issue, check /etc/resolv.conf and add this:
"nameserver 8.8.8.8"

Related

docker-compose throws error with getaddrinfo EAI_AGAIN registry.npmjs.org

background:
I'm on Ubuntu 20.04.
I'm trying to build images using docker-compose.
This is my docker-compose file
version: "3.8"
services:
web:
build: ./frontend
network_mode: "host"
ports:
- 3000:3000
api:
build: ./backend
network_mode: "host"
ports:
- 3001:3001
environment:
DB_URL: mongodb://db/foo-app
db:
image: mongo:4.0-xenial
ports:
- 27017:27017
volumes:
- foo-app-data:/data/db
volumes:
foo-app-data:
And below are my two Dockerfile files:
# ./backend file
FROM node:16.3.0-alpine3.13
RUN addgroup app && adduser -S -G app app
WORKDIR /app
USER root
COPY package*.json ./
# --- debug try
RUN npm cache clean --force
RUN npm config set registry https://registry.npmjs.org/
RUN npm install -g #angular/cli
# ---
RUN npm install
COPY . .
RUN chown app:app /app
USER app
EXPOSE 3001
CMD ["npm", "start"]
# ./frontend file
FROM node:16.3.0-alpine3.13
RUN addgroup app && adduser -S -G app app
WORKDIR /app
USER root
COPY package*.json ./
# --- debug try
RUN npm cache clean --force
RUN npm config set registry https://registry.npmjs.org/
# ---
RUN npm install
COPY . .
RUN chown app:app /app
USER app
EXPOSE 3000
CMD ["npm", "start"]
error:
When I run docker-compose build, the below error is thrown:
Step 8/14 : RUN npm install -g #angular/cli
---> Running in ce221adb18f6
npm ERR! code EAI_AGAIN
npm ERR! syscall getaddrinfo
npm ERR! errno EAI_AGAIN
npm ERR! request to https://registry.npmjs.org/#angular%2fcli failed, reason: getaddrinfo EAI_AGAIN registry.npmjs.org
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2021-06-13T10_19_13_600Z-debug.log
The command '/bin/sh -c npm install -g #angular/cli' returned a non-zero code: 1
ERROR: Service 'web' failed to build : Build failed
what i have tried so far:
when I was building each docker file manually, I was getting the same error, until I used the --network=host. It means, when I build the images with docker build --network=host, it works fine. So I tried to add network_mode= "host" to my docker-compose file but it doesn't solve the issue.
and for God's sake, read this before marking this question as duplicate:
this post here propose a solution for docker and not docker compose. When I ping registry.npmjs.org, the connection works fine.
this post here propose to docker-compose up, which will throw the exact same error as I have here.
this post here doens't work, i have already restarted docker multiple times. And on top of that, I clean all docker images after the error is thrown to make sure the next time I build, nothing is used from the cache.
this post here doesn't work either. I tried to (1) remove the proxies from the npm config, and also add the additional lines npm cache clean --force and npm config set registry https://registry.npmjs.org/ in my Dockerfile. Nothing works.
this post here not only doesn't solve the problem, but also it doesn't really explain well the reason why the solution is being proposed.
and this post here i don't even know how this answer is allowed on StackOverflow.
If you use network_mode with host value, you can't mix it with port mapping: https://docs.docker.com/compose/compose-file/compose-file-v3/#ports
Then, or you change your network_mode to bridge or you drop your port mapping for each service.
Add following Variable to .zshrc file in mac
export NODE_OPTIONS=--openssl-legacy-provider

Unable to run NodeJS microservices through Docker containers

I was getting started with Docker, created couple of tiny Express(NodeJS) services.
Plan is to run the microservices inside Docker containers and then establish a inter-communication between them using Docker Compose service names.
Here is the Github repo of this simple project. Am able to build images with below commands :
cd books
docker build -t node-micro/books .
cd auth
docker build -t node-micro/auth .
Commands to start containers :
docker run -d -p 6677:6677 node-micro/auth
docker run -d -p 7766:7766 node-micro/books
But when i hit below URL's there is no response, which was working fine couple of day's before :
http://localhost:6677/
http://localhost:7766/
And have no clue what's happening with docker compose. No luck on accessing same URL's as mentioned above after stoping all containers, delete all images & ran this command :
docker-compose up -d
Need some help on bringing up the containers individually and also through docker-compose.
I can see in each of your micro-service, your application is running on ports 3000 in the container but you are exposing 7766 and 6677 in your docker-compose.yml
Please check the below docker-compose.yml
version: '3'
services:
books:
build: './books'
ports:
- "7766:3000"
depends_on:
- auth
auth:
build: './auth'
ports:
- "6677:3005"
and then run the below command
docker-compose up --build
--build will build the images as well.
Then, you should be able to access the service
http://localhost:6677/
http://localhost:7766/
Output
docker-compose up --build
Creating network "node_microservices_default" with the default driver
Building auth
Step 1/7 : FROM node:10-alpine
---> 0aa7bb41deca
Step 2/7 : WORKDIR /usr
---> Running in a1dc67b70538
Removing intermediate container a1dc67b70538
---> 5fc74fc80a14
Step 3/7 : COPY package*.json ./
---> 454f1b7aba87
Step 4/7 : RUN npm install
---> Running in a24eea8b79d4
npm WARN auth#1.0.0 No description
npm WARN auth#1.0.0 No repository field.
added 50 packages from 37 contributors and audited 50 packages in 8.58s
found 0 vulnerabilities
Removing intermediate container a24eea8b79d4
---> 31b31ff4516e
Step 5/7 : COPY . .
---> 1eeaa8e70300
Step 6/7 : EXPOSE 3000
---> Running in fc798167dbcd
Removing intermediate container fc798167dbcd
---> 4d964d25c099
Step 7/7 : CMD ["npm", "start"]
---> Running in 3c28d92f9ef6
Removing intermediate container 3c28d92f9ef6
---> 514f68d11d7c
Successfully built 514f68d11d7c
Successfully tagged node_microservices_auth:latest
Building books
Step 1/7 : FROM node:10-alpine
---> 0aa7bb41deca
Step 2/7 : WORKDIR /usr
---> Using cache
---> 5fc74fc80a14
Step 3/7 : COPY package*.json ./
---> 56addb6c75a5
Step 4/7 : RUN npm install
---> Running in 4864fb7a171c
npm WARN books#1.0.0 No description
npm WARN books#1.0.0 No repository field.
added 50 packages from 37 contributors and audited 50 packages in 5.111s
found 0 vulnerabilities
Removing intermediate container 4864fb7a171c
---> 82bb2cd54357
Step 5/7 : COPY . .
---> 12893a93e82e
Step 6/7 : EXPOSE 3000
---> Running in 1301e29dbd52
Removing intermediate container 1301e29dbd52
---> c26948ebcb3b
Step 7/7 : CMD ["npm", "start"]
---> Running in db948866a121
Removing intermediate container db948866a121
---> 703b901d7bc4
Successfully built 703b901d7bc4
Successfully tagged node_microservices_books:latest
Creating node_microservices_auth_1 ... done
Creating node_microservices_books_1 ... done
Attaching to node_microservices_auth_1, node_microservices_books_1
auth_1 |
auth_1 | > auth#1.0.0 start /usr
auth_1 | > node index.js
auth_1 |
auth_1 | Running on port 3005
auth_1 | --------------------------
books_1 |
books_1 | > books#1.0.0 start /usr
books_1 | > node index.js
books_1 |
books_1 | Running on port 3000
books_1 | --------------------------
You are exposing from both Dockerfiles the port 3000. Replace the port for each microservice in docker-compose.yml file.
- "7766:3000"
- "6677:3000"
The mapping for ports is wrong. In both Dockerfile you are exposing the port 3000.
So you must to map the ports 6677 and 7766 to the exposed port on the Dockerfile.
To fix this, on your docker-compose.yml you must to config ports like this:
version: '3'
services:
books:
build: './books'
ports:
- "7766:3000"
depends_on:
- auth
auth:
build: './auth'
ports:
- "6677:3000"

Docker compose - Unable to connect to mysql - App builds before mysql?

I have a docker compose file spinning up 3 apps. mysql, phpmyadmin & a nodejs app. You will find the compose file below.
The nodejs app have sequilizeJS which requires to run migration & seed command when It initializes.
When I run docker-compose up --build the build fails because mysql returns with error getaddrinfo ENOTFOUND mysql
I can not figure out what I did wrong in the compose file as it looks okay to me.
both phpmyadmin and auth app requires the mysql so I have added mysql to depends_on section. It seems from the log file that composer trys to build auth before creating mysql.
Logs
Creating network "updials-auth_default" with the default driver
Building auth
Step 1/8 : FROM node:12.14.0
---> 6b5991bf650f
Step 2/8 : WORKDIR /var/www
---> Using cache
---> 21c89e8b8059
Step 3/8 : COPY . .
---> 73072a4bddb5
Step 4/8 : COPY package.json /usr/share/app
---> 886992b71802
Step 5/8 : EXPOSE 3001
---> Running in cd7c14183427
Removing intermediate container cd7c14183427
---> b93bcdf8c653
Step 6/8 : RUN npm install
---> Running in 4b6d75b77bab
npm WARN deprecated mkdirp#0.5.1: Legacy versions of mkdirp are no longer supported. Please update to mkdirp 1.x. (Note that the API surface has changed to use Promises in 1.x.)
npm WARN deprecated request#2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
npm WARN rm not removing /var/www/node_modules/.bin/rimraf as it wasn't installed by /var/www/node_modules/rimraf
> bcrypt#3.0.8 install /var/www/node_modules/bcrypt
> node-pre-gyp install --fallback-to-build
node-pre-gyp WARN Using request for node-pre-gyp https download
[bcrypt] Success: "/var/www/node_modules/bcrypt/lib/binding/bcrypt_lib.node" is installed via remote
> ejs#2.7.4 postinstall /var/www/node_modules/ejs
> node ./postinstall.js
Thank you for installing EJS: built with the Jake JavaScript build tool (https://jakejs.com/)
npm notice created a lockfile as package-lock.json. You should commit this file.
added 18 packages from 3 contributors, removed 9 packages, updated 467 packages and audited 1563 packages in 51.173s
22 packages are looking for funding
run `npm fund` for details
found 5 low severity vulnerabilities
run `npm audit fix` to fix them, or `npm audit` for details
Removing intermediate container 4b6d75b77bab
---> f3c15392ccc2
Step 7/8 : RUN npm run migrate && npm run seed
---> Running in cd58f889c907
> updials-auth#0.0.2 migrate /var/www
> npx sequelize-cli db:migrate
npx: installed 81 in 8.76s
Sequelize CLI [Node: 12.14.0, CLI: 5.5.1, ORM: 5.21.5]
Loaded configuration file "config/config.js".
Using environment "development".
ERROR: getaddrinfo ENOTFOUND mysql
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! updials-auth#0.0.2 migrate: `npx sequelize-cli db:migrate`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the updials-auth#0.0.2 migrate script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2020-03-29T15_36_20_888Z-debug.log
ERROR: Service 'auth' failed to build: The command '/bin/sh -c npm run migrate && npm run seed' returned a non-zero code: 1
Dockerfile
FROM node:12.14.0
#USER node
WORKDIR /var/www
COPY . .
COPY package.json /usr/share/app
#COPY package.lock.json /usr/share/app
EXPOSE 3001
RUN npm install
RUN npm run migrate && npm run seed
CMD ["npm", "start"]
docker-compose.yml
version: '3.7'
services:
mysql:
container_name: updials-auth-mysql
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: 'password'
MYSQL_DATABASE: 'updials'
MYSQL_USER: 'updials'
MYSQL_PASSWORD: 'password'
volumes:
- database:/var/lib/mysql
auth:
container_name: updials-auth
restart: always
depends_on:
- mysql
build: .
ports:
- '3001:5002'
environment:
DB_HOST: 'mysql'
DB_USER: 'updials'
DB_PASS: 'password'
DB_NAME: 'updials'
phpmyadmin:
container_name: phpmyadmin-updials-auth
restart: always
image: phpmyadmin/phpmyadmin:5.0.2
depends_on:
- mysql
environment:
MYSQL_USER: updials
MYSQL_PASSWORD: password
ports:
- '4000:8080'
volumes:
database:
driver: local
driver_opts:
type: 'none'
o: 'bind'
device: '/home/sisir/docker-databases/updials-auth'
The Dockerfile can never access a database, volumes, or other resources declared in the docker-compose.yml (outside that service's immediate build: block). The build runs as a separate stage; it doesn't get attached to the Compose network.
(Imagine running docker build; docker push on one system, and on a second system specifying the matching image:. In this setup the build-time system can't access the run-time database, and that's the basic model you should have in mind. More directly you can delete and recreate your mysql container without rebuilding your auth image.)
The typical pattern to make this work is to write an entrypoint script. This becomes the main command your container runs; it gets passed the Dockerfile CMD (or Docker Compose command:) as command-line arguments. Since this runs at the point the container starts up, it does have access to the database, networks, environment variables, etc.
#!/bin/sh
set -e # Stop on any error
npm run migrate # Run migrations
npm run seed # Preload initial data
exec "$#" # Run the command as the main container process
In your Dockerfile put this script as the ENTRYPOINT. You must use the JSON-array form of ENTRYPOINT here.
FROM node:12.14.0
WORKDIR /var/www
# Install dependencies first to save time on rebuild
COPY package.json .
RUN npm install
COPY . .
EXPOSE 3001
RUN chmod +x entrypoint.sh # if required
ENTRYPOINT ["./entrypoint.sh"]
CMD ["npm", "start"]

Browser not rendering Dockerized AngularJS app

So, I'm trying to dockerize an AngularJS app for some practice. Here is my repo https://github.com/Nigel33/angularJS_docker_test:
(Its from the official Angular-phonecat repo but i added Dockerfile and docker-compose)
Here's my Dockerfile:
FROM node:lts-alpine
RUN npm install -g http-server
WORKDIR /app
COPY package*.json ./
COPY . .
RUN npm install && \
npm run start
EXPOSE 8000
CMD [ "http-server", "dist" ]
Here's my docker-compose.yml:
version: '3'
services:
front:
build: .
command: bash -c "npm start"
volumes:
- .:/angular-phonecat
ports:
- "8000:8000"
When I do docker-compose build then docker-compose up, it seems to run based on the logs:
Building front
Step 1/8 : FROM node:lts-alpine
---> b95baba1cfdb
Step 2/8 : RUN npm install -g http-server
---> Using cache
---> 082793f64510
Step 3/8 : WORKDIR /app
---> Using cache
---> e76e23fa5a08
Step 4/8 : COPY package*.json ./
---> Using cache
---> 9960651e0929
Step 5/8 : COPY . .
---> c06eaebcfb38
Step 6/8 : RUN npm install && npm run start
---> Running in e2622118028d
npm WARN lifecycle angular-phonecat#0.0.0~postinstall: cannot run in wd angular-phonecat#0.0.0 npm run copy-libs (wd=/app)
audited 3777 packages in 6.409s
found 57 vulnerabilities (2 low, 2 moderate, 53 high)
run `npm audit fix` to fix them, or `npm audit` for details
> angular-phonecat#0.0.0 prestart /app
> npm install
npm WARN lifecycle angular-phonecat#0.0.0~postinstall: cannot run in wd angular-phonecat#0.0.0 npm run copy-libs (wd=/app)
audited 3777 packages in 5.338s
found 57 vulnerabilities (2 low, 2 moderate, 53 high)
run `npm audit fix` to fix them, or `npm audit` for details
> angular-phonecat#0.0.0 start /app
> http-server ./app -a localhost -p 8000 -c-1
Starting up http-server, serving ./app
Available on:
http://localhost:8000
Hit CTRL-C to stop the server
But when I open up my browser and navigate to localhost:8000, I get the "this site can't be reached" page. Any idea whats wrong?? Thank you!
UPDATES
Dockerfile
FROM node:lts-alpine
RUN npm install -g http-server
WORKDIR /app
COPY package*.json ./
COPY . .
RUN npm install
EXPOSE 8000
CMD ["npm", "run", "start"]
Docker-compose:
version: '3'
services:
front:
build: .
ports:
- "8000:8000"
Terminal
Building front
Step 1/8 : FROM node:lts-alpine
---> b95baba1cfdb
Step 2/8 : RUN npm install -g http-server
---> Using cache
---> 082793f64510
Step 3/8 : WORKDIR /app
---> Using cache
---> e76e23fa5a08
Step 4/8 : COPY package*.json ./
---> Using cache
---> 9960651e0929
Step 5/8 : COPY . .
---> Using cache
---> 35b3086dc43d
Step 6/8 : RUN npm install
---> Using cache
---> 961eddc4df33
Step 7/8 : EXPOSE 8000
---> Using cache
---> ede5977a2a0e
Step 8/8 : CMD ["npm", "run", "start"]
---> Using cache
---> efc2186c9bf2
Successfully built efc2186c9bf2
Successfully tagged angular-phonecat_front:latest
Chriss-Air:angular-phonecat chrisshopline$ docker-compose up
Creating network "angular-phonecat_default" with the default driver
Creating angular-phonecat_front_1 ... done
Attaching to angular-phonecat_front_1
front_1 |
front_1 | > angular-phonecat#0.0.0 prestart /app
front_1 | > npm install
front_1 |
front_1 | npm WARN lifecycle angular-phonecat#0.0.0~postinstall: cannot run in wd angular-phonecat#0.0.0 npm run copy-libs (wd=/app)
front_1 | audited 3777 packages in 4.622s
front_1 | found 57 vulnerabilities (2 low, 2 moderate, 53 high)
front_1 | run `npm audit fix` to fix them, or `npm audit` for details
front_1 |
front_1 | > angular-phonecat#0.0.0 start /app
front_1 | > http-server ./app -a localhost -p 8000 -c-1
front_1 |
front_1 | Starting up http-server, serving ./app
front_1 | Available on:
front_1 | http://localhost:8000
front_1 | Hit CTRL-C to stop the server
Still having the same issue though :-/
The above logs from the Docker build, not from the container. As the build process stuck at
RUN npm install && \
npm run start
You do not need to start application at Dockerfile RUN command. It should be in CMD or Entrypoint
Update your Dockerfile and it should work.
RUN npm install
Also, Something in Docker-compose should be clear.
command: bash -c "npm start"
volumes:
- .:/angular-phonecat
It will override the CMD that is defined in Dockerfile CMD [ "http-server", "dist" ].
It will also override - .:/angular-phonecat. you do not need volume if you already copied the app in the build stage.
update:
I see that you are setting address in your package.json to localhost. It should be 0.0.0.0 or you can set CMD http-server . or http-server -a 0.0.0.0 -p 8000 -c-1. or http-server -d /app/dist/ -a 0.0.0.0 -p 8000 -c-1.
Also, the dist does not exist in your repo, I assume it exist in your build directory where you copy during docker build.

Autotesting docker images with docker-compose.test : missing environment variables

I have a Node/Express app and I am using https://www.npmjs.com/package/dotenv-safe to include environment variables for dev and testing.
I am pushing my Docker repository to https://cloud.docker.com/app/gkatsanos/ and during AutoTesting (I have a docker-compose.test.yml), It seems that the environment variables are not found:
Successfully built 69f35563f12e
Successfully tagged gkatsanos/server:latest
Starting Test in docker-compose.test.yml...
Building sut
Step 1/10 : FROM node:8-alpine
---> 4db2697ce114
Step 2/10 : EXPOSE 3000
---> Using cache
---> ef9e0a8a09e1
Step 3/10 : ARG NODE_ENV
---> Using cache
---> cc6143bf9bee
Step 4/10 : ENV NODE_ENV $NODE_ENV
---> Using cache
---> 6477a9e9657f
Step 5/10 : RUN mkdir /app
---> Using cache
---> e9fff66cf3da
Step 6/10 : WORKDIR /app
---> Using cache
---> da82362255c6
Step 7/10 : ADD package.json yarn.lock seed.js /app/
---> Using cache
---> 97a842faeb2a
Step 8/10 : RUN yarn --pure-lockfile
---> Using cache
---> 6745c0d8c64c
Step 9/10 : ADD . /app
---> Using cache
---> 06d46eb4a57b
Step 10/10 : CMD yarn start
---> Using cache
---> 69f35563f12e
Successfully built 69f35563f12e
Successfully tagged b5wqkysdhyuqf8uz4kyreyn_sut:latest
Creating network "b5wqkysdhyuqf8uz4kyreyn_default" with the default driver
Creating b5wqkysdhyuqf8uz4kyreyn_sut_1 ...
Creating b5wqkysdhyuqf8uz4kyreyn_sut_1
Creating b5wqkysdhyuqf8uz4kyreyn_sut_1 ... done
yarn run v1.3.2
$ NODE_ENV=test nyc --reporter=html --reporter=text mocha --timeout 20000 --recursive src/tests
/app/node_modules/dotenv-safe/index.js:37
throw new MissingEnvVarsError(allowEmptyValues, options.path || '.env', sample, missing, dotenvResult.error);
Locally the docker building and running works.
That's my dockerfile:
FROM node:8-alpine
EXPOSE 3000
ARG NODE_ENV
ENV NODE_ENV $NODE_ENV
RUN mkdir /app
WORKDIR /app
ADD package.json yarn.lock seed.js /app/
RUN yarn --pure-lockfile
ADD . /app
CMD ["yarn", "start"]
and my docker-compose.test.yml:
version: "3"
services:
sut:
build: .
command: yarn test
By the way, cloud.docker.com has a place in its UI that allows for environment variables to be added:

Resources