Safe way to login a Docker registry on GitLab CI

published on | edited on

Recently I came a cross with GitLab CI problem when I wanted to login to to the registry.

WARNING! Using --password via the CLI is insecure. Use --password-stdin.

Solution itself is easy as just copy and configure as your need.

image: docker:stable-git

services:
  - docker:dind

stages:
  - build

before_script:
  - echo $CI_BUILD_TOKEN | docker login --username=gitlab-ci-token --password-stdin registry.gitlab.com

after_script:
  - docker logout registry.gitlab.com

build:docker:
  stage: build
  script:
    - docker build -t registry.gitlab.com/groupname/reponame/master:docker .
    - docker push registry.gitlab.com/groupname/reponame/master:docker

If you Google it this problem, and you can see many solved problem which didn’t work with GitLab CI. I found this solution than I tried and everything works as expected.