5

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:

  1. this post here propose a solution for docker and not docker compose. When I ping registry.npmjs.org, the connection works fine.
  2. this post here propose to docker-compose up, which will throw the exact same error as I have here.
  3. 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.
  4. 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.
  5. 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.
  6. and this post here i don't even know how this answer is allowed on StackOverflow.
asa
  • 675
  • 2
  • 7
  • 17

2 Answers2

0

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.

Néstor
  • 570
  • 2
  • 8
  • 22
0

Add following Variable to .zshrc file in mac

export NODE_OPTIONS=--openssl-legacy-provider
Akshay Vijay Jain
  • 13,461
  • 8
  • 60
  • 73