Search code examples
node.jsdockeramazon-ec2node-sassreact-scripts

Docker image failing to build on AWS EC2 linux but works when built on Windows and Mac


When I run docker-compose build minexample with the following 3 files on Mac or Windows, the image is built successfully. But when I try to do the same thing on a AWS Linux 2 EC2 instance, it fails when running node-sass's install script (see below).

After much trial and error over 2 days, I realized that the issue goes away if I lower react-scripts version to "3.0.0". That would also mean I need to lower eslint in my dev dependency (which I don't include in the minimal example) from 7.12.0 to 5.16.0.

I have a few questions:

  1. How can the docker build succeed on Mac/Windows, yet fail on Linux? I thought containers were supposed to be an isolated environment yet somehow the host OS affected the outcome here.
  2. How do I figure out which node package versions are in conflict? It took a lot of time via trial and error to figure out that lowering react-script's version fixed (or hid) the problem.

package.json

{
  "name": "minexample",
  "version": "1.0.0",
  "dependencies": {
    "node-sass": "4.12.0",
    "react": "16.14.0",
    "react-dom": "16.14.0",
    "react-router": "5.0.0",
    "react-router-dom": "5.0.0",
    "react-scripts": "4.0.2"
  },
  "devDependencies": {
  },
  "scripts": {
    "start": "react-scripts start"
  }
}

Dockerfile

FROM node:10.16.0-alpine AS build

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
ENV PATH /usr/src/app/node_modules/.bin:$PATH

# cache dependencies
COPY ./package.json /usr/src/app/package.json
RUN npm install

docker-compose.yml

version: '3.7'

services:
  minexample:
    build:
      context: ./
      dockerfile: Dockerfile
    container_name: minexample

Error

> node-sass@4.12.0 install /usr/src/app/node_modules/node-sass 
> node scripts/install.js

The command '/bin/sh -c npm install' returned a non-zero code: 1
ERROR: Service 'minexample' failed to build

Solution

  • Based on the comments.

    I tried to replicate the issue, but in my tests, the docker image is built successfully.

    Instance used, t2.small (have not tested using t2.micro).

    Amazon Linux 2:

    NAME="Amazon Linux"
    VERSION="2"
    ID="amzn"
    ID_LIKE="centos rhel fedora"
    VERSION_ID="2"
    PRETTY_NAME="Amazon Linux 2"
    ANSI_COLOR="0;33"
    CPE_NAME="cpe:2.3:o:amazon:amazon_linux:2"
    HOME_URL="https://amazonlinux.com/"
    

    Docker

    docker --version
    Docker version 19.03.13-ce, build 4484c46
    

    docker-compose

    docker-compose --version
    docker-compose version 1.28.4, build cabd5cfb
    

    build image

    docker image ls
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    a_minexample        latest              82968af21096        24 minutes ago      426MB
    node                10.16.0-alpine      9dfa73010b19        20 months ago       75.3MB