Docker での apt-get があまりにも遅いとき

結論

Dockerfile の最初の方で以下のように実行し、 /etc/apt/sources.list を書き換えてしまいます。なお以下の例は Ubuntu 20.04 の場合です。

RUN echo "
deb http://jp.archive.ubuntu.com/ubuntu/ groovy main restricted\n\
deb-src http://jp.archive.ubuntu.com/ubuntu/ groovy main restricted\n\
deb http://jp.archive.ubuntu.com/ubuntu/ groovy-updates main restricted\n\
deb-src http://jp.archive.ubuntu.com/ubuntu/ groovy-updates main restricted\n\
deb http://jp.archive.ubuntu.com/ubuntu/ groovy universe\n\
deb-src http://jp.archive.ubuntu.com/ubuntu/ groovy universe\n\
deb http://jp.archive.ubuntu.com/ubuntu/ groovy-updates universe\n\
deb-src http://jp.archive.ubuntu.com/ubuntu/ groovy-updates universe\n\
deb http://jp.archive.ubuntu.com/ubuntu/ groovy multiverse\n\
deb-src http://jp.archive.ubuntu.com/ubuntu/ groovy multiverse\n\
deb http://jp.archive.ubuntu.com/ubuntu/ groovy-updates multiverse\n\
deb-src http://jp.archive.ubuntu.com/ubuntu/ groovy-updates multiverse\n\
deb http://jp.archive.ubuntu.com/ubuntu/ groovy-backports main restricted universe multiverse\n\
deb-src http://jp.archive.ubuntu.com/ubuntu/ groovy-backports main restricted universe multiverse\n\
deb http://security.ubuntu.com/ubuntu groovy-security main restricted\n\
deb-src http://security.ubuntu.com/ubuntu groovy-security main restricted\n\
deb http://security.ubuntu.com/ubuntu groovy-security universe\n\
deb-src http://security.ubuntu.com/ubuntu groovy-security universe\n\
deb http://security.ubuntu.com/ubuntu groovy-security multiverse\n\
deb-src http://security.ubuntu.com/ubuntu groovy-security multiverse\n" > /etc/apt/sources.list

補足

  • この問題は以前からありますが、仕方がないとうい感じでしょうか
  • 安全を期すためには既存の /etc/apt/sources.list は別途バックアップを作っておいて(リネームして)、処理の最後にそれをもとに戻してやればいいと思います
  • 上記のようにだらだら長いコマンドを実行せず、sed で済ますとかシェルスクリプトを作っておいてそれを実行するとかの方法もあり得ると思います
Powered by はてなブログ