Search code examples
dockergitlabgitlab-ci

Run "docker build" command in a Gitlab Pipeline


I have a Gitlab Pipeline where I do some tests and then build a Docker image. See the snippet of what it looks like:

image:
  name: node:18.13.0-alpine
  entrypoint: [""]

stages:
  - test
  - build

test:
  stage: test
  script:
    - yarn install
    - yarn test:coverage

publish:
  stage: build
  script:
    - docker build -t someimage:123 .

I have two questions with this:

  1. How do I pass the node_modules folder from the test to the build stage?
  2. The node:18.13.0-alpine image doesn't have Docker installed on it. How do I go about it?

Solution

    1. Using Gitlab Artifacts.
    2. There are many ways to achieve this. One of them is to use the official Docker image and install NodeJs over it. Also, you need Docker In Docker to build an image. The thing is, Docker is a client-server app, so you can't build just using a single Docker image. The client (docker:24.0.7) will be in your main job's image and the server (docker:24.0.7-dind) in the job's service.