Gitlab as Docker Registry
Setting Up GitLab’s Docker Registry
GitLab will set up a private Docker registry with just a few configuration updates. First we’ll set up the URL where the registry will reside. SSH into your GitLab server, then open up the GitLab configuration file:
sudo nano /etc/gitlab/gitlab.rb
Scroll down to the Container Registry settings section. We’re going to uncomment the registry_external_url
line and set it to our GitLab hostname with a port number:
registry_external_url 'https://gitlab.example.com:34578'
Save and close the file, then reconfigure GitLab:
sudo gitlab-ctl reconfigure
Now switch to another machine with Docker installed, and log in to the private Docker registry:
docker login gitlab.example.com:34578
You will be prompted for your username and password. Use your GitLab credentials to log in.
Building a Docker Image
To get our app building in Docker, we need to update the .gitlab-ci.yml
file:
image: docker:19.03.0-dind
variables:
DOCKER_HOST: tcp://docker:2375
DOCKER_TLS_CERTDIR: ""
services:
- name: docker:19.03.12-dind
entrypoint: ["env", "-u", "DOCKER_HOST"]
command: ["dockerd-entrypoint.sh"]
stages:
- build
# - test
- release
variables:
TEST_IMAGE: gitlab.example.com:34578/wiki/wiki-instar-en-docker:$CI_COMMIT_REF_NAME
RELEASE_IMAGE: gitlab.example.com:34578/wiki/wiki-instar-en-docker:latest
before_script:
- docker login -u gitlab-ci-token -p $CI_JOB_TOKEN gitlab.example.com:34578
build:
stage: build
script:
- docker build --pull -t $TEST_IMAGE .
- docker push $TEST_IMAGE
# test:
# stage: test
# - docker pull $TEST_IMAGE
# - docker run $TEST_IMAGE npm test
release:
stage: release
script:
- docker pull $TEST_IMAGE
- docker tag $TEST_IMAGE $RELEASE_IMAGE
- docker push $RELEASE_IMAGE
only:
- master
Once you commit your CI script the job will be started:
Test your Docker Registry
docker pull gitlab.example.com:34578/wiki/wiki-instar-en-docker:latest
docker run -p 80:7777 gitlab.example.com:34578/wiki/wiki-instar-en-docker:latest
Clean Up
gitlab-ctl registry-garbage-collect -m