I have a CI script that builds Dockerfiles. My plan is that unit tests should be run in a `test` stage in each Dockerfile, for example:

```
FROM alpine AS build
WORKDIR /app
COPY src .
...

FROM build AS test
RUN mvn clean test

FROM build AS package
COPY --from=build ...

```

So, for a given Dockerfile, I would like to check if it has a `test` stage and, if so, run `docker build --target test ...`. If it doesn't have a `test` stage, I don't want to run `docker build` (which would fail).

**How can I check if a Dockerfile contains a certain stage without actually building it?**

I do realize this question has some [XY problem](http://xyproblem.info/) vibes to it, so feel free to enlighten me. But I also think the question can be generally useful anyway.