How to force docker-compose to wait for one build to complete before building another service when using multi-stage builds?

Viewed 35

I've got this structure:

my-app/
├─ my-submodule/
│  ├─ .git
│  ├─ Dockerfile
│  ├─ ...
├─ .git/
├─ .gitmodules
├─ docker-compose.yml
├─ Dockerfile
├─ .dockerignore
├─ ...

my-app includes a submodule that needs to be compiled before building the main app.

I would like to use docker-compose to build my-app, it would need to first build my-submodule using its Dockerfile, then build the main app.

I'm using multi-stage builds:

submodule/Dockerfile:

FROM ubuntu AS my-submodule
WORKDIR /my-submodule
COPY . 
# ...

./Dockerfile:

FROM my-submodule AS my-app
WORKDIR /my-app
COPY . .
# ...

./docker-compose.yml:

version: "3.9"

services:
  my-submodule:
    image: my-submodule
    build:
      context: ./my-submodule
      target: my-submodule
  my-app:
    build:
      context: .
      dockerfile: Dockerfile
      target: my-app
    image: my-app
    depends_on:
      - my-submodule

My .dockerignore ignores my-submodule, because its content should already be copied when building my-submodule, so there's no need to copy it again when building my-app.

Currently, when running docker-compose build for the first time, I'm getting

=> ERROR [my-app internal] load metadata for docker.io/library/my-submodule:latest 0.4s

...

failed to solve: rpc error: code = Unknown desc = failed to solve with frontend dockerfile.v0: failed to create LLB definition: pull access denied, repository does not exist or may require authorization: server message: insufficient_scope: authorization failed

The next time I run it, it builds fine because my-submodule has been built.

How can I prevent docker-compose from building my-app until my-submodule's image exists?

0 Answers
Related