GitLab CI/CD - Hands-On Lab: Working with the GitLab Container Registry

This Hands-On Guide walks you through building and storing a Docker container in GitLab.

Estimate time to complete: 15 - 20 minutes

Objectives

Docker is a platform commonly used by developers to build container applications. It is possible to generate Docker containers as a part of a CI/CD build process. In this lab, you will learn how to define a Dockerfile for your project, and integrate it into your .gitlab-ci.yml file.

Task A: Add a Dockerfile

  1. Open your CICD Demo project from earlier labs.

  2. Add a new file to the CICD Demo project by selecting + > This directory > New file

  3. In the Filename field, enter Dockerfile

  4. Paste the following into the body of the new file.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    
    FROM golang:1.11-alpine as builder
    WORKDIR /usr/build
    ADD main.go .
    RUN go build -o app .
    
    FROM alpine:latest
    
    WORKDIR /usr/src
    
    COPY --from=builder /usr/build/app .
    EXPOSE 8080
    
    CMD ["/usr/src/app"]
    
  5. In the Commit message field, type Add Dockerfile, ensure the Target Branch is set to main, and click Commit changes.

Task B: Define a build image Job

  1. In the left navigation bar, click Code > Repository

  2. Open the .gitlab-ci.yml file and click Edit > Edit single file. Paste the following new job definition at the end of the file.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    
    build image:
      stage: build
      image: docker:18
      services:
        - docker:18-dind
      variables:
        IMAGE: $CI_REGISTRY_IMAGE/$CI_COMMIT_REF_SLUG:$CI_COMMIT_SHA
      script:
        - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
        - docker build -t $IMAGE .
        - docker push $IMAGE
    
  3. In the Commit message field, type Add "build image" job definition, ensure the Target Branch is set to main, and click Commit changes.

Task C: Ensure the Pipeline is Running

  1. Go to Build > Pipelines. Click the most recent pipeline run.

  2. Click the widget for the build image job to see its progress. Wait for the job to complete.

  3. In the left navigation pane, click Deploy > Container Registry and view the container that was just uploaded by the build image job.

Lab Guide Complete

You have completed this lab exercise. You can view the other lab guides for this course.

Suggestions?

If you wish to make a change to the Hands-On Guide for GitLab CI/CD, please submit your changes via Merge Request!

Last modified April 26, 2024: Update Lab Descriptions (05f28a88)