Search code examples
reactjsdockerdocker-composedocker-machine

A service is failing to build with the command '/bin/sh -c npm ci'


I am setting up a container that will host my react client code but the build fails on the local machine with an exit code of 1. How should I go about troubleshooting this error?

I have tried restarting the docker-machine and docker clients.

docker-machine version 0.16.0, build 702c267f

docker-compose version 1.17.1, build unknown

Docker version 19.03.1, build 74b1e89

My docker-compose.yml:

version: '3.3'

services:

client:
    build: 
      context: ./services/client
      dockerfile: Dockerfile
    volumes: 
      - './services/client:/usr/src/app'
      - '/usr/src/app/node_modules'
    ports: 
      - 3007:3000
    environment: 
      - NODE_ENV=development
      - REACT_APP_USERS_SERVICE_URL=${REACT_APP_USERS_SERVICE_URL}
    depends_on: 
      - users

My Dockerfile:

# base image
FROM node:11.12.0-alpine

# set working directory
WORKDIR /usr/src/app

# add /usr/src/app/node_modules/.bin to $PATH
ENV PATH /usr/src/app/node_modules/.bin:$PATH

# install and cache app dependencies
COPY package.json /usr/src/app/package.json
COPY package-lock.json /usr/src/app/package-lock.json
RUN npm ci
RUN npm install react-scripts -g --silent

# start app
CMD ["npm", "start"]

I am expecting the service "client" to successfully build but I keep getting the following error.

npm ERR! code EAI_AGAIN
npm ERR! errno EAI_AGAIN
npm ERR! request to https://registry.npmjs.org/enzyme/-/enzyme-3.10.0.tgz failed, reason: getaddrinfo EAI_AGAIN registry.npmjs.org registry.npmjs.org:443

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2019-08-15T05_55_38_024Z-debug.log
ERROR: Service 'client' failed to build: The command '/bin/sh -c npm ci' returned a non-zero code: 1

Solution

  • the error npm ERR! errno EAI_AGAIN means a DNS error I suggest you to do one of the follwong:

    build your image using the host network:

    version: '3.4'
    
    services:
    
    client:
        build: 
          context: ./services/client
          dockerfile: Dockerfile
          network: host
        volumes: 
          - './services/client:/usr/src/app'
          - '/usr/src/app/node_modules'
        ports: 
          - 3007:3000
        environment: 
          - NODE_ENV=development
          - REACT_APP_USERS_SERVICE_URL=${REACT_APP_USERS_SERVICE_URL}
        depends_on: 
          - users
    

    or add dns setting to your daemon.json file:

    {
        "dns": ["8.8.8.8"]
    }