【问题标题】:How to execute docker command in node:10 container in GitLabCI如何在 GitLab CI 的 node:10 容器中执行 docker 命令
【发布时间】:2019-09-20 07:21:29
【问题描述】:

我正在尝试测试我在 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: not found”错误。

是否有任何 nodejs v10 图像我可以随安装的 docker 一起获得?

或者有其他方法可以解决这个问题?

【问题讨论】:

  • 好像Gitlib-Cl中没有Docker,你的节点容器不需要安装docker。这是尝试运行 docker 的 Gitlib。

标签: node.js docker gitlab-ci


【解决方案1】:

您可以使用为构建 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
在日志末尾寻找绿色。

附:
如果答案格式不正确,我正在用手机写信,非常抱歉。

【讨论】:

    猜你喜欢
    • 2020-06-28
    • 1970-01-01
    • 1970-01-01
    • 2016-04-24
    • 2021-09-17
    • 1970-01-01
    • 2018-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多