Build and Push Your Docker Images to Gitlab Container Registry
When building your application inside Docker you want a private location to store your Docker images. Gitlab has a Docker container registry included in their services. Integrating Gitlab container registry is really simple if you are aleady using Gitlab to host your code. This guide explains how you can integrate Gitlab container registry into your project.
Prerequisites
- Gitlab project
- Knowledge of Docker
Build File Config
To build Docker images we use the latest docker image image: docker:latest
. One additional setting needed in the build file is the docker:dind
service. This service activates docker-in-docker service, which makes Docker able to run within a Docker environment.
image: docker:latest
services:
- docker:dind
Variables
If you have to write the same string more than once it is a good idea to extract it as a variable. We make a local variable called REPOSITORY
. `$CI_REGISTRY` is a built in variable in Gitlab, which points to the registry url. In our case this will be registry.gitlab.com
. The variable stored will have the value registry.gitlab.com/devguides/docker
.
variables:
REPOSITORY: $CI_REGISTRY/devugides/docker
Docker Login
Before we can push our docker images to the registry we have to login. This is done by using the docker login
command. Gitlab also have a built in user you can use to authenticate with. The user is available by using the built in variable CI_REGISTRY_USER
. The user gets a one time password which is stored in the variable CI_REGISTRY_PASSWORD
. For the complete login command we then get the following:
before_script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
You can read more about Gitlab registry authentication methods in the documentation.
Build Docker Image
In this part of the file we define the Docker image build and push. First we build the Docker image docker build -f Dockerfile -t $REPOSITORY:latest .
. Notice that the variable $REPOSITORY
is the variable we created earlier. The build command assumes that you have a Dockerfile
placed in your root folder of your project. The command also adds the tag latest
to the Docker image. If you want, you can add more tags for instance, you can add build number. After building the image, the image is pushed to the docker registry using the docker push
command.
Lastly, we only want to build the Docker image when code is pushed to the master
branch, and it is achieved by adding the master
branch under the only
section. The complete build step looks like this:
build:
stage: build
script:
- docker build -f Dockerfile -t $REPOSITORY:latest .
- docker push $REPOSITORY:latest
only:
- master
Final Build File
The final Gitlab build file can be found below:
image: docker:latest
services:
- docker:dind
stages:
- build
variables:
REPOSITORY: $CI_REGISTRY/devugides/docker
before_script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
build:
stage: build
script:
- docker build -f Dockerfile -t $REPOSITORY:latest .
- docker push $REPOSITORY:latest
only:
- master
If you want to see a complete example of a project with a Dockerfile and the Gitlab build file you can have a look at this Gitlab repository.
Support
If you want to support this blog you can do so by signing up to DigitalOcean using this referral link.