In this post I will describe how to host your docker builds in your own gitlab docker registry. In addition, whenever a tag is built for the repository, a new image will be generated and pushed to the repository.
If integrating this into an existing proejct, you can skip this part.
We will push the docker
hello world image to a registry in gitlab.
First, create the a new blank project on gitlab
Your registry must be public.
Then, run these commands.
mkdir registry-hello-world git init git remote add origin ... echo 'FROM hello-world' >> Dockerfile git add Dockerfile git status git commit -m "add hello world" git push -u origin master
Pushing to your container registry
Now that we have a
Dockerfile, let's create a new
# .gitlab-ci.yml stages: - push variables: TAG_LATEST: $CI_REGISTRY_IMAGE:latest TAG_COMMIT: $CI_REGISTRY_IMAGE:$CI_COMMIT_TAG push to registry: only: - tags image: docker:latest services: - docker:dind script: - docker build -t $TAG_COMMIT -t $TAG_LATEST . - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY - docker push $TAG_COMMIT - docker push $TAG_LATEST
We are adding two variables to the pipeline.
TAG_COMMIT which represent the
:latest version of the tag. The other tag for the docker image will be the tag we create in git.
The pipeline script uses "Docker in Docker" (
dind) image to build the current project into the two tagged versions.
Next, we login to the Gitlab regsitry for the project, this is using the runners token which will allow us to push to the container registry for the project. These are all predefined gitlab CI variables.
x.x.x tags are also pushed.
Creating a tag in git
Now we can create a tag locally in git and push it to the repository. In my case I haven't created any tags so I will call it
git tag git tag 1.0 git push --tags
If you visit your
CI / CD -> Pipelines page in gitlab, you should see your new tag building!
Pull your image from your container registry...anywhere!
In Gitlab, if you navigate to your container registry you should see two images created.
If you repository is private, you must authenticate with the Gitlab registry before pulling your image locally. This is perfectly normal, and I do this for several of my own projects, but working with a private repository is outside of the scope of this lesson.
docker login registry.gitlab.com
You should now be able to pull your image from your Gitlab docker registry and run it locally, (...or on any machine with docker installed!). No git or build tools needed! There is a copy button next to the tag name that you can click to get the registry image name.
docker pull registry.gitlab.com/tomfordweb/registry-hello-world:latest
docker run registry.gitlab.com/tomfordweb/registry-hello-world:latest
You can view the repository for this article here.