- Requirements
- Building a Docker image with kaniko
- Using a registry with a custom certificate
- Video walkthrough of a working example
- Troubleshooting
Use kaniko to build Docker images
kaniko solves two problems with using the
Docker-in-Docker build
method:
To use kaniko with GitLab, a runner with one
of the following executors is required:
When building an image with kaniko and GitLab CI/CD, you should be aware of a
few important details:
In the following example, kaniko is used to:
The job runs only when a tag is pushed. A In the last step, kaniko uses the If you authenticate against the Dependency Proxy,
you must add the corresponding CI/CD variables for authentication to the If you use a custom GitLab Runner behind an http(s) proxy, kaniko needs to be set
up accordingly. This means:
The previous example can be extended as follows:
When trying to push to a Docker registry that uses a certificate that is signed
by a custom CA, you might get the following error:
This can be solved by adding your CA’s certificate to the kaniko certificate
store:
The Least Privilege Container Builds with Kaniko on GitLab
video is a walkthrough of the
Guided Exploration project pipeline. It was tested on:
The example can be copied to your own group or instance for testing. More details
on what other GitLab CI patterns are demonstrated are available at the project page.
If you receive this error, it might be due to an outside proxy. Setting the
Requirements
Building a Docker image with kaniko
gcr.io/kaniko-project/executor:debug
)
because it has a shell, and a shell is required for an image to be used with
GitLab CI/CD.
config.json
file needs to be created with the authentication
information for the desired container registry.
config.json
file is created under
/kaniko/.docker
with the needed GitLab Container Registry credentials taken from the
predefined CI/CD variables
GitLab CI/CD provides.
Dockerfile
under the
root directory of the project, builds the Docker image and pushes it to the
project’s Container Registry while tagging it with the Git tag:
build:
stage: build
image:
name: gcr.io/kaniko-project/executor:debug
entrypoint: [""]
script:
- mkdir -p /kaniko/.docker
- echo "{\"auths\":{\"${CI_REGISTRY}\":{\"auth\":\"$(printf "%s:%s" "${CI_REGISTRY_USER}" "${CI_REGISTRY_PASSWORD}" | base64 | tr -d '\n')\"}}}" > /kaniko/.docker/config.json
- >-
/kaniko/executor
--context "${CI_PROJECT_DIR}"
--dockerfile "${CI_PROJECT_DIR}/Dockerfile"
--destination "${CI_REGISTRY_IMAGE}:${CI_COMMIT_TAG}"
rules:
- if: $CI_COMMIT_TAG
config.json
file:
- echo "{\"auths\":{\"$CI_REGISTRY\":{\"auth\":\"$(printf "%s:%s" "${CI_REGISTRY_USER}" "${CI_REGISTRY_PASSWORD}" | base64 | tr -d '\n')\"},\"$CI_DEPENDENCY_PROXY_SERVER\":{\"auth\":\"$(printf "%s:%s" ${CI_DEPENDENCY_PROXY_USER} "${CI_DEPENDENCY_PROXY_PASSWORD}" | base64 | tr -d '\n')\"}}}" > /kaniko/.docker/config.json
Building an image with kaniko behind a proxy
/kaniko/.docker/config.json
http_proxy
environment variables as build arguments so the Dockerfile
instructions can use the proxy when building the image.
build:
stage: build
image:
name: gcr.io/kaniko-project/executor:debug
entrypoint: [""]
script:
- mkdir -p /kaniko/.docker
- |-
KANIKOPROXYBUILDARGS=""
KANIKOCFG="\"auths\":{\"${CI_REGISTRY}\":{\"auth\":\"$(printf "%s:%s" "${CI_REGISTRY_USER}" "${CI_REGISTRY_PASSWORD}" | base64 | tr -d '\n')\"}}"
if [ "x${http_proxy}" != "x" -o "x${https_proxy}" != "x" ]; then
KANIKOCFG="${KANIKOCFG}, \"proxies\": { \"default\": { \"httpProxy\": \"${http_proxy}\", \"httpsProxy\": \"${https_proxy}\", \"noProxy\": \"${no_proxy}\"}}"
KANIKOPROXYBUILDARGS="--build-arg http_proxy=${http_proxy} --build-arg https_proxy=${https_proxy} --build-arg no_proxy=${no_proxy}"
fi
KANIKOCFG="{ ${KANIKOCFG} }"
echo "${KANIKOCFG}" > /kaniko/.docker/config.json
- >-
/kaniko/executor
--context "${CI_PROJECT_DIR}"
--dockerfile "${CI_PROJECT_DIR}/Dockerfile"
"${KANIKOPROXYBUILDARGS}"
--destination "${CI_REGISTRY_IMAGE}:${CI_COMMIT_TAG}"
rules:
- if: $CI_COMMIT_TAG
Using a registry with a custom certificate
$ /kaniko/executor --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/Dockerfile --no-push
INFO[0000] Downloading base image registry.gitlab.example.com/group/docker-image
error building image: getting stage builder for stage 0: Get https://registry.gitlab.example.com/v2/: x509: certificate signed by unknown authority
before_script:
- mkdir -p /kaniko/.docker
- echo "{\"auths\":{\"${CI_REGISTRY}\":{\"auth\":\"$(printf "%s:%s" "${CI_REGISTRY_USER}" "${CI_REGISTRY_PASSWORD}" | base64 | tr -d '\n')\"}}}" > /kaniko/.docker/config.json
- |
echo "-----BEGIN CERTIFICATE-----
...
-----END CERTIFICATE-----" >> /kaniko/ssl/certs/additional-ca-cert-bundle.crt
Video walkthrough of a working example
Troubleshooting
403 error: “error checking push permissions”
http_proxy
and https_proxy
environment variables
can fix the problem.