Custom Build Output

I needed to build some binaries inside Dockerfile and copy the artifacts built inside the image to the host machine.

After some digging in, found buildx supports Custom Build Outputs

Here's an example workflow on how to achieve the same:

FROM ubuntu:22.04 AS base

RUN mkdir -p /data
RUN touch /data/hello.txt

# Export the data to host using buildx
FROM scratch AS export
COPY --from=base /data .
docker buildx build --file Dockerfile --output data .

After the above command builds the image, you can see ./data got created in our host machine.

├── Dockerfile
└── data
    └── hello.txt