Gitlab CI Jib plugin build Docker image - docker

I am using Jib to create a docker container and push it to the registry. To do that, I would like to build a Docker image that can be used for the purpose of container scanning before pushing the image to the Gitlab registry. The issue I am facing is I cannot use maven docker image for the build as it doesn't have docker agent running. I cannot use docker image as it doesn't have the maven image. Is there any way to address this without creating a custom docker image?
Here is my .gitlab-ci.yml file related to this part:
Building:
image: docker:19.03.1 # or maven:3-jdk-8
stage: build
only:
- master
script:
- echo "Building the project"
- mvn compile jib:dockerBuild
In case of docker image:
/bin/sh: eval: line 91: mvn: not found
In case of maven image:
Build to Docker daemon failed, perhaps you should make sure Docker is installed and you have correct privileges to run it

You can build jib using mvn compile jib:build and then make docker image and push to registry in next steps.
2 . Alternatively try running as docker in docker so that The gitlab runner can use Docker images to support our pipelines and use docker as image.
image: docker:latest
services:
- docker:dind
Building:
image: maven:3-jdk-8
stage: build
only:
- master
script:
- echo "Building the project"
- mvn compile jib:dockerBuild

Related

Recover docker image after a gitlab-ci run

Let's say I build a docker image and then run some CI build like this:
stages:
- create_builder_image
- test
Create Builder Image:
stage: create_builder_image
script:
- export DOCKER_BRANCH_TAG=$CI_COMMIT_REF_SLUG
# do stuff to build the image, using cache to speed it up
- docker push $GITLAB_IMAGE/builder:$DOCKER_BRANCH_TAG
Run Tests:
image: $GITLAB_IMAGE/builder:$CI_COMMIT_REF_SLUG
stage: build
script:
# build some stuff in the image
Then I want to push the resulting image, with the builded stuff inside
docker-package:
stage: package
script:
- docker commit ?
- docker push dockerhub:latest
That may not be possible at all.
Similar to In Gitlab CI/CD, how to commit and publish the docker container that is running our stages

If possible to run a Docker Compose comand before a job exe in GitLab CI

I am new to GitLabCI, it seems GitLab CI is docker everywhere.
I was trying to run a Mariadb before run tests. In Github actions, it is very easy, just docker-compose up -d command before my mvn.
When came to GitLab CI.
I was trying to use the following job to archive the purpose.
test:
stage: test
image: maven:3.6.3-openjdk-16
services:
- name: docker
cache:
key: "${CI_JOB_NAME}"
paths:
- .sonar/cache
- .m2/repository
script: |
docker-compose up -d
sleep 10
mvn clean verify sonar:sonar
But this does not work, docker-compose is not found.
You can make use of docker-dind docker-dind and run the docker commands inside another docker container.
But there is limitation to run docker-compose by default. It is recommended to build a custom image on top of DIND and push it to gitlab image registry. So that can be used across your jobs

Publishing image with docker from gitlab ci

I am trying to create my war artifact with gradle and push it to my remote image repo. But the problem is it I am getting
COPY failed: stat /var/lib/docker/tmp/docker-builder756634785/build/libs/myartifact.war: no such file or directory.
So, It cannot reach to my artifact
how can I point to the correct location?
//gitlab-ci.yaml
stages:
- build
variables:
GRADLE_OPTS: "-Dorg.gradle.daemon=false"
GRADLE_OPTS: "-Dorg.gradle.caching=true"
build:
image: gradle:alpine
stage: build
script:
- ./gradlew clean build -i
docker_build:
image: docker:latest
stage: build
services:
- docker:dind
script:
- docker build --pull -t myrepo.io/myimage:latest .
- docker login myrepo.io -u username -p pass
- docker push myrepo.io/myimage:latest
You need to export your artifact that you generated in the build job and after that you will be able to download it on the docker_build job (using dependency)
In this doc you have a lot of examples about how to handle it https://docs.gitlab.com/ee/ci/yaml/#artifacts
and look at this example: https://docs.gitlab.com/ee/ci/yaml/#dependencies

Gitlab CI - docker: command not found

