-
Benguria Elguezabal, Gorka authoredBenguria Elguezabal, Gorka authored
GitLab CI template for Docker
This project implements a GitLab CI/CD template to build, check and inspect your containers with Docker.
Usage
This template can be used both as a CI/CD component
or using the legacy include:project
syntax.
Use as a CI/CD component
Add the following to your gitlab-ci.yml
:
include:
# 1: include the component
- component: gitlab.com/to-be-continuous/docker/gitlab-ci-docker@5.10.2
# 2: set/override component inputs
inputs:
build-tool: buildah # ⚠ this is only an example
Use as a CI/CD template (legacy)
Add the following to your gitlab-ci.yml
:
include:
# 1: include the template
- project: 'to-be-continuous/docker'
ref: '5.10.2'
file: '/templates/gitlab-ci-docker.yml'
variables:
# 2: set/override template variables
DOCKER_BUILD_TOOL: buildah # ⚠ this is only an example
Understanding the Docker template
The template supports following ways of building container images:
- The former Docker-in-Docker (DinD) technique, that was widely used for years because of no other alternative, but that is now commonly recognized to have significant security issues (read this post for more info),
- Or using kaniko, an open-source, daemonless tool from Google for building Docker images, and that solves Docker-in-Docker security issues (and also speeds-up build times).
- Or using buildah, an open-source, daemonless tool backed by RedHat for building Docker images, and that solves Docker-in-Docker security issues (and also speeds-up build times), and can also be configured to run rootless.
By default, the template uses the kaniko way, but you may
select an alternate build tool by using the DOCKER_BUILD_TOOL
variable (see below).
Global variables
The Docker template uses some global configuration used throughout all jobs.
Input / Variable | Description | Default value |
---|---|---|
build-tool / DOCKER_BUILD_TOOL
|
The build tool to use for building container image, possible values are kaniko , buildah or dind
|
kaniko |
kaniko-image / DOCKER_KANIKO_IMAGE
|
The image used to run kaniko - for kaniko build only
|
gcr.io/kaniko-project/executor:debug (use debug images for GitLab) |
buildah-image / DOCKER_BUILDAH_IMAGE
|
The image used to run buildah - for buildah build only
|
quay.io/buildah/stable |
image / DOCKER_IMAGE
|
The Docker image used to run the docker client (see full list) - for Docker-in-Docker build only | registry.hub.docker.com/library/docker:latest |
dind-image / DOCKER_DIND_IMAGE
|
The Docker image used to run the Docker daemon (see full list) - for Docker-in-Docker build only | registry.hub.docker.com/library/docker:dind |
file / DOCKER_FILE
|
The path to your Dockerfile
|
Dockerfile |
context-path / DOCKER_CONTEXT_PATH
|
The Docker context path (working directory) | none only set if you want a context path different from the Dockerfile location |
In addition to this, the template supports standard Linux proxy variables:
Input / Variable | Description | Default value |
---|---|---|
http_proxy |
Proxy used for http requests | none |
https_proxy |
Proxy used for https requests | none |
no_proxy |
List of comma-separated hosts/host suffixes | none |
Images
For each Dockerfile, the template builds an image that may be pushed as two distinct images, depending on a certain workflow:
- snapshot: the image is first built from the Dockerfile and then pushed to some Docker registry as the snapshot image. It can be seen as the raw result of the build, but still untested and unreliable.
-
release: once the snapshot image has been thoroughly tested (both by
package-test
stage jobs and/oracceptance
stage jobs after being deployed to some server), then the image is pushed one more time as the release image. This second push can be seen as the promotion of the snapshot image being now tested and reliable.
In practice:
- the snapshot image is always pushed by the template (pipeline triggered by a Git tag or commit on any branch),
- the release image is only pushed:
- on a pipeline triggered by a Git tag,
- on a pipeline triggered by a Git commit on
master
.
The snapshot and release images are defined by the following variables:
Input / Variable | Description | Default value |
---|---|---|
snapshot-image / DOCKER_SNAPSHOT_IMAGE
|
Docker snapshot image | $CI_REGISTRY_IMAGE/snapshot:$CI_COMMIT_REF_SLUG |
release-image / DOCKER_RELEASE_IMAGE
|
Docker release image | $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME |
As you can see, the Docker template is configured by default to use the GitLab container registry. You may perfectly override this and use another Docker registry, but be aware of a few things:
- the
DOCKER_SNAPSHOT_IMAGE
requires a Docker registry that allows tag overwrite, - the
DOCKER_RELEASE_IMAGE
may use a Docker registry that doesn't allow tag overwrite, but:- you should avoid overwriting a Git tag (at it will obviously fail while trying to (re)push the Docker image),
- you have to deactivate publish on
main
(ormaster
) branch by setting the$DOCKER_PROD_PUBLISH_STRATEGY
variable tonone
(as it would lead to themain
tag being overwritten).