|
| 1 | +--- |
| 2 | +title: "Microcks" |
| 3 | +description: "How to deploy a containerized frontend application and use Microcks to mock a backend service with `score-compose` and `score-k8s`" |
| 4 | +headless: true |
| 5 | +toc_hide: true |
| 6 | +--- |
| 7 | + |
| 8 | +To begin, follow the [installation instructions](/docs/score-implementation/score-compose/installation) to install the latest version of `score-compose`. |
| 9 | + |
| 10 | +## `init` |
| 11 | + |
| 12 | +Initialize your current `score-compose` workspace, run the following command in your terminal: |
| 13 | + |
| 14 | +```bash |
| 15 | +score-compose init --no-sample \ |
| 16 | + --provisioners https://raw.githubusercontent.com/score-spec/community-provisioners/refs/heads/main/service/score-compose/10-service-with-microcks.provisioners.yaml \ |
| 17 | + --patch-templates https://raw.githubusercontent.com/score-spec/community-patchers/refs/heads/main/score-compose/microcks.tpl |
| 18 | +``` |
| 19 | + |
| 20 | +The `init` command will create the `.score-compose` directory with the [default resource provisioners]({{< relref "/docs/score-implementation/score-compose/resources-provisioners/" >}}) available. We are also importing one external provisioner to seamlessly generate a Microcks mock for the backend service resource: [`service-with-microcks` provisioner](https://github.com/score-spec/community-provisioners/blob/main/service/score-compose/10-service-with-microcks.provisioners.yaml). The [`microcks.tpl` patch template](https://github.com/score-spec/community-patchers/blob/main/score-compose/microcks.tpl) is also injected to spin up the Microcks control plane container in the generated `compose.yaml`. |
| 21 | + |
| 22 | +You can see the resource provisioners available by running this command: |
| 23 | + |
| 24 | +```bash |
| 25 | +score-compose provisioners list |
| 26 | +``` |
| 27 | + |
| 28 | +The Score file example illustrated uses one resource type: `service`. |
| 29 | + |
| 30 | +```none |
| 31 | ++---------+-------+-------------------------------------------+--------+-----------------------------------+ |
| 32 | +| TYPE | CLASS | PARAMS |OUTPUTS | DESCRIPTION | |
| 33 | ++---------+-------+-------------------------------------------+--------+-----------------------------------+ |
| 34 | +| service | (any) | port, artifacts, name, version | name, | Generates a Microcks mock for | |
| 35 | +| | | | url | an external service dependency | |
| 36 | +| | | | | using the provided OpenAPI spec. | |
| 37 | ++---------+-------+-------------------------------------------+--------+-----------------------------------+ |
| 38 | +``` |
| 39 | + |
| 40 | +## `generate` |
| 41 | + |
| 42 | +Convert the `score.yaml` file into a deployable `compose.yaml`, run the following command in your terminal: |
| 43 | + |
| 44 | +```bash |
| 45 | +score-compose generate score.yaml |
| 46 | +``` |
| 47 | + |
| 48 | +The `generate` command will add the input `score.yaml` workload to the `.score-compose/state.yaml` state file and generate the output `compose.yaml`. |
| 49 | + |
| 50 | +See the generated `compose.yaml` by running this command: |
| 51 | + |
| 52 | +```bash |
| 53 | +cat compose.yaml |
| 54 | +``` |
| 55 | + |
| 56 | +If you make any modifications to the `score.yaml` file, run `score-compose generate score.yaml` to regenerate the output `compose.yaml`. |
| 57 | + |
| 58 | +## `resources` |
| 59 | + |
| 60 | +Get the information of the resources dependencies of the workload, run the following command: |
| 61 | + |
| 62 | +```bash |
| 63 | +score-compose resources list |
| 64 | +``` |
| 65 | + |
| 66 | +```none |
| 67 | ++-----------------------------------+------------+ |
| 68 | +| UID | OUTPUTS | |
| 69 | ++-----------------------------------+------------+ |
| 70 | +| service.default#frontend.backend | name, url | |
| 71 | ++-----------------------------------+------------+ |
| 72 | +``` |
| 73 | + |
| 74 | +At this stage, we can already see the value of the `service` resource (the mocked backend URL) generated by Microcks: |
| 75 | + |
| 76 | +```bash |
| 77 | +score-compose resources get-outputs 'service.default#frontend.backend' --format '{{ .url }}' |
| 78 | +``` |
| 79 | + |
| 80 | +```none |
| 81 | +http://microcks:8080/rest/Order+Service+API/0.1.0 |
| 82 | +``` |
| 83 | + |
| 84 | +## `docker compose` |
| 85 | + |
| 86 | +Run `docker compose up` to execute the generated `compose.yaml` file: |
| 87 | + |
| 88 | +```bash |
| 89 | +docker compose up -d --wait |
| 90 | +``` |
| 91 | + |
| 92 | +```none |
| 93 | +[+] Running 3/3 |
| 94 | + ✔ Container score-microcks-microcks-1 Started |
| 95 | + ✔ Container score-microcks-backend-mock-1 Started |
| 96 | + ✔ Container score-microcks-frontend-frontend-1 Started |
| 97 | +``` |
| 98 | + |
| 99 | +Three containers are deployed: |
| 100 | + |
| 101 | +- **`frontend`** — The actual frontend application. |
| 102 | +- **`backend-mock`** — A `microcks-cli` sidecar that imports the OpenAPI spec into the Microcks control plane. |
| 103 | +- **`microcks`** — The Microcks control plane, which generates and serves the backend mock. |
| 104 | + |
| 105 | +## `docker ps` |
| 106 | + |
| 107 | +See the running containers: |
| 108 | + |
| 109 | +```bash |
| 110 | +docker ps |
| 111 | +``` |
| 112 | + |
| 113 | +```none |
| 114 | +CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES |
| 115 | +f8a17b908320 busybox "/bin/sh -c 'while t…" 20 seconds ago Up 16 seconds score-microcks-frontend-frontend-1 |
| 116 | +3d6e626b3d6e quay.io/microcks/microcks-uber:latest-native "/cnb/process/web" 20 seconds ago Up 19 seconds 0.0.0.0:9090->8080/tcp score-microcks-microcks-1 |
| 117 | +``` |
| 118 | + |
| 119 | +## `docker logs` |
| 120 | + |
| 121 | +Verify that the frontend app is successfully calling the Microcks-mocked backend: |
| 122 | + |
| 123 | +```bash |
| 124 | +docker logs score-microcks-frontend-frontend-1 |
| 125 | +``` |
| 126 | + |
| 127 | +```none |
| 128 | +Hello http://microcks:8080/rest/Order+Service+API/0.1.0/orders! |
| 129 | +Hello http://microcks:8080/rest/Order+Service+API/0.1.0/orders! |
| 130 | +``` |
| 131 | + |
| 132 | +The frontend successfully resolves `${resources.backend.url}` to the Microcks-mocked endpoint and calls it — without the actual backend service running anywhere. |
| 133 | + |
| 134 | +Congrats! You've successfully deployed, with the `score-compose` implementation, a containerized frontend workload whose external backend dependency is seamlessly mocked by Microcks. You provisioned everything through Docker, without writing the Docker Compose file by yourself. |
0 commit comments