Gitlab Continuous integration(ci) script for docker image build stage

Here is a simple GitLab CI script for building a Docker image. This assumes you have a Dockerfile in the root of your repository:

package main
stages:
  - build

variables:
  IMAGE_NAME: "your-docker-image-name"
  TAG: "latest"

before_script:
  - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY

build:
  stage: build
  script:
    - docker build -t $CI_REGISTRY_IMAGE/$IMAGE_NAME:$TAG .
    - docker push $CI_REGISTRY_IMAGE/$IMAGE_NAME:$TAG

Make sure to replace "your-docker-image-name" with the actual name you want for your Docker image.

This script defines a single job (build) in the build stage. It uses GitLab CI/CD variables to authenticate with the Docker registry ($CI_REGISTRY, $CI_REGISTRY_USER, and $CI_REGISTRY_PASSWORD). The before_script section logs in to the Docker registry.

The script section then builds the Docker image using the Dockerfile in the root directory and pushes it to the GitLab Container Registry.

Remember to configure your GitLab CI/CD settings to provide the necessary environment variables for authentication (e.g., $CI_REGISTRY_USER, $CI_REGISTRY_PASSWORD). Also, ensure that your GitLab Runner has the ability to communicate with your Docker daemon.

Leave a Reply

Your email address will not be published. Required fields are marked *