r/devops•
8solutions

Gitlab CI: feel like I'm going crazy, how do I build a docker Image?

I'd like to just build a Docker image for a node.js app. The Dockerfile builds on my computer. I have gone through the Gitlab CI Docker documentation and it feels like it's taking me to the stars and back but I'm having difficulty understanding whether I need to register a runner or where to get started. (https://docs.gitlab.com/ee/ci/docker/using_docker_build.html)

My assumption was that it would be something like the following. The build-node stage is not exactly what I'm using but it is working correctly -- there are also testing and lint stages that are working, too. The docker image build is where I'm tripping up.

//gitlab-ci.yml

build-node:
  stage: build
  before_script:
    - yarn install --ignore-engines --frozen-lockfile
  script:
    yarn build

build-docker:
  stage: dockerize
  image: some-standard-gitlab-docker-build-image
  script:
    - docker build --rm -f ./docker/Dockerfile -t my-app:latest .

Posted Feb 27, 2020, 11:46 PM UTC

27
flushy78•

If you're running Gitlab.com, then each code repo has it's own private Docker registry, which you can push and pull to/from. You'd need to create an API access token for the registry (under your account settings), and place it in your project's Settings > CI/CD > Variables area, which then passes it into your CI process as an env var.

Here's the container build stage from one of my recent projects that I managed to get it working with, hope it helps you out.

container-build:
image: docker:stable #19.03.1
stage: registry
allow_failure: false
services:
- docker:dind #19.03.1-dind
variables:
# Use TLS https://docs.gitlab.com/ee/ci/docker/using_docker_build.html#tls-enabled
DOCKER_HOST: tcp://docker:2376
DOCKER_TLS_CERTDIR: "/certs"
REGISTRY_URL: registry.gitlab.com
IMAGE_TAG: $CI_REGISTRY_IMAGE/$CI_COMMIT_REF_SLUG:$CI_COMMIT_SHA
DOCKERFILE_PATH: docker/Dockerfile-alpine
script:
- . build_info
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_TOKEN $REGISTRY_URL
- docker build -f $DOCKERFILE_PATH --build-arg BUILD_VERSION=${RELEASE_VERSION} --build-arg BUILD_SUFFIX=$SUFFIX -t $IMAGE_TAG .
- docker push $IMAGE_TAG
only: - master

3
UncleCJ•

Oh, but I've seen confusing, possibly confused variations of this everywhere - but never with the DOCKER_HOST variable set like that. Do I have it right that you use image:docker for the CLI, but then for some reason build inside services: docker:dind? Why not go with dind image (or even runner tag) from the start? I'm sure there's a page about it somewhere, I just haven't read it...

2
flushy78•

Yeah it was a pain to find, but this is how they document setting up DinD within their CI runner, so that the stage talks to the correct daemon.

https://docs.gitlab.com/ee/ci/docker/using_docker_build.html

1
8solutionsOP•

Thank you very much! The image (docker:stable #19.03.1) and (docker:dind) are what I was having such a hard time figuring out. I plan on pushing to an AWS ECR repo but I think I have that aspect figured out.

1
yodamad•

Try kaniko 😉

3
varunbabu008•

This is what I am using as well. No more docker In docker and bind mounts

5
ivan_scantron•

Yeah, I struggled with it too. It's definitely not obvious from their documentation what exactly you're supposed to do. Here's what I do:

First you gotta have some sort of container registry that you can push to and that Gitlab CI can pull from. You also have to have a runner, which in my case is an AWS EC2 node. Not trivial, obviously. Then:

Build and tag it locally: docker build -t git.yourregistry.com:4567/project/docker-images/somepython .

Log in to your registry: docker login git.yourregistry.com:4567

Push the tagged image with the latest: docker push git.yourregistry.com:4567/project/docker-images/somepython:latest

Look at the new image in the container registry. In a Gitlab registry it's something like: https://git.yourregistry.com:4567/project/docker-images/container_registry

Copy the path that the Gitlab CI will use to pull the image. It's something like: git.yourregistry.com:4567/project/docker-images/somepython

Then use this in your gitlab-ci.yml:
image: git.yourregistry.com:4567/project/docker-images/somepython

Then after all that... You'll need an runner (again, in my case an EC2 node) with gitlab-ci-multi-runner installed. And with that in place, you register the runner and when prompted, add the path to your Docker image on the container registry.

It's all very convoluted, and I'm not sure I directly addressed your needs, but I hope this is helpful.

2