0
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?

[Tips]docker build 時に apt-get updateでPackages Not Found

Last updated at Posted at 2025-01-14

事象

debian, ubantu のDockerイメージビルド時に以下のエラーが発生。
当該環境ではProxyサーバを利用している。
エラーは先頭でビルドに必要なパッケージ群をインストールする箇所で発生していた。

Dockerfile
RUN apt-get update && apt-get install XXXXX
$ docker-compose up -d --build
:
Err:1 http://security.ubuntu.com/ubuntu jammy-security InRelease
  Could not resolve 'security.ubuntu.com'
Err:2 http://archive.ubuntu.com/ubuntu jammy InRelease
  Could not resolve 'archive.ubuntu.com'
Err:3 http://archive.ubuntu.com/ubuntu jammy-updates InRelease
  Could not resolve 'archive.ubuntu.com'
Err:4 http://archive.ubuntu.com/ubuntu jammy-backports InRelease
  Could not resolve 'archive.ubuntu.com'
Reading package lists...
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/jammy/InRelease  Could not resolve 'archive.ubuntu.com'
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/jammy-updates/InRelease  Could not resolve 'archive.ubuntu.com'
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/jammy-backports/InRelease  Could not resolve 'archive.ubuntu.com'
W: Failed to fetch http://security.ubuntu.com/ubuntu/dists/jammy-security/InRelease  Could not resolve 'security.ubuntu.com'
W: Some index files failed to download. They have been ignored, or old ones used instead.
Reading package lists...
Building dependency tree...
Reading state information...
Package less is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

Package locales is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

E: Package 'locales' has no installation candidate
E: Unable to locate package vim
E: Unable to locate package tmux
[+] Building 0.0s (0/0)
The command '/bin/sh -c apt update &&     apt-get update &&     apt-get install -y locales vim tmux less' returned a non-zero code: 100

原因

apt updateにて、Proxy Serverが読み取れておらず探索が限定されている。

対処方法

(1)Dockerfileに記載する内容

--build-argに指定した内容を元に、apt用のプロキシサーバの設定を行うようなDockerfileを作成します。

docker runしたときにもdocker buildしたときと同じプロキシサーバを使う場合

docker runでコンテナを実行する際にもそのプロキシを使用して欲しいため、/etc/apt/apt.conf.d/にプロキシの設定を行なっています。

Dockerfile
FROM ubuntu:20.04
ARG APT_PROXY
RUN if [ "$APT_PROXY" != "" ]; then echo "Acquire::http { Proxy \"$APT_PROXY\"; };" > /etc/apt/apt.conf.d/01proxy; fi
RUN apt update && \
    apt install --no-install-recommends -y curl && \
    rm -rf /var/lib/apt/lists/*

まず、2行目のARGでAPT_PROXYという名前のビルド引数を設定できるようにしています。APT_PROXYに指定したプロキシがaptの通信に使われることになります。

1つ目のRUNで/etc/apt/apt.conf.d/01proxyにプロキシの設定を行なっています。ビルド時にAPT_PROXYが指定された場合にのみ01proxyを作成するので、APT_PROXYが未指定であれば、aptはインターネット上のリポジトリを直接取得します。

2つ目のRUNでは例としてcurlをインストールしています。

docker buildだけプロキシを使用して、docker runするときにはプロキシを使用しない場合

もし、コンテナビルド時だけプロキシを使用して、docker runするときにはプロキシを使用して欲しくない場合は、apt.conf.dに設定する代わりに、以下のようにhttp_proxy環境変数を設定すればビルド時だけ指定したプロキシを使うことができると思います。

Dockerfile
FROM ubuntu:22.04
ARG APT_PROXY
RUN if [ "$APT_PROXY" != "" ]; then echo "Acquire::http { Proxy \"$APT_PROXY\"; };" > /etc/apt/apt.conf.d/01proxy; fi
RUN yes | unminimize
RUN apt-get update && \
    apt-get install -y locales vim tmux less
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y tzdata
RUN locale-gen ja_JP.UTF-8
ENV LANG=ja_JP.UTF-8
ENV TZ=Asia/Tokyo
WORKDIR /work

(2)コンテナのビルド方法

dockerコマンドを使用する場合、以下のようにビルド時に--build-argを使用してプロキシを指定してビルドします。

$ docker build --build-arg APT_PROXY=http://proxy:8080/ -t apt-proxy-test .

docker-composeを使用する場合には、以下のようにargsでAPT_PROXYを設定します。

compose.yml
services:
  app:
    build:
      context: .
      args:
        - APT_PROXY=http://proxy-server.ip:8080/
    tty: true
    volumes:
      - .:/work

##参考

0
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
0
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?