In most cases we can use official Docker images hosted on the public Hub but in some cases there are needed to build custom images. We can add some additional packages to the OS or add everything that is needed with recurence.
To build custom images we will use a standard image from official Docker Hub and we will add some extra packages we need. For this step we will need a Dockerfile and .gitlab-ci.yml configs.
Dockerfile will be used to build our custom image and .gitlab-ci.yml will be used to do all magic in the Gitlab pipeline.
In this example our Dockerfile will looks the following:
FROM debian:10-slim
RUN echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections
RUN apt-get update
RUN apt-get install -yqq libldap2-dev libpng-dev libzip-dev zip sed php-curl php-mbstring php-gd php-zip php-ldap php-mysqli php-xdebug php-dom git curl
RUN curl https://getcomposer.org/download/latest-stable/composer.phar --output /composer.phar
RUN apt-get clean
In this uppser Dockerfile we are gonna install some php modules and composer.
Then we will need to create pipeline in Gitlab to build images automatically on every change.
Our pipeline config file will look like the following one:
stages:
- build
build:
stage: build
image:
name: gcr.io/kaniko-project/executor:debug
entrypoint: [""]
script:
- mkdir -p /kaniko/.docker
- echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_USERNAME\",\"password\":\"$CI_PASSWORD\"}}}" > /kaniko/.docker/config.json
- /kaniko/executor --context ${CI_PROJECT_DIR}/${KANIKO_CONTEXT:-.} --dockerfile Dockerfile --destination $CONTAINER_IMAGE --destination $CONTAINER_IMAGE_LATEST
only:
- tags
You will need to define the following variables:
- CI_REGISTRY: will contain your own registry url (git.example.com:port)
- CI_USERNAME: Gitlab user (can you used an dedicated user for this)
- CI_PASSWORD: Gitlab password for the defined user upper
- CONTAINER_IMAGE: Will contain the full registry ($CI_REGISTRY/docker-images/debian10-custom:$CI_COMMIT_TAG)
- CONTAINER_IMAGE_LATEST: Will contain the full registry ($CI_REGISTRY/docker-images/debian10-custom:latest)
This pipeline will be triggered only when a new tag is added to the project.
Need help designing scalable CI/CD pipelines or container strategies?
→ Explore our Virtualization & Containers Services
0 Comments