Docker Container

Important

For automated builds of docker images on GitLab make sure to enable the Pipelines and Container Registry in your repository. More on that [[cicd:gitlab:start|here]].

Setup

Create a new repository and add a Dockerfile and all required resources:

git clone git@gitlab.ti.bfh.ch/my/repo.git
cd repo
vi Dockerfile

GitLab

Add the .gitlab-ci.yml file to your repository and commit and push to GitLab. By adding following .gitlab-ci.yml to your repository, each commit will trigger a new build of the container:

.gitlab-ci.yml

# This file is a template, and might need editing before it works on your project.
build-master:
  # Official docker image.
  image: docker:latest
  stage: build
  services:
    - docker:18.04-dind
  before_script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
  script:
    - docker build --pull -t "$CI_REGISTRY_IMAGE" .
    - docker push "$CI_REGISTRY_IMAGE"
  only:
    - master

build:
  # Official docker image.
  image: docker:latest
  stage: build
  services:
    - docker:18.04-dind
  before_script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
  script:
    - docker build --pull -t "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG" .
    - docker push "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG"
  except:
    - master

Important

As of creation, there was a bug which prevented docker CI pipelines form working if the docker service was not set to docker:18.04-dind. The example for docker builds provided by GitLab sadly do no contain this bugfix.

git add .
git commit -m"commit message"
git push

Local Build

Important

This requires docker. Make sure to install docker beforehand (How to install docker).

sudo docker build -t <tag> -f Dockerfile .