Vibe monitoring with Last9 MCP: Ask your agent to fix production issues! Setup →
Last9 Last9

Apr 16th, ‘25 / 11 min read

A Closer Look at Docker Build Logs for Troubleshooting

Understand Docker build logs to troubleshoot errors, optimize builds, and keep your containers running smoothly.

A Closer Look at Docker Build Logs for Troubleshooting

In the world of containerization, understanding what's happening under the hood during image builds can mean the difference between smooth deployments and frustrating debugging sessions.

Docker build logs are your window into this process, offering crucial insights that help you optimize builds, troubleshoot errors, and maintain robust container infrastructure.

This guide talks about everything DevOps engineers and SREs need to know about Docker build full logs - from capturing complete log output to using that information for troubleshooting and optimization.

What Are Docker Build Logs

Docker build logs are the complete output generated during the image-building process using the docker build command. These logs capture every step of the build process, including layer creation, command execution, and any errors or warnings that occur along the way.

For DevOps and SREs, these logs aren't just optional reading - they're essential operational intelligence that:

  • Provide transparency into the build process
  • Help identify optimization opportunities
  • Serve as the first line of troubleshooting when builds fail
  • Document the exact steps taken to create an image
  • Allow for reproducibility and consistency across environments

Without full access to build logs, you're essentially flying blind when containers misbehave or build mysteriously fail.

💡
If you're looking to pause Docker containers without stopping them completely, this guide explains how to do it efficiently: Pausing Docker Containers.

How to Capture Complete Docker Build Logs

Standard Output Methods and Their Limitations

By default, Docker displays build logs directly in your terminal. While convenient, this approach has several limitations:

  • Logs disappear after the terminal session ends
  • Output can be truncated for large builds
  • Difficult to search through or analyze programmatically
  • Challenging to share with team members for collaborative troubleshooting

These limitations become particularly problematic in CI/CD pipelines or when working with complex, multi-stage builds.

Capturing the Full Build Output

To overcome these limitations and capture the complete Docker build full log, you have several options:

Method 1: Redirect to a file

docker build -t your-image:tag . > build.log 2>&1

This command redirects both standard output and error streams to a file named build.log.

Method 2: Use the --progress=plain flag

docker build --progress=plain -t your-image:tag . > build.log 2>&1

The --progress=plain flag provides more detailed output, especially useful when building with BuildKit enabled.

Method 3: Use BuildKit's enhanced logging

DOCKER_BUILDKIT=1 docker build --progress=plain -t your-image:tag . > build.log 2>&1