I am trying to build my docker image within the gitlab ci pipeline.
However it is not able to find the docker command.
/bin/bash: line 69: docker: command not found ERROR: Job failed: error
executing remote command: command terminated with non-zero exit code:
Error executing in Docker Container: 1
.gitlab-ci.yml
stages:
- quality
- test
- build
- deploy
image: node:8.11.3
services:
- mongo
- docker:dind
before_script:
- npm install
quality:
stage: quality
script:
- npm run-script lint
test:
stage: test
script:
- npm run-script test
build:
stage: build
script:
- docker build -t server .
deploy:
stage: deploy
script:
- echo "TODO deploy push docker image"
you need to choose an image including docker binaries
image: gitlab/dind
services:
- docker:dind
You have 2 options to fix this. You will need to edit your config.toml file (located wherever you installed your gitlab runner).
OPTION 1
in config.toml:
privileged = true
in .gitlab-ci.yml:
myjob:
stage: myjob
image: docker:latest
services:
- docker:18.09.7-dind # older version that does not need demand TLS (see below)
OPTION 2
in config.toml:
privileged = true
volumes = ["/certs/client", "/cache"]
in .gitlab-ci.yml:
myjob:
stage: myjob
image: docker:latest
services:
- docker:dind
variables:
DOCKER_DRIVER: overlay2 # not sure if this is needed
DOCKER_TLS_CERTDIR: "/certs"
IMPORTANT: ONCE YOU HAVE MADE THE CHANGES TO config.toml YOU WILL PROBABLY NEED TO RESTART THE GITLAB RUNNER (which may vary depending on OS) - I DID RESTART MINE, NOT SURE WHAT WOULD HAPPEN IF YOU DID NOT RESTART IT!
Instructions for restarting gitlab runner are here ... https://docs.gitlab.com/runner/commands/ ... basically gitlab-runner restart but on Windows I had to use Windows "Services" to restart it
Why this problem?
priviledged=true gets rid of the docker: command not found problem
However, docker:dind now requires TLS certs (whatever they are). If you are happy with an older docker version then you can use OPTION 1. If you want the latest you need to setup Gitlab CLI to use them which is OPTION 2. J.E.S.U.S loves you :)
For more info ... https://about.gitlab.com/blog/2019/07/31/docker-in-docker-with-docker-19-dot-03
Problem here is that node docker image does not embed docker binaries.
Two possibilities :
split stages to two jobs. One using node images for quality and test, one using docker image for building and deploying. See jobs documentation.
build a custom docker image that embed both node and docker and use this image to build your repo.
Note that in both case you will have to enable docker inside your agent. See documentation.

How to use the official docker image to be a service in GitLab CI?

Environment:
GitLab Community Edition 9.5.2
Description:
I used the node:8.4.0 be my main image. It will do something Node.js program in the other jobs, and I will ignore them below.
Here is my .gitlab-ci.yml:
image: node:8.4.0
services:
- docker:latest
stages:
- docker_build
docker_build_job:
stage: docker_build
script:
- sudo docker build -t my_name/repo_name .
- sudo docker images
Problem:
I cannot use the docker command in GitLab runner, and get the message below:
Running with gitlab-ci-multi-runner 9.5.0 (413da38)
on ci server running on a VM of PEM5208 (5a0ceca0)
Using Docker executor with image node:8.4.0 ...
Starting service docker:latest ...
Pulling docker image docker:latest ...
Using docker image docker:latest ID=sha256:be47faef67c2e5950a540799e72189867b517010ad8ef98aa0181878d81b0064 for docker service...
Waiting for services to be up and running...
*** WARNING: Service runner-5a0ceca0-project-129-concurrent-0-docker-0 probably didn't start properly.
exit code 1
*********
Using docker image sha256:3f7a536cd71bb3049cc0aa12fb3e131a03a33efe2175ffbb95216d264500d1a1 for predefined container...
Pulling docker image node:8.4.0 ...
Using docker image node:8.4.0 ID=sha256:60bea5b8607945a43b53f5022088a73f2817174e11a3b20f78ea78a45f545d34 for build container...
Running on runner-5a0ceca0-project-129-concurrent-0 via ci...
Fetching changes...
Removing node_modules/
HEAD is now at 472e1e4 Change the version of docker image.
From https://here-is-my-domain/my_name/repo_name
472e1e4..df29530 master -> origin/master
Checking out 472e1e45 as master...
Skipping Git submodules setup
Downloading artifacts for build_installation_job (914)...
Downloading artifacts from coordinator... ok id=914 responseStatus=200 OK token=fMsaFRzG
$ docker build -t my_name/repo_name .
/bin/bash: line 48: docker: command not found
ERROR: Job failed: exit code 1
How should I modify the YAML file of gitlab-ci, make it work successfully?

Resources