CurlyError
CurlyError

Reputation: 460

Build multiple Docker images with gitlab-ci

I'm trying to configure a gitlab-ci.yml file to automatically build two Docker images. I've seen it being done with docker-compose, but in my case I don't want to use docker-compose.

The docker-compose.yml configuration that I've seen is something like this:

version: '3'

services:
  vcxagency-base:
    build:
      context: https://github.com/AbsaOSS/vcxagencynode.git#master
      dockerfile: ./vcxagency-base/vcxagency-base.Dockerfile
    image: vcxagency-base:1.15.0
  
  vcxagency-node:
    build:
      context: https://github.com/AbsaOSS/vcxagencynode.git#master
      dockerfile: ./vcxagency-node/Dockerfile
    image: vcxagency-node:0.3.1
    ports:
      - 8080:8080
    depends_on:
      - postgres
    environment:
      - LOG_LEVEL=debug
      - LOG_ENABLE_INDYSDK=false
      - LOG_JSON_TO_CONSOLE=false
      - SERVER_PORT=8080
      - SERVER_MAX_REQUEST_SIZE_KB=300
      - SERVER_ENABLE_TLS=false

I haven't really worked with Docker at all. I tried to explore the documentation, but couldn't find something specific.

The goal would be to have these running on a Kubernetes cluster, but for now I just want to build the images in GitLab.

Upvotes: 2

Views: 5326

Answers (1)

SPMSE
SPMSE

Reputation: 628

You could make use of Kaniko as a docker build tool and GitLabs feature called matrix builds.

How to use kaniko for building docker images

Snippet below is taken from the official GitLab docs

build:
  stage: build
  image:
    name: gcr.io/kaniko-project/executor:debug
    entrypoint: [""]
  script:
    - mkdir -p /kaniko/.docker
    - echo "{\"auths\":{\"${CI_REGISTRY}\":{\"auth\":\"$(printf "%s:%s" "${CI_REGISTRY_USER}" "${CI_REGISTRY_PASSWORD}" | base64 | tr -d '\n')\"}}}" > /kaniko/.docker/config.json
    - >-
      /kaniko/executor
      --context "${CI_PROJECT_DIR}"
      --dockerfile "${CI_PROJECT_DIR}/Dockerfile"
      --destination "${CI_REGISTRY_IMAGE}:${CI_COMMIT_TAG}"
  rules:
    - if: $CI_COMMIT_TAG

What are matrix builds?

GitLab introduced a feature for parallel job executions which is called matrix jobs, basically the job is executed multiple times with different sets of variables (variable matrix).

Taking the above snippet as a base you would result in something like below (suppose your dockerfiles are stored under <PROJECT_DIR>/dockerfiles/ and are named A.dockerfile and B.dockerfile, respectively):

.kaniko-build:
  stage: build
  image:
    name: gcr.io/kaniko-project/executor:debug
    entrypoint: [""]
  script:
    - mkdir -p /kaniko/.docker
    - echo "{\"auths\":{\"${CI_REGISTRY}\":{\"auth\":\"$(printf "%s:%s" "${CI_REGISTRY_USER}" "${CI_REGISTRY_PASSWORD}" | base64 | tr -d '\n')\"}}}" > /kaniko/.docker/config.json
    - >-
      /kaniko/executor
      --context "${CI_PROJECT_DIR}"
      --dockerfile "${CI_PROJECT_DIR}/${DOCKERFILE}"
      --destination "${CI_REGISTRY_IMAGE}:${CI_COMMIT_TAG}"
  rules:
    - if: $CI_COMMIT_TAG

docker-build:
  extends: .build
  parallel:
    matrix:
      - DOCKERFILE: ${CI_PROJECT_DIR}/dockerfiles/A.dockerfile
      - DOCKERFILE: ${CI_PROJECT_DIR}/dockerfiles/B.dockerfile

Upvotes: 10

Related Questions