如何设置Docker容器在构建时的时间



所以我试图建立一个高山容器,包括一个应用程序,需要bashcurl安装。

问题是Alpine似乎认为年份是2037(可能是因为主机Pi缺乏硬件锁),忽略了正确的主机操作系统/系统时间(由NTP保持最新),因此apk调用失败:

fetch https://dl-cdn.alpinelinux.org/alpine/v3.14/main/armv7/APKINDEX.tar.gz
fetch https://dl-cdn.alpinelinux.org/alpine/v3.14/community/armv7/APKINDEX.tar.gz
WARNING: Ignoring https://dl-cdn.alpinelinux.org/alpine/v3.14/main: temporary error (try again later)
WARNING: Ignoring https://dl-cdn.alpinelinux.org/alpine/v3.14/community: temporary error (try again later)
ERROR: unable to select packages:
bash (no such package):
required by: world[bash]
curl (no such package):
required by: world[curl]

可以使用docker run -it --privileged ...以交互模式启动容器,并且时间将被正确设置,因此安装将顺利进行。我发现的参考资料表明privileged构建是不可能的(参考?)。

我尝试了很多方法在构建时将时间传递到容器中,但都不成功:

# syntax=docker/dockerfile:1
FROM alpine:3.14
# Pass build-time using `--build-arg time=$(date +%s)` (w/ default value)
ARG time=1632511895
#RUN ["/bin/date", "-s", "@$time"]      # `invalid date @$time`
#RUN ["/bin/date", "-s", "@"$time]      # `/bin/date/` not found
#RUN echo $(date)       # no output
#RUN date -s @$time     # `date: can't set date: Operation not permitted`
#RUN sudo date -s @$time        # /bin/sh: sudo: not found
# cannot build with `--privileged` so clock will be in 2037 and apk will fail
RUN apk add --no-cache curl bash
WORKDIR /tmp

如何在构建时安装curlbash??

<标题>尝试
  • 尝试将时区设置为此处指定的:没有效果
  • 尝试强迫apk在apk命令(TY @jan-garaj)之前使用RUN sed -i 's/https/http/g' /etc/apk/repositories的HTTP:我只得到一个新的/额外的错误:The command '/bin/sh -c apk add --no-cache curl bash' returned a non-zero code: 2

更多信息
# date       
Sat Oct 23 13:17:47 EDT 2021
# docker info
Client:
Debug Mode: false

Server:
Containers: 4
Running: 0
Paused: 0
Stopped: 4
Images: 19
Server Version: 19.03.15
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 269548fa27e0089a8b8278fc4fc781d7f65a939b
runc version: ff819c7e9184c13b7c2607fe6c30ae19403a7aff
init version: fec3683
Security Options:
seccomp
Profile: default
Kernel Version: 4.19.66-v7+
Operating System: Raspbian GNU/Linux 9 (stretch)
OSType: linux
Architecture: armv7l
CPUs: 4
Total Memory: 858.7MiB
Name: rpi0.crcondo
ID: WW63:IXLY:OBPE:AX4O:45H7:OAUH:CELE:ALDG:ZHC3:RTQW:I32M:GSDL
Docker Root Dir: /var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false

WARNING: No swap limit support
WARNING: No cpu cfs quota support
WARNING: No cpu cfs period support

所以@JanGaraj的回答给了我一个重要的引导:Alpine 3.14的发布说明提到它需要Docker >=20.10.0(我目前在19.03.15)。

回到Alpine 3.13的发布说明:

  • Docker版本要求是19.03.9[我有]
  • libseccomp 2.4.2

仅仅使用FROM alpine:3.13仍然不起作用。

检查第二个要求,我有一个以前版本的libseccomp[2]和网络搜索引导我到这篇文章:https://blog.samcater.com/fix-workaround-rpi4-docker-libseccomp2-docker-20/

使用其中的步骤升级libseccomp[2]alpine:3.13alpine:3.14都有效!!

修复(来自post)

的步骤

libseccomp2的步骤有很好的文档记录,因为这在多个平台(不仅仅是RPI4)上都是一个问题。你可以一次性安装一个新版本,可以在这里找到https://github.com/itzg/docker-minecraft-server/issues/755#issuecomment-781615497

我个人觉得更好的方法是从Buster Backports repo安装它,这是非常安全的添加。这也意味着任何未来的libseccomp更新将应用到Pi。

# Get signing keys to verify the new packages, otherwise they will not install
rpi ~$ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 04EE7237B7D453EC 648ACFD622F3D138
# Add the Buster backport repository to apt sources.list
rpi ~$ echo 'deb http://httpredir.debian.org/debian buster-backports main contrib non-free' | sudo tee -a /etc/apt/sources.list.d/debian-backports.list
rpi ~$ sudo apt update
rpi ~$ sudo apt install libseccomp2 -t buster-backports

现在看下一个构建时错误消息😅

我会回答"如何在构建时安装curl和bash ";not "如何设置Docker容器在构建时的时间":

我猜apk失败了,因为repo TLS证书dl-cdn.alpinelinux.org在2037 =有效的TLS连接可以创建后无效。您只需要安装包,因此在这种情况下可能会牺牲TLS安全性,并且可以使用纯HTTP连接到存储库作为解决方案。例如

RUN 
sed -i 's/https/http/g' /etc/apk/repositories && 
apk add --no-cache curl bash

不幸的是,apk没有任何忽略TLS验证的标志,所以现在只能使用这个解决方案。

顺便说一句:我会尝试在主机Pi上使用好的旧NTP。容器使用主机时间,因此在主机级别上进行适当的时间配置(通过NTP同步)应该也可以解决容器级别上的问题(然后还可以解决TLS证书验证的问题)。

更新:

见https://wiki.alpinelinux.org/wiki/Release_Notes_for_Alpine_3.14.0:

所以你需要升级Docker版本(20.10.0+),你只有19.03.15

最新更新