我在Dockerfile中有以下一行。

RUN apt-get install -y tzdata


运行它时,它会询问我的输入。我提供输入后,它就挂在那里。有人知道如何解决此问题吗?

Step 25/25 : RUN apt-get install -y tzdata
 ---> Running in ee47a1beff84
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 0 not upgraded.
Need to get 189 kB of archives.
After this operation, 3104 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 tzdata all 2018i-0ubuntu0.18.04 [189 kB]
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: (This frontend requires a controlling tty.)
debconf: falling back to frontend: Teletype
dpkg-preconfigure: unable to re-open stdin: 
Fetched 189 kB in 1s (219 kB/s)
Selecting previously unselected package tzdata.
(Reading database ... 25194 files and directories currently installed.)
Preparing to unpack .../tzdata_2018i-0ubuntu0.18.04_all.deb ...
Unpacking tzdata (2018i-0ubuntu0.18.04) ...
Setting up tzdata (2018i-0ubuntu0.18.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
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:
``


#1 楼

仅一行:

RUN DEBIAN_FRONTEND="noninteractive" apt-get -y install tzdata


评论


这是最简单的解决方案。您还可以添加TZ =“ America / New_York”。将环境变量添加到run命令是一个很酷的技巧。

– Harvey Pham
20年4月30日在18:27

不幸的是,这对我不起作用,@ SnakE的回答确实对我有用

– Francesco Borzi
20年8月11日在8:31

我可以建议在运行之前将其放入ENV语句中吗? ENV DEBIAN_FRONTEND =“非交互式” TZ =“欧洲/伦敦”

–丹尼·史泰普(Danny Staple)
20-10-3在11:24

@DannyStaple这会将环境变量保留在生成的Docker容器中,这可能是不希望的。通过向RUN命令提供env var,可以确保环境变量仅影响tzdata安装。

–伊格纳修斯
20-11-13在9:06

我想这取决于用例,但是可以。

–丹尼·史泰普(Danny Staple)
20-11-13在17:51

#2 楼

您需要执行一系列命令:

# set noninteractive installation
export DEBIAN_FRONTEND=noninteractive
#install tzdata package
apt-get install -y tzdata
# set your timezone
ln -fs /usr/share/zoneinfo/America/New_York /etc/localtime
dpkg-reconfigure --frontend noninteractive tzdata


(以#开头的命令是注释,您可以忽略它们)

最好的方法是要创建脚本,将脚本复制到容器中并执行它
在Dockerfile中:

ADD yourscript.sh /yourscript.sh
RUN /yourscript.sh


评论


好的答案,但是我想问为什么这是必要的?这是Docker问题还是Ubuntu问题?

–杰里米·哈耶克(Jeremy Hajek)
19年11月19日在22:09

@JeremyHajek,这就是docker(和容器)的工作方式。有些事情需要以怪异的方式完成。

–罗密欧·尼诺夫(Romeo Ninov)
19年11月20日在5:14

#3 楼

您可以使用ARGENV伪指令来发挥自己的优势: >

#4 楼

在docker-compose文件中设置两个环境变量。一个禁用提示,另一个设置时区。
version: '3.7'
services:
  timezone:
    build: .
    environment:
      - TZ=America/New_York
      - DEBIAN_FRONTEND=noninteractive

然后只需在映像中安装tzdata。 />要测试:
FROM ubuntu:18.04
RUN apt-get update && apt-get install -y tzdata
# Testing command: Print the date.  It will be in the timezone set from the compose file.
CMD date


#5 楼

确保您使用的是@petertc的解决方案,并且在apt-get update && apt-get install之后的DEBIAN_FRONTEND语句所在的行上执行&&
右:
RUN apt-get update && DEBIAN_FRONTEND="noninteractive" TZ="America/New_York" apt-get install -y tzdata

错误:
RUN DEBIAN_FRONTEND="noninteractive" TZ="America/New_York" apt-get update && apt-get install -y tzdata


#6 楼

从一个简单的Dockerfile可以工作,但可能需要进一步调整(在docker,idc中tz是19:25但在16:25内,因为它是用于ARM64 jetson nano上的自动化目的),现在是IDC。

/>
 Unpacking protobuf-compiler (3.0.0-9.1ubuntu1) ...
Setting up python-chardet (3.0.4-1) ...
Setting up tzdata (2019c-0ubuntu0.18.04) ...

Current default time zone: 'Etc/UTC'
Local time is now:      Wed Apr 22 16:25:17 UTC 2020.
Universal Time is now:  Wed Apr 22 16:25:17 UTC 2020.
Run 'dpkg-reconfigure tzdata' if you wish to change it.

Setting up libxss1:arm64 (1:1.2.2-1) ...
 


#7 楼

在焦点20.04上,使用TZ=...DEBIAN_FRONTEND=...的解决方案不再起作用。它一直工作到仿生18.04。适用于焦点的docker文件片段如下所示:
   ## preesed tzdata, update package index, upgrade packages and install needed software
   RUN truncate -s0 /tmp/preseed.cfg && \
       (echo "tzdata tzdata/Areas select America" >> /tmp/preseed.cfg) && \
       (echo "tzdata tzdata/Zones/America select Los_Angeles" >> /tmp/preseed.cfg) && \
       debconf-set-selections /tmp/preseed.cfg && \
       rm -f /etc/timezone /etc/localtime && \
       apt-get update && \
       DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true \
       apt-get install -y tzdata
   ## cleanup of files from setup
   RUN rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

解决方案主要来自另一个stackoverflow主题。

评论


将子外壳用于预置文件回显的好处是什么?我主要看到一个缺点:您引入了潜在的比赛条件。

–尼尔斯·鲍尔曼(Nils Ballmann)
20 Dec 2'在19:42



感谢您指出比赛条件。

–明华
20/12/04在20:10

#8 楼

对我来说,它起作用了,我更喜欢这种方式(这种方式,您无需设置非交互模式):
使用您的时区设置环境变量,例如:
ENV TZ=Europe/Madrid
然后,将此变量打印到文件中,然后将该文件链接到安装tzdata时配置过程将读取的文件:
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
最后,正常安装tzdata:RUN apt-get update && apt-get install -y tzdata
摘自:https ://dev.to/setevoy/docker-configure-tzdata-and-timezone-during-build-20bk