我正在尝试测试用nodejs v10编写的代码。
每个测试应在开始前启动一个新的数据库容器,并在测试完成后将其拆除,以防止其他测试引起任何副作用。
我需要在 mongodb、pg、elasticsearch 等少数数据库中执行此操作
下面是我的测试示例
describe('test Mongo', () => {
let container = null;
beforeEach(async() => {
container = await start_a_container_using_child_process_exec(); // exec(`docker run -d --rm -p 27017:27017 mongo:latest`)
});
it('test1', () => {
// connect to database and do something
});
it('test2', () => {
// connect to database and do something
});
afterEach(async () => {
await container.remove();
});
})
它在我的运行 docker 桌面的 Windows 环境中运行良好。
但是此代码无法在 Gitlab-CI 中运行。下面是我的 gitlab-ci.yaml
stages:
- test
test:
image: node:10
stage: test
script:
- 'npm install'
- 'npm run test'
当它尝试启动容器时,我不断收到“docker:找不到”错误。
有没有安装 docker 时我可以获得的 Nodejs v10 镜像?
或者有其他方法可以解决这个问题吗?
最佳答案
您可以使用为构建 docker 镜像而创建的 docker 镜像,并在该镜像上安装 nodejs 应该很简单。
您可以在 gitlab repo 中生成 gitlab-ci.yml 模板,在 GUI 中创建一个新文件并选择 gitlab-vi.yml 模板,然后选择 docker 模板
删除 docker build 中不必要的部分并安装 nodejs,您的任务应该可以解决问题。
编辑:
# This file is a template, and might need editing before it works on your project.
build-master:
# Official docker image.
image: docker:latest
stage: build
services:
- docker:dind
script:
- apk add nodejs # install nodejs
- node app.js # run nodejs file
- docker ps # run docker
Console log of gitlab build running docker & nodejs
寻找原木末端的绿色。
附注
我是用手机写的,如果答案格式不正确,我很抱歉。
关于node.js - 如何在 Node :10 container in GitLabCI中执行docker命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58023610/