dragansr
dragansr

Reputation: 490

how to fix "invalid instruction" error when running node.js in Docker, with sharp.js lib

When running node.js in Docker, with sharp.js lib I am getting "invalid instruction" error and it exits with code 1.

This is likely related to "libvips" used by sharp.js, that requires C compiler. I tried using various base Docker images, and so far all get same error. Any suggestion would be helpful.

Here is minimum reproducible set:

When const sharp = require("sharp"); is commented out it works.

When it is included, getting this error Illegal instruction

Dockerfile

FROM node
RUN mkdir /app
WORKDIR /app
COPY . ./
RUN npm install
EXPOSE 4004
CMD ["node", "test.js"]

test.js

const app = require("express")();
const sharp = require("sharp");

app.get("/", (req, res) => {
  res.send(`time: ${new Date()}`);
});

app.listen(4004, () => console.log(`listening on port 4004`));

package.json

{
  "dependencies": {
    "axios": "^0.24.0",
    "express": "^4.17.1",
    "fast-csv": "^4.1.1",
    "pdfkit": "^0.13.0",
    "sharp": "^0.29.3"
  },
  "devDependencies": {
    "nodemon": "^2.0.15"
  }
}

To build container image:

docker build -t test .

To start for interactive testing:

docker run -d --name test test tail -f /dev/null
docker exec -it test sh

then node test.js

When "sharp.js" is required, getting error Illegal instruction otherwise not.

The comments suggest that this could happen when "alpine" base image is used, but in this case it happens with other node.js Docker images, too.

Hope this makes it a bit clearer.

The same sharp lib and code without outside of container without issues.

Upvotes: 2

Views: 1351

Answers (1)

dragansr
dragansr

Reputation: 490

This issue is related to CPU instructions used by current version sharp.js lib that are not supported by some older processors. Resolved by setting exact older version of sharp.js lib, based on this useful answer here:

NodeJS: Illegal instruction (core dumped) Error after using sharp library

this solved issue

npm i [email protected] --save-exact

Interesting: it now works both with FROM node, as well as FROM node:slim and even FROM node:lts-alpine, but FROM node:alpine is not supported, it reports missing Python. Still, lts-alpine is quite small.

Hope this info can be useful for somebody else, too.

Upvotes: 1

Related Questions