Building Docker image in Gitlab-CI
Hi readers, been a while i wrote something, so today we would be learning how to build a docker image in Gitlab CI-CD using the Gitlab runner.
NOTE: All commands displayed where tested on a Linux Machine (Linux Mint)
The first step is to install Gitlab Runner using your terminal, use this command to install Gitlab Runner:
sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
Also for more distros you can check out the Gitlab Docs for installation.
After Installation you might want to change access file permission of Gitlab Runner to execute, use this command in your terminal:
sudo chmod +x /usr/local/bin/gitlab-runner
Then you might to run the service using this command:
sudo gitlab-runner start
After successful installation, its time to build Docker image in Gitlab. There are various approach in which this can be done but i will be using the Docker Socket Binding approach.
To do this you need a token and this is not the user setting Access Tokens, you need to generate one and to do that all you have to do is to create a new Project in Gitlab,
After successfully creating a project, go to Setting on the left side of the screen and click on CI/CD
Now go to the Runners tab and click on Expand
Now scroll down to Specific Runners and you should find an image like the one below:
So first you would want to register Gitlab Runner using the following command:
sudo gitlab-runner register
Then you will be requested to input the Gilab-CI coordinator URL which is:
Then you will be asked to enter Gitlab-CI token you got from creating a new project. After which you will be asked for a Description of the Runner you are creating, in my case am using test-runner. Then you will be asked for a tag for the Runner. After typing in a tag name for the Runner, you will be asked to enter the executor, which means where you want the Runner to execute. Since we are deploying to Docker, it would make sense if we use Docker as the executor. And then you will be asked to enter the default docker image, i will be using docker:18.09.7
Now if you go back to your Specific Runner page on Gitlab you should have an output like the one below:
The next thing is to set up a config.toml file in a .gitlab-runner folder, which will be in the root folder. The config.toml file will have this code inside:
concurrent = 1
check_interval = 0[session_server]
session_timeout = 1800[[runners]]
name = “custom-runner-1”
url = “https://gitlab.com/"
token = “[token]”
executor = “docker”
[runners.custom_build_dir]
[runners.docker]
tls_verify = false
image = “alpine:latest”
privileged = true
disable_entrypoint_overwrite = false
oom_kill_disable = false
disable_cache = false
volumes = [“/var/run/docker.sock:/var/run/docker.sock”, “/cache”]
shm_size = 0
[runners.cache]
[runners.cache.s3]
[runners.cache.gcs]
Now with your project created you need to clone it to your system because we will be adding a docker file, and a .gitlab-ci.yml file and then we will push it back to the repository.
use git clone “repo-link” to clone a repository
After that create a new file with the name docker in the folder you cloned and also create a .gitlab-ci.yml file in that folder also
In the Docker file you should type in this:
FROM alpine:3.4
RUN apk update && apk add \
curl \
git \
vim
VOLUME /git
WORKDIR /git
Every Dockerfile must start with the FROM
instruction, The idea behind is that you need a starting point to build your image. Right now am starting my image from alpine:3.4
And i added line to install curl, git and vim, then i created a VOLUME and WORKDIR for git.
In the .gitlab-ci.yml file you should have this:
image: docker:18.09.7
before_script:
— docker infobuild:
stage: build
script:
— docker build -t my-docker-image .
— docker run my-docker-image /script/to/run/tests
This is a yaml file that specify the image, show docker info, build the image we created in the and then run the image.
So once you have this two files in the same folder, all you have to do is go to your terminal, change directory into this folder you cloned, initialize git using git init
, add all the files using git add .
then commit a message using git commit -m "YourMessage"
and then use git push -u origin master
to push your code to the master branch. Once pushed, reload your project page on Gitlab and on the left side of your screen, click on CI/CD, then click on Jobs, you should see your build in progress, wait for a while and you should have this output:
That’s it right there, you have been able to build Docker Image using Gitlab Runner.