如何在 ubuntu docker 镜像上安装 tzdata?

use*_*739 107 timezone docker

我在 Dockerfile 中有以下行。

RUN apt-get install -y tzdata
Run Code Online (Sandbox Code Playgroud)

当我运行它时,它会询问我的输入。在我提供输入后,它挂在那里。有谁知道如何解决这个问题?

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:
``
Run Code Online (Sandbox Code Playgroud)

pet*_*rtc 105

仅一行:

RUN DEBIAN_FRONTEND="noninteractive" apt-get -y install tzdata
Run Code Online (Sandbox Code Playgroud)

  • 我可以建议在运行前将这些放入 ENV 语句中吗?`ENV DEBIAN_FRONTEND="非交互式" TZ="欧洲/伦敦"` (11认同)
  • 这是最简单的解决方案。您还可以添加 TZ="America/New_York"。在运行命令中添加环境变量是一个很酷的技巧。 (9认同)
  • @DannyStaple 这会将环境变量留在生成的 docker 容器中,这可能是不需要的。通过将环境变量赋予“RUN”命令,您可以确保环境变量仅影响 tzdata 安装。 (7认同)
  • 不幸的是,这对我不起作用,@SnakE 的答案确实有效 (5认同)
  • 如果您使用`sudo`来运行`apt-get`(因为用户不是root),请确保传递`-E`标志,否则`sudo`将丢弃所有环境变量(包括`DEBIAN_FRONTEND`)。 (5认同)

小智 56

您可以使用ARGENV指令来发挥您的优势:

ARG DEBIAN_FRONTEND=noninteractive
ENV TZ=Europe/Moscow
RUN apt-get install -y tzdata
Run Code Online (Sandbox Code Playgroud)

这种方式DEBIAN_FRONTEND只会在您构建图像时定义,而TZ在运行时会持续存在。


Rom*_*nov 48

您需要执行一系列命令:

    # 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
Run Code Online (Sandbox Code Playgroud)

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

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

ADD yourscript.sh /yourscript.sh
RUN /yourscript.sh
Run Code Online (Sandbox Code Playgroud)


小智 12

在 docker-compose 文件中设置两个环境变量。一个禁用提示,另一个设置时区。

docker-compose.yml

version: '3.7'
services:
  timezone:
    build: .
    environment:
      - TZ=America/New_York
      - DEBIAN_FRONTEND=noninteractive
Run Code Online (Sandbox Code Playgroud)

然后只需安装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
Run Code Online (Sandbox Code Playgroud)

去测试:

docker-compose build timezone
Run Code Online (Sandbox Code Playgroud)


小智 10

确保您使用的是@petertc 的解决方案,并且在apt-get update && apt-get installDEBIAN_FRONTEND语句之后的同一行上执行&&

对:

RUN apt-get update && DEBIAN_FRONTEND="noninteractive" TZ="America/New_York" apt-get install -y tzdata
Run Code Online (Sandbox Code Playgroud)

错误的:

RUN DEBIAN_FRONTEND="noninteractive" TZ="America/New_York" apt-get update && apt-get install -y tzdata
Run Code Online (Sandbox Code Playgroud)