sharp icon indicating copy to clipboard operation
sharp copied to clipboard

Sharp not running inside Docker container on Apple M1 machine

Open shedworth opened this issue 5 years ago • 12 comments

docker-compose --build completes successfully. import sharp from "sharp" works inside my tests. Attemping to use the imported "sharp" inside my tests causes the test container to crash with the following error:

test_1           |   ●  process.exit called with "1"
test_1           |
test_1           |       at Object.<anonymous> (src/lambdas/detectExplicitImages/node_modules/sharp/lib/sharp.js:23:11)
test_1           |       at Object.<anonymous> (src/lambdas/detectExplicitImages/node_modules/sharp/lib/constructor.js:8:1)
api_test_1 exited with code 1

Machine is M1 Macbook Pro.

My Dockerfile:

FROM node:16-alpine3.11
WORKDIR /app
COPY ./src ./src
COPY ./test ./test
COPY ./migrations ./migrations
COPY ./scripts ./scripts
COPY ./package.json ./package.json
COPY ./package-lock.json ./package-lock.json
COPY ./knexfile.js ./knexfile.js
COPY ./tsconfig.json ./tsconfig.json
COPY ./tsconfig.production.json ./tsconfig.production.json
RUN npm config set unsafe-perm true
RUN npm install --verbose
RUN npm run build
COPY ./knexfile.js ./build/knexfile.js
EXPOSE 8000

CMD ["node", "build/src/index.js"]

shedworth avatar Oct 01 '21 10:10 shedworth

There should be an message logged to stderr that will tell you more about why it failed.

https://github.com/lovell/sharp/blob/f37ca8249afcd26777b7e3e1d0192640572341ef/lib/sharp.js#L22-L23

Please can you provide it here.

Is this running native ARM64 inside the container, or Rosetta-emulated x64? Which version of sharp? Does this occur with a newer version of Alpine (e.g. 3.14 is available, and 3.11 is almost end-of-life).

lovell avatar Oct 01 '21 12:10 lovell

Thanks for your reply.

Sharp actually installs fine without error - sorry if that wasn't clear. The Docker container builds fine too. It's when I actually import/use Sharp that it throws the error and crashes the container:

import sharp from "sharp";
...
await sharp(inputFilePath).resize({ width: 2048 }).toFile(outputFilePath);
...

It's running native ARM64.

Sharp version: 0.29.1

I still get the same behaviour with Alpine 3.14

shedworth avatar Oct 04 '21 07:10 shedworth

Please can you ensure your container is logging stderr so you can access the error message, which will reveal why the process is ending. (It's not crashing, but rather is a controlled exit to prevent a crash.)

lovell avatar Oct 04 '21 09:10 lovell

Occasionally (and seemingly at random) it gives me:

test_1           |   ●  process.exit called with "1"
test_1           |
test_1           |       at Object.<anonymous> (src/lambdas/detectExplicitImages/node_modules/sharp/lib/sharp.js:23:11)
test_1           |       at Object.<anonymous> (src/lambdas/detectExplicitImages/node_modules/sharp/lib/constructor.js:8:1)
test_1           |   console.error
test_1           |
test_1           |     Something went wrong installing the "sharp" module
test_1           |
test_1           |     Cannot find module '../build/Release/sharp-linuxmusl-arm64v8.node' from 'src/lambdas/detectExplicitImages/node_modules/sharp/lib/sharp.js'
test_1           |
test_1           |     Possible solutions:
test_1           |     - Install with the --verbose flag and look for errors: "npm install --ignore-scripts=false --verbose sharp"
test_1           |     - Install for the current runtime: "npm install --platform=linux --arch=arm64 sharp"
test_1           |     - Consult the installation documentation: https://sharp.pixelplumbing.com/install
test_1           |
test_1           |       at Object.<anonymous> (src/lambdas/detectExplicitImages/node_modules/sharp/lib/sharp.js:22:11)
test_1           |       at Object.<anonymous> (src/lambdas/detectExplicitImages/node_modules/sharp/lib/constructor.js:8:1)

I did try the suggested solutions but none of them helped.

shedworth avatar Oct 04 '21 10:10 shedworth

Cannot find module '../build/Release/sharp-linuxmusl-arm64v8.node' from 'src/lambdas/detectExplicitImages/node_modules/sharp/lib/sharp.js'

Does the file src/lambdas/detectExplicitImages/node_modules/sharp/build/Release/sharp-linuxmusl-arm64v8.node exist inside the container?

lovell avatar Oct 04 '21 12:10 lovell

No, the contents of src/lambdas/detectExplicitImages/node_modules/sharp/build/Release/ inside the container are: nothing.a, obj.target/ and sharp-darwin-arm64v8.node*

Really appreciate your quick responses btw.

shedworth avatar Oct 04 '21 12:10 shedworth

This suggests node_modules inside the container was populated by running npm install on macOS rather than when building the container. Watch out for volume mappings or COPY commands that might override this directory.

lovell avatar Oct 04 '21 14:10 lovell

yep, a misconfigured monorepo was the root cause for this. Thanks for your help and patience @lovell

Dakuan avatar Oct 05 '21 08:10 Dakuan

Thanks for your help @lovell. Hope this is of use to someone in the future.

shedworth avatar Oct 05 '21 09:10 shedworth

I am seeing this issue. However src/lambdas/detectExplicitImages/node_modules/sharp/build/Release/sharp-linuxmusl-arm64v8.node does exist.

Cam4dams avatar Jun 10 '22 00:06 Cam4dams

I am seeing this issue. However src/lambdas/detectExplicitImages/node_modules/sharp/build/Release/sharp-linuxmusl-arm64v8.node does exist.

use RUN npm rebuild --platform=darwin --arch=arm64 sharp, see https://sharp.pixelplumbing.com/install#cross-platform

RickCole21 avatar Feb 20 '23 08:02 RickCole21

Thanks @RickCole21, I should've removed the comment after I resolved the problem. My npm libraries were getting copied into the docker container, which was causing the binaries issue. I found two ways to solve this, (1) be more selective with the directories I was copying into the container via the Dockerfile or (2) delete the node_modules directory after it was copied in. Thanks for following up on this, I appreciate it.

Cam4dams avatar Feb 20 '23 18:02 Cam4dams