> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Demo Image Automation Controller Repository

> Tutorial showing how to configure FluxCD ImageRepository with Docker Hub to scan image tags and enable automated GitOps updates using ImagePolicy and ImageUpdateAutomation

In this guide we'll connect FluxCD's ImageRepository custom resource to a Docker Hub image repository to fetch image metadata (tags). Flux's ImageRepository scans a registry and records available tags; later you can use that information to drive automated updates with ImagePolicy and ImageUpdateAutomation (GitOps).

Replace `siddharth67` below with your own [Docker Hub](https://hub.docker.com) username when building and pushing images.

## Overview

This tutorial covers:

* Creating an ImageRepository resource to point Flux at your Docker Hub repo.
* Forcing Flux to reconcile your flux-system Git source.
* Building and pushing a new image tag to Docker Hub.
* Verifying that Flux discovers the newly pushed tag.
* Next steps: ImagePolicy and ImageUpdateAutomation to automate Git updates.

Relevant keywords: FluxCD, ImageRepository, GitOps, Docker Hub, ImagePolicy, ImageUpdateAutomation, reconcile, kubectl.

## 1. Create an ImageRepository resource

You can create the ImageRepository resource directly with the Flux CLI or prepare a YAML manifest and commit it to the Git repository monitored by `flux-system`. Below is the YAML manifest (kind: `ImageRepository`) used in this demo:

```yaml theme={null}
---
apiVersion: image.toolkit.fluxcd.io/v1beta2
kind: ImageRepository
metadata:
  name: 8-demo-image-repo-bb-app
  namespace: flux-system
spec:
  image: docker.io/siddharth67/bb-app-flux-demo
  interval: 10s
```

To export an editable YAML using the Flux CLI:

```bash theme={null}
flux create image repository 8-demo-image-repo-bb-app \
  --image docker.io/siddharth67/bb-app-flux-demo \
  --interval 10s \
  --export > 8-demo-image-repo-bb-app.yml
```

Commit the exported or hand-edited YAML to the Git repository watched by the `flux-system` GitRepository.

## 2. Force Flux to reconcile the Git source

After committing the ImageRepository YAML, trigger an immediate reconciliation so Flux fetches the latest Git revision and applies any new resources:

```bash theme={null}
flux reconcile source git flux-system
```

Example CLI output showing Flux annotated and fetched the revision:

```text theme={null}
► annotating GitRepository flux-system in flux-system namespace
✔ GitRepository annotated
◎ waiting for GitRepository reconciliation
✔ fetched revision main@sha1:dc9922e7f45b3bdc8d085e68766869b15a17919c
```

Check image resources discovered by Flux. Depending on your Flux CLI version you can use:

```bash theme={null}
flux get images all
```

Or inspect the ImageRepository directly with kubectl:

```bash theme={null}
kubectl -n flux-system get imagerepositories.image.toolkit.fluxcd.io 8-demo-image-repo-bb-app
```

Example output indicating a successful scan and the number of tags found:

```text theme={null}
NAME                         LAST SCAN                 TAGS
8-demo-image-repo-bb-app     2023-04-06T13:54:05Z      1
```

(An initial scan may show only a single tag if your Docker Hub repo had one tag at the time.)

ImageRepository only scans the registry and reports tags. It does not change your Deployment images by itself. To automate updates you must add an ImagePolicy and ImageUpdateAutomation (or manually update the Deployment image in Git).

## 3. Make source changes, build a new image tag, and push to Docker Hub

Update your application source (for example, bump the app version and change UI assets), then build and push a new Docker image tag.

Example Dockerfile used in the project:

```dockerfile theme={null}
FROM php:7.4-apache
RUN docker-php-ext-install mysqli
COPY highscore.php index.php level2.php /var/www/html/
COPY images /var/www/html/images
EXPOSE 80
```

Build the new image locally and tag it (example tag `7.8.1`):

```bash theme={null}
# change directory to your app source where Dockerfile is located
docker build -t siddharth67/bb-app-flux-demo:7.8.1 .
```

Push the new tag to Docker Hub:

```bash theme={null}
docker push siddharth67/bb-app-flux-demo:7.8.1
```

Example push output (truncated):

```text theme={null}
b52848425b7: Pushed
f96c791f71d1: Pushed
5b617b432894: Pushed
...
7.8.1: digest: sha256:3b6707dd42dbb8eb02260b744d51e8a95f7fe68181f94aca69cbab7d99f2c0cb size: 3662
```

Then refresh your Docker Hub repository page and confirm the new tag (for example `7.8.1`) is visible.

## 4. Confirm Flux discovers the new tag

Flux's ImageRepository controller will scan the registry on its configured `interval` and should detect the new tag. You can force a Git reconciliation again to ensure Flux's configuration is current, then inspect the ImageRepository status to verify discovered tags.

Force reconciliation (if needed):

```bash theme={null}
flux reconcile source git flux-system
```

View the ImageRepository status to see `lastScanResult` and `latestTags`:

```bash theme={null}
kubectl -n flux-system get imagerepositories.image.toolkit.fluxcd.io 8-demo-image-repo-bb-app -o yaml
```

Look for a status block similar to:

```yaml theme={null}
status:
  lastScanResult:
    latestTags:
    - 7.8.1
    - 7.8.0
    scanTime: "2023-04-06T13:54:05Z"
    tagCount: 2
```

You can also list all ImageRepository resources and their tag counts:

```bash theme={null}
kubectl -n flux-system get imagerepositories.image.toolkit.fluxcd.io
```

Example showing two tags discovered:

```text theme={null}
NAME                         LAST SCAN                 TAGS
8-demo-image-repo-bb-app     2023-04-06T13:54:05Z      2
```

To confirm your running Deployment still uses the previous image tag (i.e., no automatic update has been performed yet):

```bash theme={null}
kubectl -n 8-demo get deployment block-buster -o wide
```

Example output showing the deployment still using the older tag:

```text theme={null}
NAME            READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES                                   SELECTOR
block-buster    1/1     1            1           15m   app          siddharth67/bb-app-flux-demo:7.8.0     app=block-buster
```

## 5. What's next: automate updates with ImagePolicy and ImageUpdateAutomation

To enable fully automated GitOps image updates:

* Create an `ImagePolicy` to select which tags are eligible (e.g., semantic versioning rules, regex).
* Create an `ImageUpdateAutomation` that updates the manifests in your Git repository when a new tag matching the policy appears.
* Flux will then update the Git repository and the GitRepository controller will apply the updated manifest, updating your Deployment.

Summary of the primary image automation resources:

| Resource Type | Purpose | Example / Notes |
| - | - | - |
| ImageRepository | Scans a registry and reports available tags | See the `ImageRepository` YAML above |
| ImagePolicy | Selects the desired tag (semver, regex, etc.) | Use to choose latest matching tag, e.g., `semver` strategy |
| ImageUpdateAutomation | Updates Git manifests when ImagePolicy selects a new tag | Runs automation that creates commits or PRs in your cluster Git repo |

Example commands (overview):

```bash theme={null}
# Create ImagePolicy - example (use flux CLI or a YAML manifest)
# Create ImageUpdateAutomation - example (use flux CLI or a YAML manifest)
# flux create image update <name> --git-repo=<repo> --git-branch=<branch> --path=<path> --export > imageupdateautomation.yml
```

Refer to Flux docs for detailed manifests and configuration options: the exact manifests depend on your repo structure, Git provider, and desired automation workflow.

## Quick reference table: useful commands

| Action | Command |
| - | - |
| Export ImageRepository with Flux CLI | `flux create image repository 8-demo-image-repo-bb-app --image docker.io/siddharth67/bb-app-flux-demo --interval 10s --export > 8-demo-image-repo-bb-app.yml` |
| Force Git reconciliation | `flux reconcile source git flux-system` |
| List images discovered by Flux | `flux get images all` |
| Get ImageRepository with kubectl | `kubectl -n flux-system get imagerepositories.image.toolkit.fluxcd.io 8-demo-image-repo-bb-app -o yaml` |
| Build Docker image | `docker build -t siddharth67/bb-app-flux-demo:7.8.1 .` |
| Push Docker image | `docker push siddharth67/bb-app-flux-demo:7.8.1` |

## Links and references

* FluxCD Image Toolkit: [https://fluxcd.io/docs/components/image/](https://fluxcd.io/docs/components/image/)
* FluxCD Image Update Automation: [https://fluxcd.io/docs/components/image/image-update/](https://fluxcd.io/docs/components/image/image-update/)
* Kubernetes kubectl reference: [https://kubernetes.io/docs/reference/kubectl/](https://kubernetes.io/docs/reference/kubectl/)
* Docker Hub: [https://hub.docker.com](https://hub.docker.com)

That's all for this demo. Follow-up steps: define an ImagePolicy and ImageUpdateAutomation to enable end-to-end automated image updates via GitOps.

> **[Watch Video](https://learn.kodekloud.com/user/courses/gitops-with-fluxcd/module/e4076c7b-6728-412c-b4d7-9316fc346fc5/lesson/be049595-7896-451b-b9fc-673552cab8b3)**

> **[Practice Lab](https://learn.kodekloud.com/user/courses/gitops-with-fluxcd/module/e4076c7b-6728-412c-b4d7-9316fc346fc5/lesson/9ab613c6-d810-4ae5-ae9a-856294eb32c4)**


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.