GitLab CI/CD: Build Docker Image & Push to Registry

GitLab CI/CD can be used with Docker to build Docker images.

For example, you can create a Docker image of your application and push it to a GitLab’s Container Registry.

In this note i am showing an example of how to create a new repository in GitLab with a CI/CD pipeline that will be used for building a “Hello World” Docker image and pushing it to a registry.

Cool Tip: Save username & password in Git credentials store! Read more →

GitLab CI/CD: Build Docker Image & Push to Registry

Create a new gitlab-build-docker-image Git repository, push it to GitLab and set up a remote origin (replace <username> in the URLs with your username):

$ mkdir gitlab-build-docker-image
$ cd gitlab-build-docker-image
$ git init
$ touch .gitignore README.md
$ git add -A
$ git commit -m 'Initial commit'
# for Git over HTTPS
$ git push --set-upstream https://gitlab.com/<username>/gitlab-build-docker-image.git
$ git remote add origin https://gitlab.com/<username>/gitlab-build-docker-image.git
# for Git over SSH
$ git push --set-upstream git@gitlab.com/<username>/gitlab-build-docker-image.git
$ git remote add origin git@gitlab.com/<username>/gitlab-build-docker-image.git

Build Docker Image using GitLab CI/CD

Create a minimal Dockerfile at the root of your repository:

# Dockerfile

FROM alpine
RUN echo "Hello World"

At the same directory, create a .gitlab-ci.yml with the contents as follows:

# .gitlab-ci.yml

variables:
  IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG
  # Tell 'docker:dind' to enable TLS (recommended)
  # and generate certificates in the specified directory.
  DOCKER_TLS_CERTDIR: "/certs"

build-push-docker-image-job:
  # Specify a Docker image to run the job in.
  image: docker:latest
  # Specify an additional image 'docker:dind' ("Docker-in-Docker") that
  # will start up the Docker daemon when it is brought up by a runner.
  services:
    - docker:dind
  script:
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    - docker build -t $IMAGE_TAG .
    - docker push $IMAGE_TAG

The pipeline in the example above is building a Docker image from the Dockerfile created earlier and pushes it to the project’s Container Registry.

It uses the following GitLab’s predefined variables:

Environment variable Description
CI_REGISTRY_IMAGE The address of the project’s Container Registry.
CI_COMMIT_REF_SLUG The branch or tag name for which project is built – in lowercase, shortened to 63 bytes, and with everything except 0-9 and a-z replaced with -.
CI_REGISTRY The address of the GitLab Container Registry. This variable includes a :port value if one is specified in the registry configuration.
CI_REGISTRY_USER The username to push containers to the project’s GitLab Container Registry.
CI_REGISTRY_PASSWORD The password to push containers to the project’s GitLab Container Registry. This password value is the same as the CI_JOB_TOKEN and is valid only as long as the job is running. Use the CI_DEPLOY_PASSWORD for long-lived access to the registry.

Cool Tip: Print all environment variables in GitLab CI/CD! Read more →

Leave a Reply