Publishing image with docker from gitlab ci - docker

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

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

Gitlab CI docker cannot login to docker hub

i have two project on gitlab with same CI config file and ci variables. When i try to build dockerfile, one project passed, but second say:
Error: Cannot perform an interactive login from a non TTY device
config:
image: docker:latest
services:
- docker:dind
stages:
- build
variables:
CONTAINER_IMAGE: sleezy/go-hello-world:${CI_COMMIT_SHORT_SHA}
build:
stage: build
script:
- docker login -u ${DOCKER_USER} -p ${DOCKER_PASSWORD}
- docker build -t ${CONTAINER_IMAGE} .
- docker tag ${CONTAINER_IMAGE} ${CONTAINER_IMAGE}
- docker tag ${CONTAINER_IMAGE} sleezy/go-hello-world:latest
- docker push ${CONTAINER_IMAGE}
How i said, everything is same, variables, dockerhub account - username, password, config, even gitlab runner version, so i really dont know why? Any help, thanks.

How to make two build inside one CircleCI configuration?

In my git repository, I have docker-compose.yml and two folders frontend and backend, each of which contains its Dockerfile.
docker-compose.yml:
version: "3.5"
services:
Django:
build:
context: backend
...
react:
build:
context: frontend
depends_on:
- django
How do I configure to build both of them into images and push them together to the docker hub after spotting new commit in the master branch?
If you are building on circleci, you dont have to build from docker compose.
You can just add steps to the circleci config (.circleci/config.yml) to build and push your images.
The file can look something like that:
version: 2
jobs:
build:
docker:
- image: docker:19.03.5
working_directory: [YOUR REPO WORKING DIR]
steps:
- checkout
- setup_remote_docker
- run:
name: build frontend
command: docker build -t [FE DOCKER REPO NAME]:tag -f [FE DOCKERFILE PATH] .
- run:
name: build backend
command: docker build -t [BE DOCKER REPO NAME]:tag -f [BE DOCKERFILE PATH] .
- run:
name: push frontend
command: |
docker login -u $DOCKER_USER -p $DOCKER_PASS
docker push [FE DOCKER REPO NAME]:tag
- run:
name: push backend
command: |
docker login -u $DOCKER_USER -p $DOCKER_PASS
docker push [BE DOCKER REPO NAME]:tag
Note that you'll have to add DOCKER_USER and DOCKER_PASS environment variables to your circleci build settings
In two words you should build images for the services you want & push them to the docker hub.
You can do it using commands like
...env configuration in your CI...
- DOCKER_ACC=my_account_name
- DOCKER_REPO_DJANGO=django-app
- DOCKER_REPO_REACT=django-app
... post tests commands ...
- echo $DOCKER_PWD | docker login -u $DOCKER_LOGIN --password-stdin
- docker build -t $DOCKER_ACC/$DOCKER_REPO_DJANGO:latest -f ./path/to/Dockerfile.django .
- docker push $DOCKER_ACC/$DOCKER_REPO_DJANGO:latest
- docker build -t $DOCKER_ACC/$DOCKER_REPO_REACT:latest -f ./path/to/Dockerfile.django .
- docker push $DOCKER_ACC/$DOCKER_REPO_REACT:latest
Where latest you can replace with your branch name, commit tag or etc and build multiple image versions for your app.
Everything depends on you, that's pretty flexible. But you have to do 2 steps - build images & push them. Built images tags should be equal to your github images urls.
Read more at articles like these ones:
http://docs.shippable.com/ci/tutorial/build-push-image-to-docker-hub/
https://circleci.com/blog/build-cicd-piplines-using-docker/

Gitlab CI Jib plugin build Docker image

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

Gitlab CI - Build Docker Image With Shared Runner (cannot connect to Docker Daemon)

I am currently using Gitlab Shared Runners to build and deploy my project (at least I'm trying too !).
I have the gitlab-ci.yml below :
image: java:8-jdk
stages:
- build
- package
before_script:
- export GRADLE_USER_HOME=`pwd`/.gradle
- docker info
cache:
paths:
- .gradle/wrapper
- .gradle/caches
build:
stage: build
script:
- ./gradlew build
artifacts:
paths:
- build/libs/*.jar
expire_in: 1 week
only:
- master
docker-build:
image: docker:stable
services:
- docker:dind
stage: package
script:
docker build -t registry.gitlab.com/my-project .
docker push registry.gitlab.com/my-project
after_script:
- echo "End CI"
First, build stage is doing great, but there is a problem with the second stage when I'm trying to build and push my docker image.
I get this log :
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
It seems that Gitlab is using a shared runner that can't build a docker image, but I don't know how I can change that. I cannot change the configuration of my runner, because I'm using shared runners. I also tried to put some tags to my second stages, in hope that a more suitable runner would have to take care of my job, but I'm still getting this error.
Thank you for your help.
I believe you need to set DOCKER_HOST to connect to the DinD running in another container:
docker-build:
image: docker:stable
services:
- docker:dind
stage: package
script:
- export DOCKER_HOST=tcp://docker:2375/
- docker build -t registry.gitlab.com/my-project .
- docker push registry.gitlab.com/my-project
If your shared runner executor is of type docker you may try this setup :
stages:
- build
- package
before_script:
- export GRADLE_USER_HOME=`pwd`/.gradle
- docker info
cache:
paths:
- .gradle/wrapper
- .gradle/caches
build:
image: java:8-jdk
stage: build
script:
- ./gradlew build
artifacts:
paths:
- build/libs/*.jar
expire_in: 1 week
only:
- master
docker-build:
stage: package
script:
docker build -t registry.gitlab.com/my-project .
docker push registry.gitlab.com/my-project
after_script:
- echo "End CI"
Even we have faced the same problem in our org. We found that there is a long standing issue with the docker in docker area for gitlab which can be tracked in these issues #3612, #2408 and #2890 as well.
We have found that in our case using docker binding was much suitable for our usecase than the docker-in-docker one. so, we used the solution in their official page.
I know this has been already answered but this may help some one who have a similar usecase :)

Resources