NeuroAgent

How to Force Docker Clean Build Without Cache

Learn how to force Docker to rebuild images without cache using --no-cache flag. Complete guide to clean Docker builds for consistent container environments.

Question

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

I’m building a Docker image using the command:

docker build -t u12_core -f u12_core .

When I rebuild 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 shows that aerospike is installed. However, I don’t find it inside containers spawned from this image, so I want to rebuild this image without using the cache. How can I force Docker to rebuild a clean image without the cache?

NeuroAgent

To force Docker to perform a clean build without using cache, you need to add the --no-cache flag to your build command. For your specific case, the command would be:

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

This flag tells Docker to ignore any cached layers and rebuild the entire image from scratch, ensuring that all steps are executed again regardless of whether they appear to be unchanged.

Contents


Understanding Docker Cache

Docker build caching is designed to speed up the build process by storing intermediate layers. When you run docker build, Docker checks each step against the cache. If a step hasn’t changed, Docker uses the cached version instead of rebuilding it. This is why you see “Using cache” messages in your build output.

However, as you’ve discovered, this caching behavior can sometimes lead to inconsistencies where the container doesn’t reflect the expected state despite showing cached layers in the build process.

Primary Methods for Clean Builds

Using the --no-cache Flag

The most straightforward solution is using the --no-cache flag. According to Cloudbees, “Using Docker’s –no-cache option forces the container build process to skip all cached layers.”

For your specific command:

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

This will:

  • Force Docker to execute every build step
  • Download fresh base images when needed
  • Create entirely new image layers
  • Ignore all existing cache entries

Note: Clean builds take longer because Docker can’t leverage any cached layers, but they ensure complete consistency between your Dockerfile and the resulting image.

Alternative Approaches

Using --pull with --no-cache

Sometimes you may want to force a fresh pull of the base image as well. In this case, combine both flags:

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

As one Stack Overflow user noted, “The --pull option did the trick for me. Just --no-cache, build still broke. Put in --pull as well, build worked!”

Using Cache Busting Arguments

For more controlled cache invalidation, you can use build arguments with changing values. As Bomberbot demonstrates:

bash
docker build -t u12_core -f u12_core --build-arg CACHEBUST=$(date +%s) .

This approach:

  • Adds a timestamp as a build argument
  • Invalidates cache for that layer and all subsequent layers
  • Allows caching for unchanged base layers
  • Provides more granular control over cache invalidation

Docker Compose Solution

If you’re using docker-compose, the solution is similar but with a slightly different syntax:

bash
docker-compose build --no-cache

Or for a specific service:

bash
docker-compose build --no-cache u12_core

Best Practices

  1. Use --no-cache strategically: While tempting, avoid using --no-cache for every build as it significantly slows down the build process.

  2. Isolate cache-busting steps: Place build arguments or commands that change frequently near the end of your Dockerfile to minimize the impact on build performance.

  3. Consider layer optimization: Review your Dockerfile to ensure you’re following best practices for Docker layering. This can help reduce the need for frequent cache invalidation.

  4. Regular cache cleanup: Periodically clean up unused Docker cache to save disk space:

    bash
    docker system prune -a
    

The --no-cache flag should resolve your immediate issue with the aerospike installation not appearing in containers, as it forces a complete rebuild of every layer from scratch.

Sources

  1. How to Use Docker Build Without Cache for Clean Builds - Cloudbees
  2. How to force Docker for a clean build of an image - Stack Overflow
  3. Docker Cache – How to Do a Clean Image Rebuild and Clear Docker’s Cache - Medium
  4. docker build --no-cache | Build docker without cache - GoLinuxCloud
  5. How to Make Docker Rebuild an Image Without Its Cache - HowToGeek
  6. Docker Cache – How to Do a Clean Image Rebuild and Clear Docker’s Cache - Bomberbot

Conclusion

To force Docker to perform a clean build without cache, simply add the --no-cache flag to your build command. This ensures that Docker rebuilds every layer from scratch, addressing the inconsistency you’re experiencing with aerospike installation. For your specific use case, run:

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

Remember that clean builds take longer but guarantee complete consistency between your Dockerfile and the resulting image. Consider using cache-busting techniques or optimizing your Dockerfile structure for better performance in development workflows.