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