BuildKit (Docker's next-generation builder) offers improved logging capabilities with more detailed information about each build stage.

💡
If you need to clear Docker logs to free up space or manage your system more effectively, here's how to do it: Clearing Docker Logs.

How to Setup Persistent Logging for Docker Builds

For teams that need systematic logging of all Docker builds, setting up persistent logging is crucial. This can be accomplished by:

  1. Creating a simple wrapper script around the docker build command
  2. Configuring your CI/CD platform to automatically capture and store build logs
  3. Implementing a centralized logging solution that captures Docker engine output

Here's a simple bash wrapper script example:

#!/bin/bash
LOG_DIR="/var/log/docker-builds"
mkdir -p $LOG_DIR
BUILD_ID=$(date +%Y%m%d_%H%M%S)
LOG_FILE="$LOG_DIR/build_$BUILD_ID.log"

echo "Starting Docker build, logging to $LOG_FILE"
DOCKER_BUILDKIT=1 docker build --progress=plain "$@" | tee $LOG_FILE

Understanding Docker Build Log Structure

Docker build logs follow a specific structure that, once understood, makes them much easier to analyze and extract value from.

Anatomy of a Build Log

A typical Docker build full log contains:

  1. Context preparation messages - Information about files being sent to the Docker daemon
  2. Step indicators - Each Dockerfile instruction generates a "Step X/Y" entry
  3. Layer IDs - Unique identifiers for each layer created
  4. Command output - The actual output of commands run during the build
  5. Cache information - Messages about cache hits or misses
  6. Pull progress - Details when pulling base images
  7. Build summary - Final status and image ID information

Key Sections to Focus On

When analyzing build logs, these sections deserve special attention:

Context Upload: Large context sizes can slow builds significantly

Sending build context to Docker daemon  254.8MB

Cache Information: Identifies opportunities for build optimization

Step 3/15 : COPY package*.json ./
 ---> Using cache
 ---> 7cb3bd8443e5

Command Output: Reveals issues with scripts or commands running during the build

Step 8/15 : RUN npm install
 ---> Running in f8d3e69c9743
npm WARN deprecated fsevents@1.2.9: The platform "linux" is incompatible with this module.
npm WARN deprecated fsevents@1.2.9: The platform "linux" is incompatible with this module.

Error Messages: Critical for troubleshooting failed builds

The command '/bin/sh -c apt-get update && apt-get install -y python3' returned a non-zero code: 100

Differences Between Regular and BuildKit Logs

BuildKit introduces significant changes to the logging format:

Regular Docker Builder BuildKit
Sequential step output Concurrent step output
Limited progress info Detailed progress bars
Basic error reporting Enhanced error context
No mount information Detailed mount reporting
Limited caching details Expanded caching information

Understanding these differences is important when switching between the two builders or when troubleshooting in different environments.

💡
If you’re using Docker Compose and want to ensure your containers are healthy, check out this guide on setting up health checks: Docker Compose Health Checks.

Common Docker Build Log Error Patterns and Solutions

Docker build logs often contain specific patterns that indicate common problems. Learning to recognize these can dramatically speed up troubleshooting.

Error Pattern:

Failed to fetch http://archive.ubuntu.com/ubuntu/pool/main/... 
Could not resolve 'archive.ubuntu.com'

Solution: Check your network connectivity and proxy settings. For builds in restricted environments, consider using the --network=host flag or setting up a proper registry mirror.

Permission Issues

Error Pattern:

permission denied while trying to connect to the Docker daemon socket

Solution: Ensure your user is part of the docker group or use sudo. For files inside the container, check the permissions in your Dockerfile and consider using chmod when necessary.

Resource Limitations

Error Pattern:

failed to allocate memory: out of memory

Solution: Increase the memory allocated to Docker in the daemon settings. For multi-stage builds, ensure you're not carrying unnecessary dependencies between stages.

Missing Dependencies

Error Pattern:

Package 'python-dev' has no installation candidate

Solution: Ensure your base image is up-to-date and package repositories are correctly configured. Consider using a more specific base image version to avoid dependency issues.

Corrupted or Missing Cache

Error Pattern:

unexpected EOF
failed to get layer sha256:1234...

Solution: Clear your Docker build cache using docker builder prune and rebuild. For persistent issues, check disk space and file system health.

How to Optimize Docker Builds Using Log Insights

One of the most valuable aspects of Docker build full logs is the insights they provide for optimization opportunities.

Identifying Slow Build Steps

By analyzing timestamps in your build logs, you can identify which steps take the most time:

grep "Step" build.log | awk '{print $1, $2, $NF}' > step_times.txt

Focus optimization efforts on the slowest steps first for maximum impact.

Using Build Cache Effectively

Build logs reveal which steps are using the cache and which are forcing rebuilds:

Step 4/15 : COPY . /app
 ---> CACHED

vs.

Step 4/15 : COPY . /app
 ---> Running in 3f5a2d7c9b4d

Optimize your Dockerfile to place frequently changing files later in the build to maximize cache usage. For example:

Before optimization:

FROM node:14
WORKDIR /app
COPY . /app
RUN npm install
CMD ["npm", "start"]

After optimization:

FROM node:14
WORKDIR /app
COPY package*.json /app/
RUN npm install
COPY . /app
CMD ["npm", "start"]

Multi-Stage Build Optimization

Build logs can help identify opportunities for multi-stage builds:

Step 10/15 : RUN npm run build
 ---> Running in 3f5a2d7c9b4d
...
Step 15/15 : CMD ["node", "server.js"]

Converting to a multi-stage build can significantly reduce image size:

FROM node:14 AS builder
WORKDIR /app
COPY . /app
RUN npm install && npm run build

FROM node:14-slim
WORKDIR /app
COPY --from=builder /app/dist /app/dist
COPY --from=builder /app/node_modules /app/node_modules
COPY --from=builder /app/package.json /app/
CMD ["node", "server.js"]

Resource Allocation Based on Log Analysis

Build logs often reveal resource bottlenecks:

npm install took 125.7s

This might indicate that increasing CPU allocation could speed up builds substantially. Adjust your Docker desktop resource settings or CI/CD runner configurations accordingly.

💡
If you're looking to filter Docker logs with grep for easier troubleshooting, this guide walks you through the process: Filtering Docker Logs with Grep.

Integrating Docker Build Logs with Observability Systems

Individual build logs are useful, but integrated observability unlocks their full potential.

Centralized Logging Solutions for Docker Builds

Setting up centralized logging for Docker builds enables:

  • Historical analysis of build performance
  • Team-wide access to build information
  • Correlation with deployment and runtime issues
  • Automated anomaly detection

For optimal results, consider sending your Docker build full logs to a dedicated observability platform.

How to Stream Docker Build Logs to External Systems

To stream Docker build logs to external systems:

  1. Use a logging driver configured in your Docker daemon settings
  2. Implement a log forwarding agent like Fluentd or Logstash
  3. Stream logs directly from CI/CD systems to your observability platform

Here's a simple example using jq to parse and forward logs:

docker build -t myapp:latest . | jq -R 'fromjson? // {log: .}' | curl -s -XPOST -d @- https://your-log-endpoint

Correlating Build Logs with Runtime Incidents

The real power of Docker build logs comes when you can correlate build changes with runtime behavior. To enable this:

  1. Tag images with build IDs that can be traced back to specific logs
  2. Include build metadata as labels in your Docker images
  3. Ensure your observability system can trace from container instances back to the builds that created them

This correlation capability helps quickly identify whether a production issue stems from a build change or a runtime environment issue.

💡
If you want to integrate Grafana with Docker for better monitoring, this guide covers everything you need to get started: Grafana and Docker.

Advanced Docker Build Log Analysis Techniques

For teams dealing with complex containerization workflows, advanced log analysis techniques can provide deeper insights.

Using Log Parsing Tools to Extract Metrics

Several tools can help extract meaningful metrics from Docker build logs:

  • buildkit-mon: Visualizes BuildKit operations in real-time
  • docker-build-timer: Extracts and displays time spent on each build step
  • jq: Powerful command-line JSON processor that works well with structured logs

Example of extracting layer sizes using jq:

docker history --no-trunc myimage:latest --format '{{json .}}' | jq -r '[.CreatedBy, .Size] | @tsv'

Creating Build Performance Dashboards

For teams that build containers frequently, creating dedicated dashboards for build performance can surface trends and issues:

Metric Why It Matters Source in Logs
Total build time Overall efficiency Start/end timestamps
Cache hit ratio Build optimization Cache hit/miss messages
Layer sizes Image optimization Layer creation messages
Error frequency Build stability Error messages
Context size Build speed Context upload message

Automated Log Analysis for CI/CD Pipelines

Implement automated analysis of build logs in your CI/CD pipeline to:

  • Fail builds that exceed size or time thresholds
  • Identify security issues in dependencies
  • Flag suspicious patterns that might indicate compromised base images
  • Monitor build performance trends over time

Here's a simple script that could run post-build:

#!/bin/bash
BUILD_LOG="build.log"

# Check for critical errors
if grep -q "returned a non-zero code" "$BUILD_LOG"; then
  echo "Build failed with non-zero exit code"
  exit 1
fi

# Check build time
BUILD_TIME=$(grep "Successfully built" "$BUILD_LOG" | awk '{print $NF}' | sed 's/s//')
if [[ $BUILD_TIME -gt 300 ]]; then
  echo "Warning: Build time exceeded 5 minutes"
fi

# Check for suspicious activities
if grep -q "curl -s" "$BUILD_LOG"; then
  echo "Warning: Build contains curl commands that might download unverified content"
fi
💡
If you need to tail Docker logs for real-time monitoring or troubleshooting, this guide shows you how: Docker Logs Tail Guide.

Best Practices for Docker Build Logging in Production Environments

For production-grade containerization workflows, following these best practices ensures you get the most value from your build logs.

Security Considerations When Logging Builds

Docker build logs can contain sensitive information:

  • API keys and tokens
  • Internal network details
  • Security-related configurations
  • Authentication information

To mitigate risks:

  1. Redact sensitive information: Use build args for secrets rather than hardcoding them
  2. Implement log access controls: Restrict who can view complete build logs
  3. Audit build log access: Track who is viewing build information
  4. Rotate any credentials exposed in logs: If secrets do get logged, rotate them immediately

Log Retention Policies for Compliance and Debugging

Consider:

  • Regulatory requirements for log retention in your industry
  • Storage costs versus troubleshooting needs
  • Implementing tiered retention (full logs for 30 days, summarized data for 1 year)
  • Automating log archiving to cold storage for longer-term needs

Integrating with Last9 for Comprehensive Docker Build Monitoring

For teams seeking advanced observability, Last9 offers several advantages for Docker build monitoring:

  • High-cardinality observability supports detailed build analysis
  • OpenTelemetry and Prometheus integration simplifies setup
  • Correlation capabilities connect build issues to runtime problems

Integrating Docker build logs with Last9 creates a unified view of your containerization pipeline from build to runtime, enabling faster troubleshooting and more effective optimization.

💡
Now, fix production Docker log issues instantly—right from your IDE, with AI and Last9 MCP.

Troubleshooting Common Docker Build Log Issues in CI/CD Environments

CI/CD environments present unique challenges for Docker build logging.

Handling Truncated Logs in CI Systems

Many CI systems limit log output size, which can result in truncated Docker build logs. To address this:

  1. Use the --progress=plain flag to produce more compact logs
  2. Implement log streaming to external storage
  3. Configure your CI system to increase log size limits
  4. Focus on capturing errors and warnings rather than full output

Ensuring Build Reproducibility Through Log Analysis

Logs should help guarantee build reproducibility. Watch for:

  • Non-deterministic behavior in builds
  • Inconsistent cache usage
  • Base image changes
  • Network-dependent operations that might vary

When these issues appear in logs, consider:

# Add explicit versioning
FROM ubuntu:20.04 
# Pin package versions
RUN apt-get update && apt-get install -y python3=3.8.5-1ubuntu1
# Add verification steps
RUN python3 --version | grep "3.8.5" || exit 1

Debugging Failed Builds in Automated Environments

In CI/CD pipelines, you often don't have interactive access to debug. Use logs effectively by:

  1. Enabling verbose mode with --progress=plain

Using BuildKit's enhanced error reporting:

DOCKER_BUILDKIT=1 docker build .

Implementing retry logic with different flags:

docker build . || docker build --no-cache .

Adding deliberate debug steps in your Dockerfile:

RUN ls -la /app && env && df -h

Using Docker Build Logs for Continuous Improvement

Beyond troubleshooting, build logs provide a foundation for ongoing improvement of your containerization practices.

Establishing Build Performance Baselines

Use logs to establish baseline metrics for your builds:

  1. Record build times for standard images
  2. Document typical layer sizes
  3. Establish normal cache hit rates
  4. Identify standard resource utilization patterns

With these baselines, you can quickly spot regressions and improvements.

Implementing Automated Build Audits

Set up automated audits of build logs to enforce standards:

  • Flag builds exceeding size thresholds
  • Identify insecure practices (running as root, using latest tags)
  • Detect inefficient patterns (cache invalidation, large layer sizes)
  • Monitor build time trends

These audits can be integrated into code review processes to prevent issues before they reach production.

Creating Feedback Loops for Developer Education

Use insights from build logs to educate team members:

  1. Share anonymized examples of efficient vs. inefficient Dockerfiles
  2. Create documentation highlighting patterns identified in logs
  3. Implement pre-commit hooks that suggest improvements based on common issues
  4. Conduct regular reviews of build performance metrics

This education process helps spread containerization best practices throughout your organization.

💡
If you're exploring options beyond Docker, check out this guide on the top 10 Docker alternatives: Top 10 Docker Alternatives.

Wrapping Up

Have more questions about Docker build logs or containerization best practices? Join our Discord community where DevOps engineers and SREs share knowledge and experiences. We'd love to hear about your Docker optimization successes!

FAQs

How do I capture the full Docker build log without truncation?

To capture the complete Docker build log without truncation, use:

docker build --progress=plain -t your-image:tag . > build.log 2>&1

The --progress=plain flag ensures detailed output while redirecting both standard output and errors to a file prevents any terminal-based truncation.

Why are my Docker build logs showing "Using cache" when I expected a rebuild?

This typically happens because Docker hasn't detected changes that would invalidate the cache. The most common reasons include:

  1. Your changed files aren't included in the build context
  2. The changes occur after the cached layer in the Dockerfile
  3. You're building with a different context path that doesn't include your changes

To force a complete rebuild, use the --no-cache flag:

docker build --no-cache -t your-image:tag .

How can I identify which step is causing my Docker build to fail?

Look for these indicators in your build log:

  1. The last successful step message ("Step X/Y: ...")
  2. Any error message containing "returned a non-zero code"
  3. Specific error details immediately preceding the build failure

For more detailed debugging, use BuildKit:

DOCKER_BUILDKIT=1 docker build --progress=plain -t your-image:tag .

BuildKit provides enhanced error reporting with more context about failures.

What's the difference between docker build logs and docker logs?

Docker build logs show the image creation process, while docker logs show the runtime output from a running container:

  • Docker build logs: Created during docker build, show each step of constructing the image
  • Docker logs: Generated by applications running inside containers, accessed via docker logs [container]

Both are important for different phases of the container lifecycle, but they capture fundamentally different information.

How do I interpret "WARNING: apt does not have a stable CLI interface" in my build logs?

This warning appears in many Debian/Ubuntu-based Docker builds and is generally safe to ignore. It indicates that the apt command-line interface might change in future releases.

If you want to eliminate this warning from your logs for clarity, use the apt-get command instead of apt in your Dockerfiles:

# Instead of:
RUN apt update && apt install -y python3

# Use:
RUN apt-get update && apt-get install -y python3

Contents


Newsletter

Stay updated on the latest from Last9.

Topics