anpanman
Published on

GitLab CI/CD — docker registry

Introduction

Docker registry is a platform for storing, managing, and distributing Docker images. It allows users to create private image repositories and share them with other users or teams. Docker Hub is the most commonly used public image repository, but there are also third-party options available such as Quay.io and GitLab Container Registry. Using a Docker registry can improve the efficiency of managing and deploying applications.

GitLab Container Registry is a private Docker registry that is integrated with GitLab. It allows you to store Docker images and use them in your CI/CD pipelines. It is a great way to store and share Docker images with your team. It is also a great way to share Docker images with other teams or organizations.

Push project image to Docker Registry

In this section, we will push the project image to the Docker Registry. We will use the GitLab Container Registry to store the image. The GitLab Container Registry is a private Docker registry that is integrated with GitLab. It allows you to store Docker images and use them in your CI/CD pipelines.

  1. Create a "Dockerfile" file in the root directory of your project. This file contains the instructions needed to build the image.

    FROM denoland/deno:alpine-1.26.0
    
    # 建立一個資料夾
    WORKDIR /app
    
    # 複製檔案到資料夾
    COPY . /app
    
    # 設定 port
    EXPOSE 8000
    
    # 執行指令
    RUN deno cache main.ts
    
    # 把 server 跑起來
    CMD ["run", "--allow-all", "main.ts"]
    

    Run the following command to build the image.

    docker build -t [image_name] .
    
  2. Create a ".gitlab-ci.yml" file in the root directory of your project. This file contains the instructions needed to build the image.

    stages:
        - testing
        - build
        - publish
    
    run_tests:
    stage: testing
    image: denoland/deno:latest
    script:
        - deno test
    
    build_docker_image:
    stage: build
    tags:
        - remote
    needs:
        - run_tests
    before_script:
        - export IMAGE_VERSION=$(cat deno.json | jq -r .version)
    script:
        - docker build -t $CI_REGISTRY_IMAGE:$IMAGE_VERSION . # CLI command (build)
        - echo "job [build_docker_image] done!!"
    
    push_to_registry:
    stage: publish
    tags:
        - remote
    needs:
        - build_docker_image
    before_script:
        - export IMAGE_VERSION=$(cat deno.json | jq -r .version)
        - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY # CLI command (login)
    script:
        - docker push $CI_REGISTRY_IMAGE:$IMAGE_VERSION # CLI command (push)
    

    Find the CLI command in Package and Registry >>> Container Registry in GitLab.

  3. Create a "deno.json" file in the root directory of your project. This file contains the version of the project.

    {
      "version": "0.1.0"
    }
    

    Use the following command to get the version of the project.

    cat deno.json | jq -r .version
    

    In the ".gitlab-ci.yml" file, we use the following command to get the version of the project.

    export IMAGE_VERSION=$(cat deno.json | jq -r .version)