我创建了如下简单的Dockerfile:
FROM node:16.7.0
WORKDIR /app
COPY . .
RUN npm install
# ENTRYPOINT [ "npm" ]
CMD ["sh", "-c", "tail -f /dev/null"]
我已经添加了"tail -f /dev/null"
的cmd行,以检查如果我在容器内发出npm test
究竟是什么问题。
当我在容器内运行npm test
时——>它把我扔到错误下面
# npm test
> docker-jest@1.0.0 test
> jest --verbose
sh: 1: jest: not found
mypackage.json
{
"name": "docker-jest",
"version": "1.0.0",
"description": "Package for Jest",
"scripts": {
"test": "jest --verbose"
},
"Dependencies": {
"@babel/node": "*",
"@babel/core": "*",
"@babel/preset-env": "*",
"babel-jest": "*",
"jest": "*"
},
"license": "ISC"
}
sum.js
function sum(a, b) {
return a + b;
}
module.exports = sum;
sum.test.js
const sum = require('./sum');
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});
即使我禁用CMD并启用ENTRYPOINT,在构建之后,如果我发出:
docker run -it <imagename> test
它给我带来了同样的错误,我看到npm install
正在安装,但找不到jest @/usr/local/lib/node_modules/
,因为我看到节点模块部署在容器内的位置/usr/local/lib/node_modules/
,如果我发出jest
,它说jest not found
。如果我在没有容器的情况下运行相同的程序,它可以正常工作。我的意思是在运行npm install and then npm run test
之后的命令行。
谁能帮助我为什么我得到这个错误,以及如何修复它?
----------- 更新 -------------Found the fix
,这是因为我损坏的包锁文件。当我在没有docker的情况下在本地进行测试时,我不知何故损坏了锁文件,后来当我构建并尝试使用docker运行时,损坏的锁文件导致了很多问题。所以我删除了它,并再次运行docker…运行正常
我也有同样的问题,我的修复是运行npm install -g jest(或yarn global add jest)。
将此添加到您的包中。Json执行以下操作:
"scripts: {
"test": "npm install -g jest && jest --verbose"
},