Simon
Simon

Reputation: 1536

How to execute docker command in node:10 container in GitLabCI

I am trying to test my code writen in nodejs v10.

Each test should bring up a new database container before start and tear it down after test finish to prevent any side effect cause by other test.

I need to do this in few databases like mongodb, pg, elasticsearch and so on

Below is the sample of my test

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();
  });
})

It work well in my Windows environment with docker desktop running.

But this code is not runable in Gitlab-CI. Below is my gitlab-ci.yaml

stages:
  - test
test:
  image: node:10
  stage: test
  script:
    - 'npm install'
    - 'npm run test'

I keep on getting 'docker: not found' error when it attempt to start a container.

Is there any nodejs v10 image I can get which it come along with docker installed?

Or there is other way to solve this problem?

Upvotes: 1

Views: 551

Answers (1)

Shmuel
Shmuel

Reputation: 1668

You can use a docker image created for building docker images and install nodejs on that image should be simple.

You can generate a template of gitlab-ci.yml in gitlab repo create a new file in the GUI and select gitlab-vi.yml template then select docker template
Remove the unnecessary part of docker build and install nodejs and your tasks should do the trick.

Edit:

# 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
Look for the green in the end of the log.

P.S.
I'm writing from my phone so sorry if the answer is not formated nicely.

Upvotes: 1

Related Questions