気付いたけどググってもあまり情報が無かったので書く。
なぜ意味が無いのか
高速なミラーだったり、アーカイブリポジトリを使うように設定する方法として、Ubuntu 22.04以前をベースイメージとしたDockerfileで以下のように/etc/apt/sources.listに対してsedをかける方法がよく見られる。
RUN sed -i 's|http://archive.ubuntu.com/ubuntu/|http://jp.archive.ubuntu.com/ubuntu/|g' /etc/apt/sources.list
Ubuntu 22.04以前のイメージでは以下のように/etc/apt/sources.listにリポジトリ情報が書かれている。
sedコマンドでURIを書き換えることで、aptが参照するリポジトリを変更している。
$ docker run --rm ubuntu:22.04 cat /etc/apt/sources.list | head
# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://archive.ubuntu.com/ubuntu/ jammy main restricted
# deb-src http://archive.ubuntu.com/ubuntu/ jammy main restricted
## Major bug fix updates produced after the final release of the
## distribution.
deb http://archive.ubuntu.com/ubuntu/ jammy-updates main restricted
# deb-src http://archive.ubuntu.com/ubuntu/ jammy-updates main restricted
しかしUbuntu 24.04以降のイメージでは、/etc/apt/sources.listは以下のようにリポジトリ情報が別ファイルに移動した旨のコメントのみが書かれている。そのため、上記のsedコマンドを実行しても何も効果が無い。昔作ったDockerfileのベースイメージだけ24.04に変えて更新した場合などにこのような状況が発生する。
$ docker run --rm ubuntu:24.04 cat /etc/apt/sources.list
# Ubuntu sources have moved to the /etc/apt/sources.list.d/ubuntu.sources
# file, which uses the deb822 format. Use deb822-formatted .sources files
# to manage package sources in the /etc/apt/sources.list.d/ directory.
# See the sources.list(5) manual page for details.
どう変化したのか
そもそもリポジトリの変更が必要かどうか(本当に高速になっているか、変更先のサーバーは安定しているか等)を検討する必要があるが、もし必要な場合は/etc/apt/sources.list.d/以下のubuntu.sourcesファイルが旧来のsources.listに相当する。
$ docker run --rm ubuntu:24.04 cat /etc/apt/sources.list.d/ubuntu.sources
# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
## Ubuntu distribution repository
##
## The following settings can be adjusted to configure which packages to use from Ubuntu.
## Mirror your choices (except for URIs and Suites) in the security section below to
## ensure timely security updates.
##
## Types: Append deb-src to enable the fetching of source package.
## URIs: A URL to the repository (you may add multiple URLs)
## Suites: The following additional suites can be configured
## <name>-updates - Major bug fix updates produced after the final release of the
## distribution.
## <name>-backports - software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
## Components: Aside from main, the following components can be added to the list
## restricted - Software that may not be under a free license, or protected by patents.
## universe - Community maintained packages. Software in this repository receives maintenance
## from volunteers in the Ubuntu community, or a 10 year security maintenance
## commitment from Canonical when an Ubuntu Pro subscription is attached.
## multiverse - Community maintained of restricted. Software from this repository is
## ENTIRELY UNSUPPORTED by the Ubuntu team, and may not be under a free
## licence. Please satisfy yourself as to your rights to use the software.
## Also, please note that software in multiverse WILL NOT receive any
## review or updates from the Ubuntu security team.
##
## See the sources.list(5) manual page for further settings.
Types: deb
URIs: http://archive.ubuntu.com/ubuntu/
Suites: noble noble-updates noble-backports
Components: main universe restricted multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
## Ubuntu security updates. Aside from URIs and Suites,
## this should mirror your choices in the previous section.
Types: deb
URIs: http://security.ubuntu.com/ubuntu/
Suites: noble-security
Components: main universe restricted multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
ではどうすればいいのか
そのため、ここのURIを書き換えてやれば良い。
RUN sed -i 's|http://archive.ubuntu.com/ubuntu/|http://jp.archive.ubuntu.com/ubuntu/|g' /etc/apt/sources.list.d/ubuntu.sources && \
sed -i 's|http://security.ubuntu.com/ubuntu/|http://jp.archive.ubuntu.com/ubuntu/|g' /etc/apt/sources.list.d/ubuntu.sources
URIsには複数のURIを指定できる。複数指定した場合は先頭から順に試されるので、高速なミラーを使うために書き換えるような場合は元のURIを残しておくのもいいだろう。
RUN sed -i 's|URIs: http://archive.ubuntu.com/ubuntu/|URIs: http://jp.archive.ubuntu.com/ubuntu/ http://archive.ubuntu.com/ubuntu/|g' /etc/apt/sources.list.d/ubuntu.sources && \
sed -i 's|URIs: http://security.ubuntu.com/ubuntu/|URIs: http://jp.archive.ubuntu.com/ubuntu/ http://security.ubuntu.com/ubuntu/|g' /etc/apt/sources.list.d/ubuntu.sources