LoginSignup
0
0

More than 1 year has passed since last update.

Dockerfileで対話式のコマンドをうまく扱えなかったのでnoninteractiveで回避した

Posted at

1.この記事の内容

Dockerfileに記述するaptなどでパッケージをインストールする際の,タイムゾーン設定などの対話式の対処方法を記載します.

1-1.まず結論

対話が必要となるコマンドに対して,DEBIAN_FRONTEND=noninteractiveを設定することで対話箇所を回避できます.

2.問題の事象

筆者はlibopencv-devのインストールでタイムゾーンの設定で止まってしまう問題に遭遇しました.

FROM ubuntu:22.10

RUN apt update && \
    apt install -y libopencv-dev
docker build
$ docker build --network=host -f Dockerfile.bak -t test_image .
[+] Building 916.1s (4/5)
 => [internal] load .dockerignore                                                             0.5s
 => => transferring context: 2B                                                               0.0s
 => [internal] load build definition from Dockerfile.bak                                      0.7s
 => => transferring dockerfile: 205B                                                          0.0s
 => [internal] load metadata for docker.io/library/ubuntu:22.10                               1.9s
 => CACHED [1/2] FROM docker.io/library/ubuntu:22.10@sha256:699796ebf58f6d43889a7a2a29bcc8e4  0.0s
 => [2/2] RUN apt update &&     apt install -y libopencv-dev                                913.3s
 => => # Please select the geographic area in which you live. Subsequent configuration
 => => # questions will narrow this down by presenting a list of cities, representing
 => => # the time zones in which they are located.
 => => #   1. Africa   3. Antarctica  5. Arctic  7. Atlantic  9. Indian    11. US
 => => #   2. America  4. Australia   6. Asia    8. Europe    10. Pacific  12. Etc
 => => # Geographic area:

3.対処方法

設定値は固定なので予め用意した値を設定するだけなのですが,これがうまく扱えずDEBIAN_FRONTEND=noninteractiveを設定して回避する策に落ち着きました.
今回の対話箇所がタイムゾーンの設定でしたので,環境変数TZを設定することで代替できました.

FROM ubuntu:22.10

ENV TZ=Asia/Tokyo

RUN apt update && \
    DEBIAN_FRONTEND=noninteractive apt install -y libopencv-dev
$ docker build --network=host -f Dockerfile -t test_image
.
[+] Building 884.5s (6/6) FINISHED
 => [internal] load build definition from Dockerfile                                          0.7s
 => => transferring dockerfile: 251B                                                          0.0s
 => [internal] load .dockerignore                                                             0.5s
 => => transferring context: 2B                                                               0.0s
 => [internal] load metadata for docker.io/library/ubuntu:22.10                               2.0s
 => CACHED [1/2] FROM docker.io/library/ubuntu:22.10@sha256:699796ebf58f6d43889a7a2a29bcc8e4  0.0s
 => [2/2] RUN apt update &&     DEBIAN_FRONTEND=noninteractive apt install -y libopencv-de  874.5s
 => exporting to image                                                                        7.2s
 => => exporting layers                                                                       7.1s
 => => writing image sha256:d7936f8c199e79f8219894450dbe26f736b5621fe76c03bb0b67b9ec36bf0c1b  0.1s
 => => naming to docker.io/library/test_image                                                 0.1s
$ date
Sat Mar  4 09:55:57 JST 2023
$

調べていると<で入力元ファイルを指定する方法も挙げられていましたが,これでは解決できませんでした.

上手くいかなかった例
FROM ubuntu:22.10

COPY input.txt /tmp/input.txt

RUN apt update && \
    apt install -y libopencv-dev </tmp/input.txt
上手くいかなかった例
$ docker build --network=host -f Dockerfile.bak2 -t test_image .
[+] Building 1074.7s (6/7)
 => [internal] load build definition from Dockerfile.bak2                                     0.8s
 => => transferring dockerfile: 253B                                                          0.0s
 => [internal] load .dockerignore                                                             1.2s
 => => transferring context: 2B                                                               0.0s
 => [internal] load metadata for docker.io/library/ubuntu:22.10                               1.9s
 => CACHED [1/2] FROM docker.io/library/ubuntu:22.10@sha256:699796ebf58f6d43889a7a2a29bcc8e4  0.0s
 => [internal] load build context                                                             0.5s
 => => transferring context: 42B                                                              0.0s
 => [2/3] COPY input.txt /tmp/input.txt                                                       1.4s
 => [3/3] RUN apt update &&     apt install -y libopencv-dev </tmp/input.txt               1069.4s
 => => # Please select the geographic area in which you live. Subsequent configuration
 => => # questions will narrow this down by presenting a list of cities, representing
 => => # the time zones in which they are located.
 => => #   1. Africa   3. Antarctica  5. Arctic  7. Atlantic  9. Indian    11. US
 => => #   2. America  4. Australia   6. Asia    8. Europe    10. Pacific  12. Etc
 => => # Geographic area:

4.さいごに

今回はタイムゾーン設定でしたので,環境変数で代替できましたが,他のコマンドで困る時が来るかもしれませんので,恒久対策しておきたいところです.

対話式のコマンドへ適切に設定する記述方法をご存知の方がいらっしゃれば,コメントなどでご教示いただけますと幸いです.

5.関連リンク

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