|
| 1 | +--- |
| 2 | +description: Overview of Docker Compose Bridge |
| 3 | +keywords: compose, orchestration, kubernetes, bridge |
| 4 | +title: Overview of Docker Compose Bridge |
| 5 | +--- |
| 6 | + |
| 7 | +{{< include "compose-bridge-early-access.md" >}} |
| 8 | + |
| 9 | +## Introduction |
| 10 | + |
| 11 | +Docker Compose makes it easy to define a multi-container application |
| 12 | +to be run on a single-node Docker Engine, relying on a `compose.yaml` file to |
| 13 | +describe resources with a simple abstraction. |
| 14 | + |
| 15 | +Compose Bridge lets you reuse this exact same `compose.yaml` file but |
| 16 | +translate it into another platform's definition format, with a primary |
| 17 | +focus on Kubernetes. This transformation can be customized to match |
| 18 | +specific needs and requirements. |
| 19 | + |
| 20 | +## Usage |
| 21 | + |
| 22 | +Compose Bridge is a command line tool that consumes a `compose.yaml` file |
| 23 | +and runs a transformation to produce resource definitions for another platform. |
| 24 | +[By default](transformation.md), it produces Kubernetes manifests and a Kustomize overlay for Docker Desktop. For example: |
| 25 | +```console |
| 26 | +$ compose-bridge -f compose.yaml convert |
| 27 | +Kubernetes resource api-deployment.yaml created |
| 28 | +Kubernetes resource db-deployment.yaml created |
| 29 | +Kubernetes resource web-deployment.yaml created |
| 30 | +Kubernetes resource api-expose.yaml created |
| 31 | +Kubernetes resource db-expose.yaml created |
| 32 | +Kubernetes resource web-expose.yaml created |
| 33 | +Kubernetes resource 0-avatars-namespace.yaml created |
| 34 | +Kubernetes resource default-network-policy.yaml created |
| 35 | +Kubernetes resource private-network-policy.yaml created |
| 36 | +Kubernetes resource public-network-policy.yaml created |
| 37 | +Kubernetes resource db-db_data-persistentVolumeClaim.yaml created |
| 38 | +Kubernetes resource api-service.yaml created |
| 39 | +Kubernetes resource web-service.yaml created |
| 40 | +Kubernetes resource kustomization.yaml created |
| 41 | +Kubernetes resource db-db_data-persistentVolumeClaim.yaml created |
| 42 | +Kubernetes resource api-service.yaml created |
| 43 | +Kubernetes resource web-service.yaml created |
| 44 | +Kubernetes resource kustomization.yaml created |
| 45 | +``` |
| 46 | + |
| 47 | +Such manifests can then be used to run the application on Kubernetes using |
| 48 | +the standard deployment command `kubectl apply -k out/overlays/desktop/`. |
| 49 | + |
| 50 | +## Customization |
| 51 | + |
| 52 | +The Kubernetes manifests produced by Compose Bridge |
| 53 | +are designed to allow deployment on Docker Desktop with Kubernetes enabled. |
| 54 | + |
| 55 | +Kubernetes is such a versatile platform that there are many ways |
| 56 | +to map Compose concepts into a Kubernetes resource definitions. Compose |
| 57 | +Bridge lets you customize the transformation to match your own infrastructure |
| 58 | +decisions and preferences, with various level of flexibility / investment. |
| 59 | + |
| 60 | + |
| 61 | +### Modify the default templates |
| 62 | + |
| 63 | +You can extract templates used by default transformation `docker/compose-bridge-kubernetes` |
| 64 | +by running `compose-bridge transformations create my-template --from docker/compose-bridge-kubernetes` |
| 65 | +and adjusting those to match your needs. |
| 66 | + |
| 67 | +The templates will be extracted into a directory named after your template name (ie `my-template`). |
| 68 | +Inside, you will find a Dockerfile that allows you to create your own image to distribute your template, as well as a directory containing the templating files. |
| 69 | +You are free to edit the existing files, delete them, or [add new ones](#add-your-own-templates) to subsequently generate Kubernetes manifests that meet your needs. |
| 70 | +You can then use the generated Dockerfile to package your changes into a new Transformer image, which you can then use with Compose Bridge: |
| 71 | + |
| 72 | +```console |
| 73 | +$ docker build --tag mycompany/transform --push . |
| 74 | +``` |
| 75 | + |
| 76 | +You can then use your transformation as a replacement: |
| 77 | +```console |
| 78 | +$ compose-bridge -f compose.yaml convert --transformation mycompany/transform |
| 79 | +``` |
| 80 | + |
| 81 | +For more information, see [Templates](./templates.md). |
| 82 | + |
| 83 | +### Add your own templates |
| 84 | + |
| 85 | +For resources that are not managed by Compose Bridge's default transformation, |
| 86 | +you can build your own templates. The `compose.yaml` model may not offer all |
| 87 | +the configuration attributes required to populate the target manifest. If this is the case, you can |
| 88 | +then rely on Compose custom extensions to let developers better describe the |
| 89 | +application, and offer an agnostic transformation. |
| 90 | + |
| 91 | +As an illustration, if developers add `x-virtual-host` metadata |
| 92 | +to service definitions in the `compose.yaml` file, you can use the following custom attribute |
| 93 | +to produce Ingress rules: |
| 94 | + |
| 95 | +```yaml |
| 96 | +{{ $project := .name }} |
| 97 | +#! {{ $name }}-ingress.yaml |
| 98 | +# Generated code, do not edit |
| 99 | +apiVersion: networking.k8s.io/v1 |
| 100 | +kind: Ingress |
| 101 | +metadata: |
| 102 | + name: virtual-host-ingress |
| 103 | + namespace: {{ $project }} |
| 104 | +spec: |
| 105 | + rules: |
| 106 | +{{ range $name, $service := .services }} |
| 107 | +{{ if $service.x-virtual-host }} |
| 108 | + - host: ${{ $service.x-virtual-host }} |
| 109 | + http: |
| 110 | + paths: |
| 111 | + - path: "/" |
| 112 | + backend: |
| 113 | + service: |
| 114 | + name: ${{ name }} |
| 115 | + port: |
| 116 | + number: 80 |
| 117 | +{{ end }} |
| 118 | +{{ end }} |
| 119 | +``` |
| 120 | + |
| 121 | +Once packaged into a Docker image, you can use this custom template |
| 122 | +when transforming Compose models into Kubernetes in addition to other |
| 123 | +transformations: |
| 124 | + |
| 125 | +```console |
| 126 | +$ compose-bridge -f compose.yaml convert \ |
| 127 | + --transformation docker/compose-bridge-kubernetes \ |
| 128 | + --transformation mycompany/transform |
| 129 | +``` |
| 130 | + |
| 131 | +### Build your own transformation |
| 132 | + |
| 133 | +While Compose Bridge templates make it easy to customize with minimal changes, |
| 134 | +you may want to make significant changes, or rely on an existing conversion tool. |
| 135 | + |
| 136 | +A Compose Bridge transformation is a Docker image that is designed to get a Compose model |
| 137 | +from `/in/compose.yaml` and produce platform manifests under `/out`. This simple |
| 138 | +contract makes it easy to bundle an alternate transformation, as illustrated below using |
| 139 | +[Kompose](https://kompose.io/): |
| 140 | + |
| 141 | +```Dockerfile |
| 142 | +FROM alpine |
| 143 | + |
| 144 | +# Get kompose from github release page |
| 145 | +RUN apk add --no-cache curl |
| 146 | +ARG VERSION=1.32.0 |
| 147 | +RUN ARCH=$(uname -m | sed 's/armv7l/arm/g' | sed 's/aarch64/arm64/g' | sed 's/x86_64/amd64/g') && \ |
| 148 | + curl -fsL \ |
| 149 | + "https://github.com/kubernetes/kompose/releases/download/v${VERSION}/kompose-linux-${ARCH}" \ |
| 150 | + -o /usr/bin/kompose |
| 151 | +RUN chmod +x /usr/bin/kompose |
| 152 | + |
| 153 | +CMD ["/usr/bin/kompose", "convert", "-f", "/in/compose.yaml", "--out", "/out"] |
| 154 | +``` |
| 155 | + |
| 156 | +This Dockerfile bundles Kompose and defines the command to run this tool according |
| 157 | +to the Compose Bridge transformation contract. |
| 158 | + |
| 159 | +## Use `compose-bridge` as a `kubectl` plugin |
| 160 | + |
| 161 | +To use the `compose-bridge` binary as a `kubectl` plugin, you need to make sure that the binary is available in your PATH and the name of the binary is prefixed with `kubectl-`. |
| 162 | + |
| 163 | +1. Rename or copy the `compose-bridge` binary to `kubectl-compose_bridge`: |
| 164 | + |
| 165 | + ```console |
| 166 | + $ mv /path/to/compose-bridge /usr/local/bin/kubectl-compose_bridge |
| 167 | + ``` |
| 168 | + |
| 169 | +2. Ensure that the binary is executable: |
| 170 | + |
| 171 | + ```console |
| 172 | + $ chmod +x /usr/local/bin/kubectl-compose_bridge |
| 173 | + ``` |
| 174 | + |
| 175 | +3. Verify that the plugin is recognized by `kubectl`: |
| 176 | + |
| 177 | + ```console |
| 178 | + $ kubectl plugin list |
| 179 | + ``` |
| 180 | + |
| 181 | + In the output, you should see `kubectl-compose_bridge`. |
| 182 | + |
| 183 | +4. Now you can use `compose-bridge` as a `kubectl` plugin: |
| 184 | + |
| 185 | + ```console |
| 186 | + $ kubectl compose-bridge [command] |
| 187 | + ``` |
| 188 | + |
| 189 | +Replace `[command]` with any `compose-bridge` command you want to use. |
0 commit comments