Docker失败-BCRYPT_LIB.NODE:EXEC格式错误
我试图创建我的后端API的Docker图像。 但是遇到错误。我已经搜索过它,每个遇到相同问题的人都必须在.dockerignore文件上添加node_module。
我已经做到了,但是仍然有同样的错误。
我在这里添加我的文件信息。
dockerfile
FROM node:alpine
WORKDIR /usr/src/app
COPY package*.json .
#COPY yarn.lock .
RUN apk add --no-cache yarn --repository="http://dl-cdn.alpinelinux.org/alpine/edge/community"
#RUN yarn install --frozen-lockfile
RUN yarn install
RUN yarn
COPY . .
CMD ["yarn", "dev"];
.dockerignore
/node_modules
.env
docker-compose.yml
docker-compose.yml
version: "3.9"
services:
mongo_db:
container_name: mongodb_container
image: mongo:latest
restart: always
ports:
- "27017:27017"
volumes:
- mongo_db:/data/db
#EET service
eetapi:
container_name: eetapi_container
build: .
volumes:
- .:/usr/src/app
ports:
- "3000:3000"
environment:
SITE_URL: http://localhost
PORT: 3000
MONGO_URL: mongodb://mongodb_container:27017/easyetapi
JWT_SECRET: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
SENTRY_DSN: https://[email protected]/xxxxxxx
MAILGUN_DOMAIN: mg.myeetdomain.tld
MAILGUN_API_KEY: xxxxxxxxxxxxxxx-xxxxxxxxxxx-xxxxxxxx
NODE_ENV: production
depends_on:
- mongo_db
volumes:
mongo_db: {}
error
请帮助我。
谢谢
I have tried to create a docker image of my backend API.
but getting errors. I have googled about it, and everyone who has the same issue had to add node_module on the .dockerignore file.
I already did it, but, still have the same error.
I am adding my file info here.
Dockerfile
FROM node:alpine
WORKDIR /usr/src/app
COPY package*.json .
#COPY yarn.lock .
RUN apk add --no-cache yarn --repository="http://dl-cdn.alpinelinux.org/alpine/edge/community"
#RUN yarn install --frozen-lockfile
RUN yarn install
RUN yarn
COPY . .
CMD ["yarn", "dev"];
.dockerignore
/node_modules
.env
docker-compose.yml
docker-compose.yml
version: "3.9"
services:
mongo_db:
container_name: mongodb_container
image: mongo:latest
restart: always
ports:
- "27017:27017"
volumes:
- mongo_db:/data/db
#EET service
eetapi:
container_name: eetapi_container
build: .
volumes:
- .:/usr/src/app
ports:
- "3000:3000"
environment:
SITE_URL: http://localhost
PORT: 3000
MONGO_URL: mongodb://mongodb_container:27017/easyetapi
JWT_SECRET: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
SENTRY_DSN: https://[email protected]/xxxxxxx
MAILGUN_DOMAIN: mg.myeetdomain.tld
MAILGUN_API_KEY: xxxxxxxxxxxxxxx-xxxxxxxxxxx-xxxxxxxx
NODE_ENV: production
depends_on:
- mongo_db
volumes:
mongo_db: {}
The Error
Please help me out.
Thank You
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
卷:
块覆盖图像中的所有内容,并使用主机上的当前目录。其中包括node_modules
在Dockerfile中安装的树。如果您有MacOS或Windows主机,但是Linux容器,则更换node_modules
树将导致您遇到的错误。您应该删除
卷:
块,以便运行图像中内置的代码和库树。由于绑定安装的覆盖实际上是Dockerfile所做的一切,因此它否定了从构建Docker映像中获得的任何好处。有效地,您只是在使用绑定安装的主机内容运行一个未修改的
节点
图像,如果您在主机上的节点而不涉及Docker,则可以通过更简单的设置获得相同的效果。 (您仍然可以从容器中运行数据库中受益。)The
volumes:
block overwrites everything in the image with the current directory on the host. That includes thenode_modules
tree installed in the Dockerfile. If you have a MacOS or Windows host but a Linux container, replacing thenode_modules
tree will cause the error you get.You should delete the
volumes:
block so that you run the code and library tree that are built into the image.Since the bind-mount overwrites literally everything the Dockerfile does, it negates any benefit you get from building the Docker image. Effectively you're just running an unmodified
node
image with bind-mounted host content, and you'll get the same effect with a much simpler setup if you Node on the host without involving Docker. (You could still benefit from running the database in a container.)我最终找到了一个差异解决方案,问题是由于我的计算机(Windows)创建的节点模块而引起的,我正在Alpine中创建Docker Images。因此,我使用独立于平台的bcryptjs。
bcrypt
:它是与C ++ BCrypt库的本机绑定。它需要编译并包含与基础系统C库的绑定。结果,由于其本机实施,它可能具有更好的性能。这意味着,如果我在Windows中编译了库,那么它将在Linux中工作。但是性能会更好。bcryptjs
:这是bcrypt算法的纯JavaScript实现。它没有本机绑定,完全依赖于JavaScript。虽然与BCRypt相比,尤其是在CPU密集型操作中,它可能较慢,但在不同系统上安装和使用而无需本机汇编更容易。因此,我安装了bcryptjs,并取得了成功。同样,这只是一个选择,如果您的项目中有很大的复杂密码散列,那么您应该获得bcrypt以获得更好的性能。
I ended up a difference solution , the problem occurred due to node modules created by my machine which is windows and I am creating docker images in alpine. So I use bcryptjs that is platform independent.
bcrypt
: It is a native binding to the C++ bcrypt library. It requires compilation and contains bindings to the underlying system's C library. As a result, it might have better performance due to its native implementation. That means if I compiled the library in windows then it will not work in linux. But performance would be better.bcryptjs
: It is a pure JavaScript implementation of the bcrypt algorithm. It doesn't have native bindings and relies entirely on JavaScript. While it might be slower compared to bcrypt, especially in CPU-intensive operations, it's easier to install and use across different systems without requiring native compilation.So I installed bcryptjs and I got the build success. Again it is just a choice, if you have big complex password hashing in your project then you should got for bcrypt for better performance.
删除现有的
node_modules
文件夹和重建Docker映像。Delete the existing
node_modules
folder and rebuild Docker images.我解决了这个问题。只需在卷中添加docker -compose.yml,例如 - '/app/node_modules'。确保目录在容器中可用。
I solved this problem. Just add in docker-compose.yml in volumes a code like - '/app/node_modules'.For ensure that the directory is available within the container.
您需要做的就是在
src/
文件夹中复制所有代码。然后,您应该仅在主机和Docker映像之间安装该文件夹(IEsrc/
)。这样,您只需要在添加新软件包时重建docker-compose文件。All you need to do is copy all your code inside a
src/
folder. Then you should mount only that folder (i.e.src/
) between the host and the docker image. That way, you will only need to rebuild your docker-compose file when you add a new package to package.json.经过几个小时的调试,为什么这突然开始发生。我发现,在我的Docker Compose.yml文件的“服务”部分下,我的服务命名为:
apis-service
具有不正确的卷绑定。我有此卷部分,该部分告诉Docker不要覆盖我的主机
node_modules
目录。产生错误的错误设置,
我发现
/api-service/node_modules
是不正确的,因为我的dockerfile
workdir
is/apis - 服务
不是/api-service
。因此,现在我必须更改它,并指向容器内部的正确目录,一切都开始工作正常。
产生预期结果的正确设置。
After several hours of debugging the main issue why this suddenly started happening. I discovered that under the service section of my docker compose.yml file my service which was named:
apis-service
Was having an incorrect volume binding.I had this volume section that tells docker not to override my host
node_modules
directory.The incorrect setup that produced the error
I discovered that the
/api-service/node_modules
is incorrect because myDockerfile
workdir
is/apis-service
Not/api-service
.So now I have to change it and point to the correct directory inside the container and everything started working fine again.
The correct setup that produced the desired outcome.