8

I've noticed with many Debian/ubuntu based docker images, you have to do an apt-get update before you install any packages. The cache is completely clear. I feel like I should do the same myself after I do my RUN apt-update && apt-get install -y whatever. I can't figure out how to do that. Is there a command, or do I just rm -rf q folder?

1
  • 1
    Maybe you should kill the urge to want to remove the apt package index? There is no need to do so. – Rinzwind Jun 29 '18 at 18:16
5

When you're building a docker image you try to keep it the smallest possible. So who builds an image installing a package removes the cache to keep il small. The consequence is that you must run apt-get update if you want to install any package.

And as each docker command executed produces a layer, and its content will stay there "forever", once you finish to install the package you want to remove the cache too.

The two ways to do that are either one of the following:

  1. rm -rf /var/cache/apt/archives.

OR

  1. apt-get clean

clears out the local repository of retrieved package files. It removes everything but the lock file from /var/cache/apt/archives/ and /var/cache/apt/archives/partial/.

(source: man page)

5

rm -rf /var/lib/apt/lists/*

It's in the Docker best practices:

when you clean up the apt cache by removing /var/lib/apt/lists it reduces the image size, since the apt cache is not stored in a layer.

/var/cache/apt/archives doesn't have much data in it. Compare the du between those 2 dirs.

5

As said in David's answer , you usually apt-get clean or rm -rf /var/cache/apt/archives. This removes the package cache, which, according to the manual, contains downloaded packages themselves.

However, according to Docker's best practices, apt-get clean is done for you on any official Ubuntu/Debian image.


The only thing left to clean in this case is the repository index cache : You would only have to

rm -rf /var/cache/apt/lists.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.