NeuroAgent

Docker Build No Cache: Complete Guide to Clean Builds

Learn how to force Docker to perform clean builds without cache. Complete guide to using --no-cache flag, troubleshooting cache issues, and ensuring proper package installation in Docker images.

Question

How to force Docker to perform a clean build without using the cache?

I’m building a Docker image using the following command:

docker build -t u12_core -f u12_core .

When I rebuild the image with the same command, Docker uses the build cache, as shown in the output below:

Step 1 : FROM ubuntu:12.04
 ---> eb965dfb09d2
Step 2 : MAINTAINER Pavan Gupta <pavan.gupta@gmail.com>
 ---> Using cache
 ---> 4354ccf9dcd8
Step 3 : RUN apt-get update
 ---> Using cache
 ---> bcbca2fcf204
Step 4 : RUN apt-get install -y openjdk-7-jdk
 ---> Using cache
 ---> 103f1a261d44
Step 5 : RUN apt-get install -y openssh-server
 ---> Using cache
 ---> dde41f8d0904
Step 6 : RUN apt-get install -y git-core
 ---> Using cache
 ---> 9be002f08b6a
Step 7 : RUN apt-get install -y build-essential
 ---> Using cache
 ---> a752fd73a698
Step 8 : RUN apt-get install -y logrotate
 ---> Using cache
 ---> 93bca09b509d
Step 9 : RUN apt-get install -y lsb-release
 ---> Using cache
 ---> fd4d10cf18bc
Step 10 : RUN mkdir /var/run/sshd
 ---> Using cache
 ---> 63b4ecc39ff0
Step 11 : RUN echo 'root:root' | chpasswd
 ---> Using cache
 ---> 9532e31518a6
Step 12 : RUN sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config
 ---> Using cache
 ---> 47d1660bd544
Step 13 : RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd
 ---> Using cache
 ---> d1f97f1c52f7
Step 14 : RUN wget -O aerospike.tgz 'http://aerospike.com/download/server/latest/artifact/ubuntu12'
 ---> Using cache
 ---> bd7dde7a98b9
Step 15 : RUN tar -xvf aerospike.tgz
 ---> Using cache
 ---> 54adaa09921f
