frans
frans

Reputation: 9748

With `docker build` is it possible to get both the build log and the ID of the generated image on stdout/stderr?

I know docker build ... writes all the log from the builder out to command line while docker build --quiet .. doesn't but prints the resulting image ID instead.

Both is very important IMHO, can I get both?

There is also --idfile which can supply but forces me to provide and clean up a file name.

Is there also a way to get both, the image ID and the build log just via stdout/err instead?

Docker version is 25.0.3 with BuildKit enabled

Upvotes: 1

Views: 165

Answers (3)

marc.guenther
marc.guenther

Reputation: 2958

I'm using this ridiculous incantation:

img_file=$(mktemp)
trap 'rm -f "$img_file"' 0
docker build --iidfile "$img_file" . && docker run $(cat "$img_file") ...

This will create a temporary file to store the image-id and set's up a trap to delete the file on exit of the process (overwriting any previous traps you might have set).

It will then build the image, and start a container from the image.

Upvotes: 0

frans
frans

Reputation: 9748

This is how I do it today and it works for me

create-image-id.sh

#!/usr/bin/env bash

TEMP_CONTEXT_DIR=$(mktemp -d)
trap 'rm -rf "${TEMP_CONTEXT_DIR}"' EXIT
IIDFILE="${TEMP_CONTEXT_DIR}/iidfile.txt"

docker buildx build \
    --iidfile "${IIDFILE}" \
    -f "<path/to/Dockerfile>" \
    "${TEMP_CONTEXT_DIR}" \
    1>&2

cat "${IIDFILE}"

1>&2 is important, without it stdout will contain clutter

Upvotes: 0

shunmuga prakash
shunmuga prakash

Reputation: 91

Try pipe approach

docker build . | tee build.log | awk '/Successfully built/{print $NF}'

Upvotes: 1

Related Questions