Docker image for your gitlab-CI
A good way to properly execute a CI is to run it in a docker composer. To do this you need to create a docker image with everythings your program needs.
You need to create a new repository on git lab with all the following files
- Dockerfile
- .gitlab-ci.yaml
Create your docker image
To create your docker image you need to create a Dockerfile
and follow 4 little steps:
- With parent image we used
- Install all program you need
- Copy your code in a work directory
- Create and use a new user (only if you use a postgresql database)
1. Parent image
You need to give to docker a parent image to create your own. Examples ubuntu 22.04 LTS.
FROM ubuntu:22.04
ARG DEBIAN_FRONTEND=noninteractive
The ARG
line tells your image that it will not be used by a real user.
You can find all official parent image here
2. Install other program
After if you need some program, like sudo, git npm, postgres, … you must install it with the standard command line of your parent image prefixed by RUN
to say to the dockerfile to run this command.
RUN apt-get update
RUN apt-get install -y sudo
RUN apt-get install -y git
RUN apt-get install -y npm
...
This example is for ubuntu.
3. Copy your code
The last step (if you don’t use postgresql database) is to copy your code and define a new work directory.
COPY ./code/
WORKDIR /code
4. Create and use a new user
If you use postgresql data base you can’t launch postgres at root. You need to be a none root user.
To do that you must be create a new user, add it in the sudo group and use this user.
RUN adduser usrrunner
RUN passwd -d usrrunner
RUN adduser usrrunner sudo
USER usrrunner
Documentation:
Example of a Dockerfile
FROM ubuntu:22.04
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update
RUN apt-get install -y apt-utils
RUN apt-get -y install libhdf5-dev
RUN apt-get install -y sudo
RUN apt-get -y install postgresql
RUN apt-get update && apt-get install -y python3.11 python3.11-dev python3.11-distutils \
python3.11-venv python3-apt python3-pipRUN apt-get install -y git
RUN apt-get install -y npm
COPY . /code/
WORKDIR /code
RUN npm install -y
RUN adduser runner
RUN passwd -d runner
RUN adduser runner sudo
USER runner
Build your docker image on gitlab
Now that you have a docker image you need to generate and store it in your container registry.
To do that you need to create 2 variables hidden in the group or project CI-CD settings.
$DEPLOY_USER
$DEPLOY_TOKEN
Once done you can create your .gitlab-ci.yaml
file and copy this example.
image: docker:latest
services:
- docker:dind
stages:
- build
build:
stage: build
before_script:
- docker login gitlab.obspm.fr:4567 -u ${DEPLOY_USER} -p ${DEPLOY_TOKEN}
script:
- docker build -t gitlab.obspm.fr:4567/<your_group>/<project_name> .
- docker push gitlab.obspm.fr:4567/<your_group>/<project_name>
- image: We use the latest docker image provided by gitlab.
- services: We use the docker’s service dind (docker in docker) to build the image.
- before_script: Before the script we logging on the gitlab with the user and the token that we jsut created.
- script: build: First we build our image that can be found on the gitlab (the Dockerfile).
- script: push: Then we push the result on the container register on the gitlab.
At each push on the main branches, the CI log to gitlab.obspm.fr with the port 4567 and a deploy user with deploy token created on the gitlab project. After login we build the image and push it in the server.
Use the image sotck in Gitlab container registry
To use your image in Gitlab-CI of other project you must add at the top of your .gitlab-ci.yaml
image: "gitlab.obspm.fr:4567/<your_group>/<your_docker_project>:latest"
Documentation only in french:
Common Errors
Permission denied while trying to connect to the Docker daemon socket
If we have a new runner and the pipeline fail with the following message.
Got permission denied while trying to connect to the Docker daemon socket
at unix:///var/run/docker.sock
You need to give the good access to the gitlab runner. For that we execute these commands on the shell of the gitlab runner’s server.
sudo usermod -aG docker gitlab-runner
sudo service docker restart
usermod
modify a user account. With options -a
and -G
with add the user (docker here) to the a group (gitlab-runner here). Then we restart the docker service.