Skip to content

Instantly share code, notes, and snippets.

@Teut2711
Last active January 12, 2024 18:37
Show Gist options
  • Save Teut2711/f8c0eef8ee6effd14a3727f200953266 to your computer and use it in GitHub Desktop.
Save Teut2711/f8c0eef8ee6effd14a3727f200953266 to your computer and use it in GitHub Desktop.
What the heck does apt update do?
# Use an official PostgreSQL image as the base image
FROM postgres:13.12-bullseye
# Automate interactive shell
ENV DEBIAN_FRONTEND=noninteractive
# Update index/packages
RUN apt update
RUN apt upgrade -y
# Install postgres common package which lists extensions
RUN apt install -y gnupg postgresql-common apt-transport-https lsb-release wget
RUN echo | /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh
# Download timescaledb package
RUN echo "deb https://packagecloud.io/timescale/timescaledb/debian/ $(lsb_release -c -s) main" | tee /etc/apt/sources.list.d/timescaledb.list
RUN wget --quiet -O - https://packagecloud.io/timescale/timescaledb/gpgkey | apt-key add -
# Update index
RUN apt update
# Install timescaledb
RUN apt -y install timescaledb-2-postgresql-13
# Install pg cron
RUN apt -y install postgresql-13-cron
# Install postgis
RUN apt update
RUN apt -y install postgresql-13-postgis-3
# Install locales
RUN apt update && apt-get install -y locales
RUN echo "en_US.UTF-8 UTF-8" > /etc/locale.gen
RUN locale-gen en_US.UTF-8
# Install other requirements
RUN apt -y install postgresql-client
RUN apt -y install systemd
RUN apt -y install vim nano
COPY docker-entrypoint-initdb.d/* /docker-entrypoint-initdb.d/
# RUN rm -f /var/lib/postgresql/data/postgresql.auto.conf
# COPY ./postgresql.conf /var/lib/postgresql/data/postgresql.auto.conf
EXPOSE 5432
-----------------------
Why are there 2 apt updates here?
here is the full, detailed explanation
apt is a package management tool
when debian and debian derivatives want to publish packages for people to download, they put them on a source repository
in order for apt to download and install those packages, it must know where those repositories are and what's in them
each repository has a release file that contains the packages and versions that are in that repository
apt, your local program, needs to know this information so that it can know what packages and versions it can install
it does this by referencing the sources.list file (and files in sources.list.d)
when you run apt update, it reads those files, and downloads the release files from those source repositories, and updates its local database of what packages and versions are available for download
so that later, you can install or upgrade packages from those source repositorie
this database becomes outdated whenever those source repositories change, or become unavailable
so you must run apt update to get the current state of what's available, working on your local copy
in practice, that means you run apt update before installing or upgrading packages, and after modifying any of your package sources
in container land, you want to minimize image size, so you also run apt clean after you're done
in your second example, they needed to add a third party package repository to install the package
but they also needed to install some tools first
so they had to update, install the needed tools, add the new source, then update again, so they could install the package
it's not redundant, it's a necessary order of operations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment