Node Alpine Container Silent Crash
spetrac opened this issue ยท 6 comments
I have a server setup in a 2-stage node alpine docker container. Everything worked fine for bcrypt version 5.0.1, but since I used "^5.0.1" for my dependency, the latest version 5.1.1 was used for my last build. Although I have a try-catch block around the await bcrypt.compare(...)
statement, the whole application quit silently without any warnings. During debugging I traced the error down to the compare call of the bindings, but I could not debug any further with NodeJS. I figure, since there were not many changes from bcrypt version 5.0.1 to 5.1.1, the error might be introduced somewhere in the updated from node-pre-gyp version 1.0.0 to 1.0.11.
I cannot provide you with my sourcecode, but trust me that this is not a javascript issue. It is just the bcrypt.compare call that has worked previously anyway. I think more interesting is the container setup to recreate the issue, which I will add exemplary down below:
# Dockerfile
# Builder:
FROM node:lts AS builder
ENV NODE_ENV="production"
RUN mkdir -p /opt/my-app
WORKDIR /opt/my-app
COPY . .
RUN npm install
# Runner:
FROM node:lts-alpine AS runner
ENV NODE_ENV="production"
RUN mkdir -p /opt/my-app
WORKDIR /opt/my-app
COPY --from=builder /opt/my-app /opt/my-app
EXPOSE 8080
ENTRYPOINT npm start
# docker build --tag my-app .
# docker run -p 8080:8080 my-app
For debugging inside the container I just changed the entrypoint and added the debugging port to the run:
# ...
ENTRYPOINT node --inspect-brk=0.0.0.0 ./src/my-app.js
# docker build --tag inspect-my-app .
# docker run -p 8080:8080 -p 9229:9229 -it --rm inspect-my-app
We faced the same issue on node:18-alpine3.17. Previously version 5.1.0 is working fine.
I'm having the same issue, with both bcrypt@5.1.1 and bcrypt@5.1.0.
The failure is intermittent and seems related to a network timeout?
Extract from docker build logs piping through the npm ci
errors
#18 59.67 npm ERR! code 1
#18 59.67 npm ERR! path /app/node_modules/bcrypt
#18 59.67 npm ERR! command failed
#18 59.67 npm ERR! command sh -c node-pre-gyp install --fallback-to-build
#18 59.67 npm ERR! Failed to execute '/usr/local/bin/node /usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js configure --fallback-to-build --module=/app/node_modules/bcrypt/lib/binding/napi-v3/bcrypt_lib.node --module_name=bcrypt_lib --module_path=/app/node_modules/bcrypt/lib/binding/napi-v3 --napi_version=9 --node_abi_napi=napi --napi_build_version=3 --node_napi_label=napi-v3' (1)
#18 59.67 npm ERR! node-pre-gyp info it worked if it ends with ok
#18 59.67 npm ERR! node-pre-gyp info using node-pre-gyp@1.0.11
#18 59.67 npm ERR! node-pre-gyp info using node@18.18.2 | linux | x64
#18 59.67 npm ERR! node-pre-gyp info check checked for "/app/node_modules/bcrypt/lib/binding/napi-v3/bcrypt_lib.node" (not found)
#18 59.67 npm ERR! node-pre-gyp http GET https://github.com/kelektiv/node.bcrypt.js/releases/download/v5.1.0/bcrypt_lib-v5.1.0-napi-v3-linux-x64-musl.tar.gz
#18 59.67 npm ERR! node-pre-gyp ERR! install request to https://github.com/kelektiv/node.bcrypt.js/releases/download/v5.1.0/bcrypt_lib-v5.1.0-napi-v3-linux-x64-musl.tar.gz failed, reason: read ECONNRESET
#18 59.67 npm ERR! node-pre-gyp WARN Pre-built binaries not installable for bcrypt@5.1.0 and node@18.18.2 (node-v108 ABI, musl) (falling back to source compile with node-gyp)
#18 59.67 npm ERR! node-pre-gyp WARN Hit error request to https://github.com/kelektiv/node.bcrypt.js/releases/download/v5.1.0/bcrypt_lib-v5.1.0-napi-v3-linux-x64-musl.tar.gz failed, reason: read ECONNRESET
#18 59.67 npm ERR! gyp info it worked if it ends with ok
#18 59.67 npm ERR! gyp info using node-gyp@9.4.0
#18 59.67 npm ERR! gyp info using node@18.18.2 | linux | x64
#18 59.67 npm ERR! gyp info ok
#18 59.67 npm ERR! gyp info it worked if it ends with ok
#18 59.67 npm ERR! gyp info using node-gyp@9.4.0
#18 59.67 npm ERR! gyp info using node@18.18.2 | linux | x64
#18 59.67 npm ERR! gyp ERR! find Python
#18 59.67 npm ERR! gyp ERR! find Python Python is not set from command line or npm configuration
#18 59.67 npm ERR! gyp ERR! find Python Python is not set from environment variable PYTHON
#18 59.67 npm ERR! gyp ERR! find Python checking if "python3" can be used
#18 59.67 npm ERR! gyp ERR! find Python - "python3" is not in PATH or produced an error
#18 59.67 npm ERR! gyp ERR! find Python checking if "python" can be used
#18 59.67 npm ERR! gyp ERR! find Python - "python" is not in PATH or produced an error
#18 59.67 npm ERR! gyp ERR! find Python
#18 59.67 npm ERR! gyp ERR! find Python **********************************************************
#18 59.67 npm ERR! gyp ERR! find Python You need to install the latest version of Python.
#18 59.67 npm ERR! gyp ERR! find Python Node-gyp should be able to find and use Python. If not,
#18 59.67 npm ERR! gyp ERR! find Python you can try one of the following options:
#18 59.67 npm ERR! gyp ERR! find Python - Use the switch --python="/path/to/pythonexecutable"
#18 59.67 npm ERR! gyp ERR! find Python (accepted by both node-gyp and npm)
#18 59.67 npm ERR! gyp ERR! find Python - Set the environment variable PYTHON
#18 59.67 npm ERR! gyp ERR! find Python - Set the npm configuration variable python:
#18 59.67 npm ERR! gyp ERR! find Python npm config set python "/path/to/pythonexecutable"
#18 59.67 npm ERR! gyp ERR! find Python For more information consult the documentation at:
#18 59.67 npm ERR! gyp ERR! find Python https://github.com/nodejs/node-gyp#installation
#18 59.67 npm ERR! gyp ERR! find Python **********************************************************
#18 59.67 npm ERR! gyp ERR! find Python
#18 59.67 npm ERR! gyp ERR! configure error
#18 59.67 npm ERR! gyp ERR! stack Error: Could not find any Python installation to use
#18 59.67 npm ERR! gyp ERR! stack at PythonFinder.fail (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/find-python.js:330:47)
#18 59.67 npm ERR! gyp ERR! stack at PythonFinder.runChecks (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/find-python.js:159:21)
#18 59.67 npm ERR! gyp ERR! stack at PythonFinder.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/find-python.js:[202](https://gitlab.com/laiout/laiout-app/-/jobs/5346334631#L202):16)
#18 59.67 npm ERR! gyp ERR! stack at PythonFinder.execFileCallback (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/find-python.js:294:16)
#18 59.67 npm ERR! gyp ERR! stack at exithandler (node:child_process:430:5)
#18 59.67 npm ERR! gyp ERR! stack at ChildProcess.errorhandler (node:child_process:442:5)
#18 59.67 npm ERR! gyp ERR! stack at ChildProcess.emit (node:events:517:28)
#18 59.67 npm ERR! gyp ERR! stack at ChildProcess._handle.onexit (node:internal/child_process:290:12)
#18 59.67 npm ERR! gyp ERR! stack at onErrorNT (node:internal/child_process:477:16)
#18 59.67 npm ERR! gyp ERR! stack at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
#18 59.67 npm ERR! gyp ERR! System Linux 4.14.322-244.539.amzn2.x86_64
#18 59.67 npm ERR! gyp ERR! command "/usr/local/bin/node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "configure" "--fallback-to-build" "--module=/app/node_modules/bcrypt/lib/binding/napi-v3/bcrypt_lib.node" "--module_name=bcrypt_lib" "--module_path=/app/node_modules/bcrypt/lib/binding/napi-v3" "--napi_version=9" "--node_abi_napi=napi" "--napi_build_version=3" "--node_napi_label=napi-v3"
#18 59.67 npm ERR! gyp ERR! cwd /app/node_modules/bcrypt
#18 59.67 npm ERR! gyp ERR! node -v v18.18.2
#18 59.67 npm ERR! gyp ERR! node-gyp -v v9.4.0
#18 59.67 npm ERR! gyp ERR! not ok
#18 59.67 npm ERR! node-pre-gyp ERR! build error
#18 59.67 npm ERR! node-pre-gyp ERR! stack Error: Failed to execute '/usr/local/bin/node /usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js configure --fallback-to-build --module=/app/node_modules/bcrypt/lib/binding/napi-v3/bcrypt_lib.node --module_name=bcrypt_lib --module_path=/app/node_modules/bcrypt/lib/binding/napi-v3 --napi_version=9 --node_abi_napi=napi --napi_build_version=3 --node_napi_label=napi-v3' (1)
#18 59.67 npm ERR! node-pre-gyp ERR! stack at ChildProcess.<anonymous> (/app/node_modules/@mapbox/node-pre-gyp/lib/util/compile.js:89:23)
#18 59.67 npm ERR! node-pre-gyp ERR! stack at ChildProcess.emit (node:events:517:28)
#18 59.67 npm ERR! node-pre-gyp ERR! stack at maybeClose (node:internal/child_process:1098:16)
#18 59.67 npm ERR! node-pre-gyp ERR! stack at ChildProcess._handle.onexit (node:internal/child_process:303:5)
#18 59.67 npm ERR! node-pre-gyp ERR! System Linux 4.14.322-244.539.amzn2.x86_64
#18 59.67 npm ERR! node-pre-gyp ERR! command "/usr/local/bin/node" "/app/node_modules/.bin/node-pre-gyp" "install" "--fallback-to-build"
#18 59.67 npm ERR! node-pre-gyp ERR! cwd /app/node_modules/bcrypt
#18 59.67 npm ERR! node-pre-gyp ERR! node -v v18.18.2
#18 59.67 npm ERR! node-pre-gyp ERR! node-pre-gyp -v v1.0.11
#18 59.67 npm ERR! node-pre-gyp ERR! not ok
#18 59.68
@spetrac-nicos-ag You are building on glibc and running on alpine which uses musl. While musl has limited glibc compatibility, it is not very helpful if it cannot load some symbols and crashes outright.
Change your builder to FROM node:lts-alpine AS builder
Same issue running with docker-compose using an aws-lambda image.
image: public.ecr.aws/lambda/nodejs:20-x86_64
Pinning the version to 5.0.1 fixes the issue.
Is it possible to fix this without changing the image i'm using?