Step 16 : RUN dpkg -i aerospike-server-community-*/*.deb
 ---> Using cache
 ---> 11aba013eea5
Step 17 : EXPOSE 22 3000 3001 3002 3003
 ---> Using cache
 ---> e33aaa78a931
Step 18 : CMD /usr/sbin/sshd -D
 ---> Using cache
 ---> 25f5fe70fa84
Successfully built 25f5fe70fa84

The cache indicates that aerospike is installed, but I can’t find it inside containers spawned from this image. How can I force Docker to rebuild the image completely without using the cache?

NeuroAgent

To force Docker to perform a completely clean build without using the cache, you can use the --no-cache flag. Simply add this flag to your existing command:

bash
docker build --no-cache -t u12_core -f u12_core .

This will force Docker to rebuild every layer from scratch, ignoring any cached layers, which should resolve your issue with aerospike not being properly installed despite the cache indicating it was.


Contents


Using the --no-cache Flag

The most straightforward way to force a clean build is by using the --no-cache flag with your docker build command:

bash
docker build --no-cache -t u12_core -f u12_core .

This flag tells Docker to ignore the cache completely and rebuild every layer from scratch. When you use this flag, you’ll see no “Using cache” messages in the build output - every step will be executed as if it’s the first time building.

Important considerations:

  • Build time: Clean builds take significantly longer as Docker must execute every command in your Dockerfile
  • Network usage: Each RUN command that downloads packages (like apt-get update) will re-download everything
  • Storage: Build context and intermediate images will use more temporary storage

Here’s how the output would differ with --no-cache:

Step 1/18 : FROM ubuntu:12.04
 ---> eb965dfb09d2
Step 2/18 : MAINTAINER Pavan Gupta <pavan.gupta@gmail.com>
 ---> Running in a5f8c3d2e1f4
 ---> 4354ccf9dcd8
Step 3/18 : RUN apt-get update
 ---> Running in b7c9d4e5f6g7
 ---> bcbca2fcf204

Notice that instead of “Using cache”, you’ll see “Running in [container-id]” messages for each step that uses the cache.


Understanding Docker’s Build Cache

Docker uses a build cache to speed up builds by storing intermediate results. Understanding how this works helps you understand why your aerospike issue might be occurring and how to properly resolve it.

How Docker Cache Works

Docker builds images layer by layer. For each step in your Dockerfile, Docker checks if it can use a cached layer instead of rebuilding:

  1. Cache lookup: Docker looks for a matching layer in the cache
  2. Checksum calculation: Docker calculates a checksum for the build context and Dockerfile
  3. Cache hit: If the checksum matches, Docker uses the cached layer
  4. Cache miss: If anything differs, Docker rebuilds that layer

Why Your Aerospike Issue Might Be Happening

The problem you’re describing - where the cache indicates aerospike is installed but it’s not actually available in containers - typically occurs when:

  • Partial build: A previous build failed midway through, leaving you with a partial installation
  • Cache inconsistency: The cache is corrupted or inconsistent
  • Build context changes: Something in your build context changed without updating the Dockerfile

Alternative Methods for Clean Builds

While --no-cache is the most direct approach, there are other methods to achieve clean builds in different scenarios.

Method 1: Modify the Dockerfile

Sometimes you can trigger a clean build by making a small, insignificant change to your Dockerfile:

dockerfile
# Add a comment or change a comment
# This forces Docker to rebuild from this point
RUN echo "Build timestamp: $(date)" > /dev/null && \
    apt-get update && \
    apt-get install -y openjdk-7-jdk

Method 2: Clean the Cache Manually

You can clean the Docker build cache entirely:

bash
# Clean up all unused objects including build cache
docker system prune -a

# Or specifically clean build cache
docker builder prune -a

Warning: This removes all unused objects and will significantly increase build times afterward.

Method 3: Multi-stage Builds

For complex builds, consider using multi-stage builds to avoid caching issues between different build phases:

dockerfile
# Build stage
FROM ubuntu:12.04 as builder
RUN apt-get update && apt-get install -y build-essential
COPY . /app
RUN cd /app && make build

# Final stage  
FROM ubuntu:12.04
COPY --from=builder /app/dist /app/dist
# ... rest of your configuration

Specific Fix for Your Aerospike Problem

Since you’re experiencing issues with aerospike not being properly installed despite the cache indicating it was, here’s a systematic approach:

  1. Force a clean build with --no-cache
  2. Check the build log carefully for any error messages during the aerospike installation steps
  3. Verify the installation within the build container:
bash
# Build with --no-cache
docker build --no-cache -t u12_core_fixed -f u12_core .

# Run a container and check if aerospike is actually installed
docker run -it --rm u12_core_fixed bash -c "which aerospike-server && dpkg -l | grep aerospike"
  1. Inspect the final image to ensure all files are present:
bash
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock alpine docker run --rm -v $(pwd):/target u12_core bash -c "ls -la /opt/aerospike* 2>/dev/null || echo 'Aerospike not found'"

Common Cache Issues and Solutions

Issue Solution
Corrupted cache Use --no-cache or docker builder prune
Partial builds Always check build logs for complete success
Missing files in final image Use RUN commands to verify installation within the build process
Cache invalidation Modify Dockerfile or use build args with timestamps

Best Practices for Docker Builds

When to Use --no-cache

Use --no-cache when:

  • You’re debugging build issues
  • You suspect cache corruption
  • You’ve made significant changes to the Dockerfile or build context
  • You need to ensure a completely fresh build

Optimizing Builds While Maintaining Reliability

To avoid unnecessary clean builds while maintaining reliability:

  1. Use specific package versions in your Dockerfile:

    dockerfile
    RUN apt-get install -y aerospike-server-community=3.12.1
    
  2. Add build verification steps:

    dockerfile
    RUN dpkg -l | grep aerospike || (echo "Aerospike installation failed" && exit 1)
    
  3. Use build arguments for version control:

    dockerfile
    ARG AEROSPIKE_VERSION=3.12.1
    RUN apt-get install -y aerospike-server-community=${AEROSPIKE_VERSION}
    
  4. Clean up apt cache in the same layer:

    dockerfile
    RUN apt-get update && apt-get install -y \
        openjdk-7-jdk \
        aerospike-server-community && \
        rm -rf /var/lib/apt/lists/*
    

Monitoring Build Health

Add verification commands to ensure your builds are working correctly:

dockerfile
# Add this to verify aerospike installation
RUN aerospike-server --help >/dev/null 2>&1 || \
    (echo "Aerospike server not properly installed" && exit 1)

This will cause the build to fail if aerospike isn’t properly installed, preventing you from creating broken images.


Conclusion

To force Docker to perform a clean build without using the cache, simply add the --no-cache flag to your build command: docker build --no-cache -t u12_core -f u12_core .. This will rebuild every layer from scratch and resolve your aerospike installation issue.

Key takeaways:

  • Use --no-cache for debugging and when you suspect cache corruption
  • Understand how Docker’s layer caching works to better troubleshoot build issues
  • Consider alternative methods like modifying your Dockerfile or cleaning the cache manually
  • Add verification steps in your Dockerfile to catch installation failures early
  • For complex builds, use multi-stage builds and specific package versions to maintain reliability

The most immediate solution for your specific problem is to run the build with --no-cache and carefully inspect the output to ensure the aerospike installation completes successfully this time.

Sources

  1. Docker Build Reference - --no-cache flag
  2. Docker Build Cache Documentation
  3. Docker Best Practices - Managing Build Cache