在构建 Dockerfile 时处理 Configuring tzdata 交互输入问题

在构建 Dockerfile 时处理 Configuring tzdata 交互输入问题

通过 dockerfile 构建基于 ubuntu 的镜像时遇到有应用依赖 tzdata,需要交互式输入选项问题解决

问题

在 dockerfile 中通过 apt 安装软件包时,如果软件依赖有 tzdata,会出现如下情况

shell
1
2
3
4
5
6
7
Configuring tzdata
------------------

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 2. America 3. Antarctica 4. Australia 5. Arctic 6. Asia 7. Atlantic 8. Europe 9. Indian 10. Pacific 11. SystemV 12. US 13. Etc
Geographic area:

在自动化构建时会由于交互问题一直卡住,为了解决这个问题,我们需要将 tzdata 设置为非交互。

解决方法

在 dockerfile 中添加 DEBIAN_FRONTEND=noninteractive 环境变量或者直接在 RUN 中添加

Dockerfile
1
2
3
4
5
6
7
FROM ubuntu:20.04

ENV TZ=Asia/Shanghai

RUN DEBIAN_FRONTEND=noninteractive \
&& apt -y update \
&& apt -y install tzdata

构建日志如下:

docker build >folded
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# docker build -t tzdata:test .
Sending build context to Docker daemon 2.048kB
Step 1/3 : FROM ubuntu:20.04
---> 26b77e58432b
Step 2/3 : ENV TZ=Asia/Shanghai
---> Using cache
---> c7fdcacae610
Step 3/3 : RUN DEBIAN_FRONTEND=noninteractive && apt -y update && apt -y install tzdata
---> Running in c89b1bf7bdcb

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

Get:1 http://archive.ubuntu.com/ubuntu focal InRelease [265 kB]
Get:2 http://security.ubuntu.com/ubuntu focal-security InRelease [109 kB]
Get:3 http://archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Get:4 http://security.ubuntu.com/ubuntu focal-security/restricted amd64 Packages [218 kB]
Get:5 http://archive.ubuntu.com/ubuntu focal-backports InRelease [101 kB]
Get:6 http://archive.ubuntu.com/ubuntu focal/universe amd64 Packages [11.3 MB]
Get:7 http://security.ubuntu.com/ubuntu focal-security/universe amd64 Packages [682 kB]
Get:8 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages [746 kB]
Get:9 http://security.ubuntu.com/ubuntu focal-security/multiverse amd64 Packages [21.6 kB]
Get:10 http://archive.ubuntu.com/ubuntu focal/multiverse amd64 Packages [177 kB]
Get:11 http://archive.ubuntu.com/ubuntu focal/restricted amd64 Packages [33.4 kB]
Get:12 http://archive.ubuntu.com/ubuntu focal/main amd64 Packages [1275 kB]
Get:13 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [1160 kB]
Get:14 http://archive.ubuntu.com/ubuntu focal-updates/restricted amd64 Packages [250 kB]
Get:15 http://archive.ubuntu.com/ubuntu focal-updates/multiverse amd64 Packages [29.6 kB]
Get:16 http://archive.ubuntu.com/ubuntu focal-updates/universe amd64 Packages [949 kB]
Get:17 http://archive.ubuntu.com/ubuntu focal-backports/universe amd64 Packages [4305 B]
Fetched 17.5 MB in 5s (3197 kB/s)
Reading package lists...
Building dependency tree...
Reading state information...
7 packages can be upgraded. Run 'apt list --upgradable' to see them.

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

Reading package lists...
Building dependency tree...
Reading state information...
The following NEW packages will be installed:
tzdata
0 upgraded, 1 newly installed, 0 to remove and 7 not upgraded.
Need to get 295 kB of archives.
After this operation, 4033 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 tzdata all 2021a-0ubuntu0.20.04 [295 kB]
debconf: delaying package configuration, since apt-utils is not installed
Fetched 295 kB in 1s (198 kB/s)
Selecting previously unselected package tzdata.
(Reading database ... 4121 files and directories currently installed.)
Preparing to unpack .../tzdata_2021a-0ubuntu0.20.04_all.deb ...
Unpacking tzdata (2021a-0ubuntu0.20.04) ...
Setting up tzdata (2021a-0ubuntu0.20.04) ...
debconf: unable to initialize frontend: Dialog
debconf: (TERM is not set, so the dialog frontend is not usable.)
debconf: falling back to frontend: Readline
debconf: unable to initialize frontend: Readline
debconf: (Can't locate Term/ReadLine.pm in @INC (you may need to install the Term::ReadLine module) (@INC contains: /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.30.0 /usr/local/share/perl/5.30.0 /usr/lib/x86_64-linux-gnu/perl5/5.30 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.30 /usr/share/perl/5.30 /usr/local/lib/site_perl /usr/lib/x86_64-linux-gnu/perl-base) at /usr/share/perl5/Debconf/FrontEnd/Readline.pm line 7.)
debconf: falling back to frontend: Teletype
Configuring tzdata
------------------

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 4. Australia 7. Atlantic 10. Pacific 13. Etc
2. America 5. Arctic 8. Europe 11. SystemV
3. Antarctica 6. Asia 9. Indian 12. US
Geographic area:
Use of uninitialized value $_[1] in join or string at /usr/share/perl5/Debconf/DbDriver/Stack.pm line 111.

Current default time zone: '/UTC'
Local time is now: Wed Apr 14 02:38:09 UTC 2021.
Universal Time is now: Wed Apr 14 02:38:09 UTC 2021.
Run 'dpkg-reconfigure tzdata' if you wish to change it.

Use of uninitialized value $val in substitution (s///) at /usr/share/perl5/Debconf/Format/822.pm line 83, <GEN6> line 4.
Use of uninitialized value $val in concatenation (.) or string at /usr/share/perl5/Debconf/Format/822.pm line 84, <GEN6> line 4.
Removing intermediate container c89b1bf7bdcb
---> 96ee179e6473
Successfully built 96ee179e6473
Successfully tagged tzdata:test

DEBIAN_FRONTEND 这个引导参数控制用于安装程序的用户界面的类型。当前可能参数设置可以是:

  • DEBIAN_FRONTEND=noninteractive

  • DEBIAN_FRONTEND=text

  • DEBIAN_FRONTEND=newt

  • DEBIAN_FRONTEND=slang

  • DEBIAN_FRONTEND=ncurses

  • DEBIAN_FRONTEND=bogl

  • DEBIAN_FRONTEND=gtk

  • DEBIAN_FRONTEND=corba

缺省的前端是 DEBIAN_FRONTEND=newtDEBIAN_FRONTEND=text 可以用于串口控制台的安装。一般来说在缺省安装介质上只有 newt 前端,因此目前这个并非很有用。

参考文章:

评论

:D 一言句子获取中...

加载中,最新评论有1分钟缓存...