Hi!
I'm trying to run sharp inside a node:alpine container but I'm having lots of trouble, after reading issue #988 I've managed to get it to compile but it crashes at runtime.
This is the dockerfile I'm using.
FROM node:alpine
RUN apk update && apk add --update nodejs-npm
RUN apk add vips-dev fftw-dev --update-cache --repository https://dl-3.alpinelinux.org/alpine/edge/testing/
RUN apk --no-cache add -t .build-deps \
gcc \
libc-dev\
make \
libpng-dev \
g++ \
git && \
mkdir -p /opt/google && \
cd /opt/google && \
git clone https://github.com/google/guetzli.git && \
cd guetzli && \
make && \
cd /opt && \
mv /opt/google/guetzli/bin/Release/guetzli . && \
rm -fr /opt/google && \
apk --no-cache add libpng libstdc++
ENV TZ=Europe/Dublin
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
WORKDIR /usr/server
COPY ./shared /usr/app/shared
COPY ./server /usr/app/server
WORKDIR /usr/app/shared
RUN npm install
WORKDIR /usr/app/server
RUN npm install -g ts-node typescript && npm install && npm run build
EXPOSE 3005
ENTRYPOINT npm run dev
I can see sharp it's getting built during npm install
> [email protected] install /usr/app/server/node_modules/sharp
> (node install/libvips && node install/dll-copy && prebuild-install) || (node-gyp rebuild && node install/dll-copy)
info sharp Detected globally-installed libvips v8.6.3
info sharp Building from source via node-gyp
make: Entering directory '/usr/app/server/node_modules/sharp/build'
TOUCH Release/obj.target/libvips-cpp.stamp
CXX(target) Release/obj.target/sharp/src/common.o
CXX(target) Release/obj.target/sharp/src/metadata.o
CXX(target) Release/obj.target/sharp/src/stats.o
CXX(target) Release/obj.target/sharp/src/operations.o
CXX(target) Release/obj.target/sharp/src/pipeline.o
CXX(target) Release/obj.target/sharp/src/sharp.o
CXX(target) Release/obj.target/sharp/src/utilities.o
SOLINK_MODULE(target) Release/obj.target/sharp.node
COPY Release/sharp.node
make: Leaving directory '/usr/app/server/node_modules/sharp/build'
But the moment I do (Using typescript)
import * as sharp from 'sharp'
I get the following error:
Error: Error loading shared library /usr/app/server/node_modules/sharp/build/Release/sharp.node: Exec format error
It's there something I'm missing? Thank you!
Hello, my best guess would be that the COPY directives are populating node_modules directories inside the container with (OS X?) contents from outside the container.
Hi, thanks for answering so fast.
I've tried adding RUN ls -l /usr/app/server before the npm install and there's no node_modules (It's on the .dockerignore file).
I'm totally at a loss here.
Are you able to try the latest v0.21.0? Pre-built binaries are now provided for Alpine and you will no longer need to install vips-dev.
@lovell - Would that mean that I can remove the two RUN ... scripts from my Dockerfile below with the new 0.21.0 ?
FROM mhart/alpine-node:10.10 as base
WORKDIR /usr/src
# Add necessary packages for Sharp to work
RUN apk add --update-cache --repository http://dl-3.alpinelinux.org/alpine/edge/testing \
vips-dev fftw-dev gcc g++ make libc6-compat
COPY package.json yarn.lock ./
RUN yarn install
COPY . .
RUN yarn build
RUN yarn install --production
FROM mhart/alpine-node:10.10
WORKDIR /usr/src
# Add the `vips` package only to this stage of the build to make it leaner
RUN apk add --update-cache --repository http://dl-3.alpinelinux.org/alpine/edge/testing \
vips
COPY --from=base /usr/src/dist ./dist
COPY --from=base /usr/src/node_modules ./node_modules
COPY --from=base /usr/src/package.json package.json
CMD node dist/src/index.js
EXPOSE 4000
@jhalborg If your comment refers to the RUN apk add ... directives then yes.
@lovell - Just tried it, but alas - it didn't work. With the same Dockerfile as above, where the RUN apk ... scripts are removed, I get this error:
> [4/4] Building fresh packages...
> info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
> error /usr/src/node_modules/sharp: Command failed.
> Exit code: 1
> Command: (node install/libvips && node install/dll-copy && prebuild-install) || (node-gyp rebuild && node install/dll-copy)
> Arguments:
> Directory: /usr/src/node_modules/sharp
> Output:
> info sharp Downloading https://github.com/lovell/sharp-libvips/releases/download/v8.7.0/libvips-8.7.0-linuxmusl-x64.tar.gz
> prebuild-install WARN install Error loading shared library libbz2.so.1: No such file or directory (needed by /usr/src/node_modules/sharp/build/Release/../../vendor/lib/libgsf-1.so.114)
> gyp info it worked if it ends with ok
> gyp info using [email protected]
> gyp info using [email protected] | linux | x64
> gyp ERR! configure error
> gyp ERR! stack Error: Can't find Python executable "python", you can set the PYTHON env variable.
> gyp ERR! stack at PythonFinder.failNoPython (/usr/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:484:19)
> gyp ERR! stack at PythonFinder.<anonymous> (/usr/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:406:16)
> gyp ERR! stack at F (/usr/lib/node_modules/npm/node_modules/which/which.js:68:16)
> gyp ERR! stack at E (/usr/lib/node_modules/npm/node_modules/which/which.js:80:29)
> gyp ERR! stack at /usr/lib/node_modules/npm/node_modules/which/which.js:89:16
> gyp ERR! stack at /usr/lib/node_modules/npm/node_modules/isexe/index.js:42:5
> gyp ERR! stack at /usr/lib/node_modules/npm/node_modules/isexe/mode.js:8:5
> gyp ERR! stack at FSReqWrap.oncomplete (fs.js:154:21)
> gyp ERR! System Linux 4.13.0-1011-gcp
> gyp ERR! command "/usr/bin/node" "/usr/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
> gyp ERR! cwd /usr/src/node_modules/sharp
> gyp ERR! node -v v10.10.0
> gyp ERR! node-gyp -v v3.8.0
> gyp ERR! not ok
> Error! Build failed
error Command failed with exit code 1.
It seems the Alpine images does not come with Python?
It looks like the mhart/alpine-node images lack bz2 support.
You can workaround this for now with apk add libbz2.
I've created https://github.com/lovell/sharp-libvips/issues/10 to fix this.
Should be working with the mhart/alpine-node images now.
$ docker run -it --rm mhart/alpine-node:10 /bin/sh
/ # npm install sharp
> [email protected] install /node_modules/sharp
> (node install/libvips && node install/dll-copy && prebuild-install) || (node-gyp rebuild && node install/dll-copy)
info sharp Downloading https://github.com/lovell/sharp-libvips/releases/download/v8.7.0/libvips-8.7.0-linuxmusl-x64.tar.gz
+ [email protected]
added 77 packages from 100 contributors and audited 179 packages in 10.581s
found 0 vulnerabilities
For me the problem was that I didn't specify the .dockerignore on the root folder precise enough. I needed to add **/node_modules and not node_module