---
# System prepended metadata

title: Build Docker Image in Gitlab-CI
tags: [gitlab, ' ci-cd', ' docker']

---

---
tags: gitlab, ci-cd, docker
---

# Build Docker Image in Gitlab-CI

## Requirements

### Variables
- `CI_REGISTRY`
- `CI_REGISTRY_USER` <-- if you are using dockerhub registry
- `CI_REGISTRY_PASSWORD` <-- if you are using dockerhub registry

## Examples

### Push to Gitlab registry

```yaml=
TODO: Write example here
```

### Push to DockerHub registry

```yaml=
stages:
    - build

variables:
    CI_REGISTRY_IMAGE: yourimagename

build:
    stage: build
    image:
        name: gcr.io/kaniko-project/executor:debug
        entrypoint: [""]
    script:
        - echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json
        - /kaniko/executor --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/Dockerfile --destination $CI_REGISTRY_IMAGE:${CI_COMMIT_TAG//v} --destination $CI_REGISTRY_IMAGE:latest
    only:
        - tags
```

## Resources
- https://www.balticlsc.eu/gitlab/help/ci/docker/using_kaniko.md
