简体   繁体   English

如何使用 alpine 镜像在 Docker 容器中运行单元测试并保存结果?

[英]How can I run units tests in Docker container using alpine image and save the results?

I am currently located within my folder of the vue.js project.我目前位于 vue.js 项目的文件夹中。 The source files are located in ./src/ folder and test files are located within ./tests/源文件位于 ./src/ 文件夹中,测试文件位于 ./tests/ 中

Usually to run unit tests locally I run the following commands:通常在本地运行单元测试我运行以下命令:

npm install
npm ci
npm run test:ci

and it produces, among the others ./report/coverage.lcov file它产生,其中包括 ./report/coverage.lcov 文件

However, I want to use node:12-alpine docker image to run unit tests inside of it.但是,我想使用 node:12-alpine docker image 在其中运行单元测试。 DO NOT offer to use Dockerfile.不要提供使用 Dockerfile。 I want to run it using docker run --rm node:12-alpine .... and copy the content of ./report folder into my local folder when docker run command is complete.我想使用docker run --rm node:12-alpine ....运行它,并在 docker run 命令完成时将 ./report 文件夹的内容复制到我的本地文件夹中。 However, I could not figure out how I can do that?但是,我无法弄清楚我该怎么做? What docker run arguments I should use?我应该使用哪些 docker run 参数?

为什么机器人在运行时将您的报告目标挂载为一个卷?

I am able to run it locally by creating script.sh file我可以通过创建 script.sh 文件在本地运行它

cd /tmp/run
npm install
npm ci
npm run test:ci

in project directory and running:在项目目录中并运行:

docker run --rm -v ${PWD}/:/tmp/run -u 0 node:12-alpine sh /tmp/run/script.sh

This is great as I do not need to install another node version locally and container is deleted after run... However, I could not replace the last line via: cd /tmp/run && npm install ... as it raised an error.这很棒,因为我不需要在本地安装另一个节点版本,并且在运行后删除容器...但是,我无法通过以下方式替换最后一行: cd /tmp/run && npm install ... 因为它引发了错误. Did not really wanted to introduce an extra script.并不是真的想引入额外的脚本。 Using --entrypoint outputs another error.使用 --entrypoint 会输出另一个错误。

是的,我可以做到,而且很简单

docker run --rm -v ${PWD}/:/tmp/run -u 0 --workdir=/tmp/run node:14-alpine npm install && npm ci && npm run test:ci

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM