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
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.1stage: registryallow_failure: falseservices:- docker:dind #19.03.1-dindvariables:# Use TLS https://docs.gitlab.com/ee/ci/docker/using_docker_build.html#tls-enabledDOCKER_HOST: tcp://docker:2376DOCKER_TLS_CERTDIR: "/certs"REGISTRY_URL: registry.gitlab.comIMAGE_TAG: $CI_REGISTRY_IMAGE/$CI_COMMIT_REF_SLUG:$CI_COMMIT_SHADOCKERFILE_PATH: docker/Dockerfile-alpinescript:- . 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_TAGonly: - masterOh, but I've seen confusing, possibly confused variations of this everywhere - but never with the
DOCKER_HOSTvariable 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...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
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.