GitLab Runner: Pushing to the GitLab container registry

This documentation is part of the Building Docker images guide. You can view the complete guide here: Build and push Docker images efficiently from your GitLab CI/CD pipelines using Stackhero runners and Docker-in-Docker.

👋 Welcome to the Stackhero documentation!

Stackhero offers a straightforward GitLab Runner cloud solution that makes running your GitLab CI/CD jobs efficient and hassle-free. Here’s what you can expect:

  • Unlimited CI/CD minutes: run your pipelines as often as you need, with no per-minute billing or unexpected charges.
  • Multiple concurrent jobs: speed up your development by running several jobs in parallel.
  • The Docker executor with Docker-in-Docker support: easily build and push container images as part of your CI/CD process.
  • Works seamlessly with both GitLab.com and self-managed GitLab instances.
  • A private, dedicated infrastructure with fast NVMe/SSD storage ensures stable and predictable build performance.
  • Available in 🇪🇺 Europe and 🇺🇸 USA regions to meet your team’s requirements.

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

GitLab provides predefined variables (CI_REGISTRY, CI_REGISTRY_USER, CI_REGISTRY_PASSWORD, CI_REGISTRY_IMAGE) so your pipeline can authenticate and push images to your project's container registry securely. No additional secrets are required.

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

build-and-push:
  stage: build
  image: docker:29
  services:
    - name: docker:29-dind
      alias: docker
  variables:
    DOCKER_HOST: "tcp://docker:2375"
    DOCKER_TLS_CERTDIR: ""
  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

To push your images to a different registry (such as Docker Hub or a private registry), you can store credentials as CI/CD variables and use them with docker login in the same way.