How to view logs for a docker image?

Update: Since this question has been asked, it seems everyone is finding it after seeing the output changes from buildkit. Buildkit includes the following options (docker build --help to see them all):

      --build-arg list          Set build-time variables
      --cache-from strings      Images to consider as cache sources
  -f, --file string             Name of the Dockerfile (Default is 'PATH/Dockerfile')
      --no-cache                Do not use cache when building the image
  -o, --output stringArray      Output destination (format: type=local,dest=path)
      --platform string         Set platform if server is multi-platform capable
      --progress string         Set type of progress output (auto, plain, tty). Use plain to show container output (default "auto")
      --pull                    Always attempt to pull a newer version of the image
  -q, --quiet                   Suppress the build output and print image ID on success
  -t, --tag list                Name and optionally a tag in the 'name:tag' format
      --target string           Set the target build stage to build.

The option many want with buildkit is --progress=plain:

docker build -t my-image --progress=plain .

If you really want to see the previous build output, you can disable buildkit with an environment variable, but I tend to recommend against this since there are a lot of features from buildkit you'd lose (skipping unused build steps, concurrent build steps, multi-platform images, and new syntaxes for the Dockerfile for features like RUN --mount...):

DOCKER_BUILDKIT=0 docker build -t my-image .

The OP is asking to include the logs of their build within the image itself. Generally I would recommend against this, you'd want those logs outside of the image.

That said, the easiest method for that is to use tee to send a copy of all your command output to a logfile. If you want it attached to the image, output your run commands to a logfile inside of the image with something like:

RUN my-install-cmd | tee /logs/my-install-cmd.log

Then you can run a quick one-off container to view the contents of those logs:

docker run --rm my-image cat /logs/my-install-cmd.log

If you don't need the logs attached to the image, you can log the output of every build with a single change to your build command (instead of lots of changes to the run commands) exactly as JHarris says:

docker build -t my-image . | tee my-image.build.log

With the classic docker build command, if you build without using --rm=true, then you have all the intermediate containers, and each one of those has a log you can review with

docker logs $container_id

And lastly, don't forget there's a history of the layers in the image. They don't show the output of each command, but it is useful for all of those commands that don't log any output and knowing which build each layer comes from particularly when there's lots of caching being used.

docker history my-image

Had the same problem, I solved it using

docker build --no-cache --progress=plain -t my-image .