I have the following Dockerfile
as part of a larger docker compose
project.
FROM node:slim AS node
# Update package list and install dependencies
RUN apt-get update && apt-get install -y
git python3 make gcc g++ libc6-dev libpng-dev python3-pip
libpixman-1-dev libcairo2-dev libpango1.0-dev libjpeg-dev libgif-dev librsvg2-dev
libjpeg62-turbo-dev pkg-config libtool automake build-essential --no-install-recommends &&
apt-get clean
# Fix for Python3.12 and node-gyp
RUN python3 -m pip install setuptools --break-system-packages
# Update npm and install global dependencies
RUN npm install -g npm@latest
RUN npm install -g node-gyp
# Set the working directory
WORKDIR /app
# Copy the package.json file and install dependencies
COPY package.json /app/package.json
RUN npm install
# Copy the application source code
COPY . /app
# Expose the application port
EXPOSE 3000
# Set the environment variables
ENV NODE_ENV=production
ENV LD_LIBRARY_PATH=/usr/lib:/usr/local/lib:$LD_LIBRARY_PATH
ENV PKG_CONFIG_PATH=/usr/lib/pkgconfig:/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH
# Start the application
CMD ["sh", "-c", "echo 'LD_LIBRARY_PATH='$LD_LIBRARY_PATH && npm run start"]
# CMD ["tail", "-f", "/dev/null"]
However, this results in the following error:
sudo docker logs 3c41c08f610d
LD_LIBRARY_PATH=/usr/lib:/usr/local/lib:
> [email protected] start
> node ./app.js
node:internal/modules/cjs/loader:1724
return process.dlopen(module, path.toNamespacedPath(filename));
^
Error: libjpeg.so.8: cannot open shared object file: No such file or directory
at Object..node (node:internal/modules/cjs/loader:1724:18)
at Module.load (node:internal/modules/cjs/loader:1303:32)
at Function._load (node:internal/modules/cjs/loader:1117:12)
at TracingChannel.traceSync (node:diagnostics_channel:322:14)
at wrapModuleLoad (node:internal/modules/cjs/loader:218:24)
at Module.require (node:internal/modules/cjs/loader:1325:12)
at require (node:internal/modules/helpers:136:16)
at Object.<anonymous> (/app/node_modules/canvas/lib/bindings.js:3:18)
at Module._compile (node:internal/modules/cjs/loader:1546:14)
at Object..js (node:internal/modules/cjs/loader:1698:10) {
code: 'ERR_DLOPEN_FAILED'
}
The interest thing though, is that if I commit the container with an interactive entrypoint (ie sh
), via
docker commit 3c41c08f610d landon/test
docker run -ti --entrypoint=sh landon/test
# pwd
/app
# npm start
> [email protected] start
> node ./app.js
App listening on port 3000!
it works with no issues. I’ve tried asking ChatGPT for some help, and most of it revolves around LD library so I added some debugging to make sure environment variables were being set properly and we can see LD_LIBRARY_PATH=/usr/lib:/usr/local/lib:
so looks like it is. If the library truly didn’t exist, then surely running npm start
through the interactive shell wouldn’t work?
Also found a similar issue here, and they recommended to add node_modules
to .dockerignore
which I tried and the issue still persists. The package-lock.json
and node_modules
are also included in my .gitignore
and I do a fresh pull each time for testing, so it shouldn’t be have a conflicting set of modules being copied over to the container during build.
Thanks for any help
2
Answers
I ended up just doing a full
docker system prune -a
anddocker volume prune -a
and the issue went away. Not entirely sure what was going on but that worked for me.It´s probably because the dockerfile user npm run start but when you enter on interactive mode you are using nodejs commmand.
echo ‘LD_LIBRARY_PATH=’$LD_LIBRARY_PATH’ on this point, this is the other difference, and the error looks that something are missing. Could you test the CMD command exactly on interactive mode?