1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Ubuntu 24.04のDockerfileでsed /etc/apt/sources.listには意味が無い

Last updated at Posted at 2025-12-16

気付いたけどググってもあまり情報が無かったので書く。

なぜ意味が無いのか

高速なミラーだったり、アーカイブリポジトリを使うように設定する方法として、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が参照するリポジトリを変更している。

Ubuntu 22.04のsources.list
$ 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に変えて更新した場合などにこのような状況が発生する。

Ubuntu 24.04のsources.list
$ 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に相当する。

ubuntu.sourcesファイル
$ 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を書き換えてやれば良い。

jpミラーに変更する例
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を残しておくのもいいだろう。

複数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
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?