|
| 1 | +--- |
| 2 | +description: Build images for services with shared definition |
| 3 | +keywords: compose, build |
| 4 | +title: Build dependent images |
| 5 | +weight: 50 |
| 6 | +--- |
| 7 | + |
| 8 | +{{< introduced compose 2.22.0 "/manuals/compose/releases/release-notes.md#2220" >}} |
| 9 | + |
| 10 | +To reduce push/pull time and image weight, a common practice for compose applications is to have services |
| 11 | +share base layers as much as possible. You will typically select the same operating system base image for |
| 12 | +all services. But you also can get one step further sharing image layers when your images share the system |
| 13 | +system packages. The challenge to address is then to avoid repeating the exact same Dockerfile instruction |
| 14 | +in all services. |
| 15 | + |
| 16 | +Foe illustration, in this article we will consider you want all your services to be built on `alpine` base |
| 17 | +image and install system package `openssl` |
| 18 | + |
| 19 | +## Multi-stage Dockerfile |
| 20 | + |
| 21 | +Recommended approach is to group shared declaration in a single Dockerfile, and use multi-stage feature |
| 22 | +so that service images are built from this shared declaration |
| 23 | + |
| 24 | + |
| 25 | +Dockerfile: |
| 26 | +``` |
| 27 | +FROM alpine as base |
| 28 | +RUN /bin/sh -c apk add --update --no-cache openssl |
| 29 | +
|
| 30 | +FROM base as service_a |
| 31 | +# build service a |
| 32 | +... |
| 33 | +
|
| 34 | +FROM base as service_b |
| 35 | +# build service b |
| 36 | +... |
| 37 | +``` |
| 38 | + |
| 39 | +compose.yaml: |
| 40 | +```yaml |
| 41 | +services: |
| 42 | + a: |
| 43 | + build: |
| 44 | + target: service_a |
| 45 | + b: |
| 46 | + build: |
| 47 | + target: service_b |
| 48 | +``` |
| 49 | +
|
| 50 | +## Use another service image as base image |
| 51 | +
|
| 52 | +A popular pattern is to reuse a service image as base image in another service. |
| 53 | +As Compose does not parse the `Dockerfile`, it can't automatically detect this dependency |
| 54 | +between services to correctly order the build execution. |
| 55 | + |
| 56 | + |
| 57 | +a.Dockerfile: |
| 58 | +``` |
| 59 | +FROM alpine |
| 60 | +RUN /bin/sh -c apk add --update --no-cache openssl |
| 61 | +``` |
| 62 | + |
| 63 | +b.Dockerfile: |
| 64 | +``` |
| 65 | +FROM service_a |
| 66 | +# build service b |
| 67 | +``` |
| 68 | + |
| 69 | +compose.yaml: |
| 70 | +```yaml |
| 71 | +services: |
| 72 | + a: |
| 73 | + image: service_a |
| 74 | + build: |
| 75 | + dockerfile: a.Dockerfile |
| 76 | + b: |
| 77 | + image: service_b |
| 78 | + build: |
| 79 | + dockerfile: b.Dockerfile |
| 80 | +``` |
| 81 | + |
| 82 | + |
| 83 | +Legacy Docker Compose v1 used to build images sequentially, which made this pattern usable |
| 84 | +out of the box. Compose v2 uses buildkit to optimise build and build images in parallel. |
| 85 | +Covering this usage requires an explicit declarartion. |
| 86 | + |
| 87 | +Recommendend approach is to declare dependent base image as an additional build context: |
| 88 | + |
| 89 | +compose.yaml |
| 90 | +```yaml |
| 91 | +services: |
| 92 | + a: |
| 93 | + image: service_a |
| 94 | + build: |
| 95 | + dockerfile: a.Dockerfile |
| 96 | + b: |
| 97 | + image: service_b |
| 98 | + build: |
| 99 | + context: b/ |
| 100 | + dockerfile: b.Dockerfile |
| 101 | + additional_contexts: |
| 102 | + # `FROM service_a` will be resolved as a dependency on service a which has to be built first |
| 103 | + service_a: "service:a" |
| 104 | +``` |
| 105 | +
|
| 106 | +
|
| 107 | +## Build with Bake |
| 108 | +
|
| 109 | +Using [bake](../../build/bake/_index.md) allows to pass the complete build definition for all service |
| 110 | +and to orchestrate build execution in the most efficient way. |
| 111 | +
|
| 112 | +To enable this feature, run compose with `COMPOSE_BAKE=true` variable set in your environment. |
| 113 | + |
| 114 | +``` |
| 115 | +$ COMPOSE_BAKE=true docker compose build |
| 116 | +[+] Building 0.0s (0/1) |
| 117 | + => [internal] load local bake definitions 0.0s |
| 118 | +... |
| 119 | +[+] Building 2/2 manifest list sha256:4bd2e88a262a02ddef525c381a5bdb08c83 0.0s |
| 120 | + ✔ service_b Built 0.7s |
| 121 | + ✔ service_a Built |
| 122 | +``` |
| 123 | +
|
| 124 | +Bake can also be selected as default builder by editing yout `$HOME/.docker/config.json` config file: |
| 125 | +```json |
| 126 | +{ |
| 127 | + ... |
| 128 | + "plugins": { |
| 129 | + "compose": { |
| 130 | + "build": "bake" |
| 131 | + } |
| 132 | + } |
| 133 | + ... |
| 134 | +} |
| 135 | +``` |
0 commit comments