事象
debian, ubantu のDockerイメージビルド時に以下のエラーが発生。
当該環境ではProxyサーバを利用している。
エラーは先頭でビルドに必要なパッケージ群をインストールする箇所で発生していた。
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/にプロキシの設定を行なっています。
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環境変数を設定すればビルド時だけ指定したプロキシを使うことができると思います。
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を設定します。
services:
app:
build:
context: .
args:
- APT_PROXY=http://proxy-server.ip:8080/
tty: true
volumes:
- .:/work
##参考