GitLab Runner: Pushing to the GitLab container registry

This documentation is part of the Building Docker images guide. View the full guide here: Build and push Docker images from your GitLab CI/CD pipelines using your Stackhero runner and Docker-in-Docker.

👋 Welcome to the Stackhero documentation!

Stackhero gives you an easy-to-use GitLab Runner cloud solution designed to handle your GitLab CI/CD jobs efficiently. Here is what you can look forward to:

  • Unlimited CI/CD minutes: there is no per-minute billing, so your pipelines can run whenever you need them.
  • Multiple concurrent jobs: run several jobs at the same time to speed up your entire pipeline.
  • The Docker executor with Docker-in-Docker support: streamline building and pushing your container images.
  • Compatible with GitLab.com as well as any self-managed GitLab instance.
  • A private, dedicated VM powered by fast NVMe/SSD disks for consistent, reliable builds.
  • Available in both 🇪🇺 Europe and 🇺🇸 USA regions.

Save time: you can connect your first GitLab Runner and start running pipelines in just a few minutes!

GitLab provides several predefined variables (CI_REGISTRY, CI_REGISTRY_USER, CI_REGISTRY_PASSWORD, CI_REGISTRY_IMAGE) so your pipeline can log in and push images to the project's container registry without needing extra secrets.

Here is an example job that builds and pushes your image:

build-and-push:
  stage: build
  image: docker:27
  services:
    - docker:27-dind
  variables:
    DOCKER_TLS_CERTDIR: "/certs"
  before_script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" "$CI_REGISTRY"
  script:
    - docker build -t "$CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA" .
    - docker push "$CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA"
    # If you are on the default branch, you can also tag and push "latest":
    - |
      if [ "$CI_COMMIT_BRANCH" = "$CI_DEFAULT_BRANCH" ]; then
        docker tag "$CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA" "$CI_REGISTRY_IMAGE:latest"
        docker push "$CI_REGISTRY_IMAGE:latest"
      fi

If you would like to push your images to a different registry (like Docker Hub or a private registry), you can store those credentials as CI/CD variables and use them with docker login in a similar way.