5

Sorry, very new to server stuff, but very curious. Why run apt-get update when building a container?

My guess would be that it's for security purposes, if that the case than that'll answer the question.

jkris
  • 5,851
  • 1
  • 22
  • 30

2 Answers2

4

apt-get update ensures all package sources and dependencies are at their latest version, it does not update existing packages that have been installed. It's recommended that you always run apt-get update prior to running an apt-get install this is so when the apt-get install is run, the latest version of the package should be used.

RUN apt-get update -q -y && apt-get install -q -y <your-program>

(the -q -y flags just mean that the apt process will run quietly without asking you for confirmations as this would cause the Docker process to fail)

TheStoneFox
  • 3,007
  • 3
  • 31
  • 47
  • Adding to the answer, it's not only to get the latest version, but also because the package might be outright missing if the sources are outdated enough, which means `apt-get install` would fail completely. – Christian Jun 08 '23 at 22:46
2

First, lets make a distinction between apt-get update and apt-get upgrade. The update is to get the latest package index. This is so that you don't run into errors for outdated or redacted packages when doing a apt-get install.

The upgrade is actually going through an upgrading packages. It usually also requires a preceding update to have the updated package index. This might be done if there are package or security concerns of already installed packages.

You usually see an update a lot in builds because the base image may have a fairly out of date package index and just doing an apt-get install can fail.

The upgrade would be less common. But could still be done if you want to ensure the latest packages are installed.

Andy Shinn
  • 26,561
  • 8
  • 75
  • 93