BBJ*_*BJ3 22 linux date container docker alpine-linux
我使用以下 Dockerfile在Docker 容器中构建了Alpine Linux:
FROM alpine:3.2
RUN apk add --update jq curl && rm -rf /var/cache/apk/*
Run Code Online (Sandbox Code Playgroud)
构建运行成功:
$ docker build -t collector .
Sending build context to Docker daemon 2.048 kB
Sending build context to Docker daemon
Step 0 : FROM alpine:3.2
3.2: Pulling from alpine
8697b6cc1f48: Already exists
alpine:3.2: The image you are pulling has been verified. Important: image verification is a tech preview feature and should not be relied on to provide security.
Digest: sha256:eb84cc74347e4d7c484d566dec8a5eef82bab1b78308b92cda559bcff29c27cc
Status: Downloaded newer image for alpine:3.2
---> 8697b6cc1f48
Step 1 : RUN apk add --update jq curl && rm -rf /var/cache/apk/*
---> Running in 888571296e79
fetch http://dl-4.alpinelinux.org/alpine/v3.2/main/x86_64/APKINDEX.tar.gz
(1/11) Installing run-parts (4.4-r0)
(2/11) Installing openssl (1.0.2a-r1)
(3/11) Installing lua5.2-libs (5.2.4-r0)
(4/11) Installing lua5.2 (5.2.4-r0)
(5/11) Installing ncurses-terminfo-base (5.9-r3)
(6/11) Installing ncurses-widec-libs (5.9-r3)
(7/11) Installing lua5.2-posix (33.3.1-r2)
(8/11) Installing ca-certificates (20141019-r2)
(9/11) Installing libssh2 (1.5.0-r0)
(10/11) Installing curl (7.42.1-r0)
(11/11) Installing jq (1.4-r0)
Executing busybox-1.23.2-r0.trigger
Executing ca-certificates-20141019-r2.trigger
OK: 9 MiB in 26 packages
---> 7625779b773d
Removing intermediate container 888571296e79
Successfully built 7625779b773d
Run Code Online (Sandbox Code Playgroud)
无论如何,当我运行date -d它失败时:
$ docker run -i -t collector sh
/ # date -d yesterday
date: invalid date 'yesterday'
/ # date -d now
date: invalid date 'now'
/ # date -d next-month
date: invalid date 'next-month'
Run Code Online (Sandbox Code Playgroud)
而其余选项似乎运行正常:
/ # date
Sat May 30 18:57:24 UTC 2015
/ # date +"%A"
Saturday
/ # date +"%Y-%m-%dT%H:%M:%SZ"
2015-05-30T19:00:38Z
Run Code Online (Sandbox Code Playgroud)
BBJ*_*BJ3 42
BusyBox/Alpine 版本的 date 不支持-d选项,即使帮助在 Ubuntu 版本和其他更胖的发行版中完全相同。
此外,“容器化”在这里不会遗漏任何东西。
要使用-d选项,您只需要添加coreutils包:
$ cat Dockerfile.alpine-coreutils
FROM alpine:3.2
RUN apk add --update coreutils && rm -rf /var/cache/apk/*
$ docker build -t alpine-coreutils - < Dockerfile.alpine-coreutils
Sending build context to Docker daemon 2.048 kB
Sending build context to Docker daemon
Step 0 : FROM alpine:3.2
3.2: Pulling from alpine
8697b6cc1f48: Already exists
alpine:3.2: The image you are pulling has been verified. Important: image verification is a tech preview feature and should not be relied on to provide security.
Digest: sha256:eb84cc74347e4d7c484d566dec8a5eef82bab1b78308b92cda559bcff29c27cc
Status: Downloaded newer image for alpine:3.2
---> 8697b6cc1f48
Step 1 : RUN apk add --update coreutils && rm -rf /var/cache/apk/*
---> Running in 694fa5cb271c
fetch http://dl-4.alpinelinux.org/alpine/v3.2/main/x86_64/APKINDEX.tar.gz
(1/3) Installing libattr (2.4.47-r3)
(2/3) Installing libacl (2.2.52-r2)
(3/3) Installing coreutils (8.23-r0)
Executing busybox-1.23.2-r0.trigger
OK: 12 MiB in 18 packages
---> a7d9116a00ee
Removing intermediate container 694fa5cb271c
Successfully built a7d9116a00ee
$ docker run -i -t alpine-coreutils sh
/ # date -d last-week
Sun May 24 09:19:34 UTC 2015
/ # date -d yesterday
Sat May 30 09:19:46 UTC 2015
/ # date
Sun May 31 09:19:50 UTC 2015
Run Code Online (Sandbox Code Playgroud)
与 Debian 标准相比,图像大小将增加一倍,但直到 11.47 MB,比大小少一个数量级:
$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
alpine-coreutils latest a7d9116a00ee 2 minutes ago 11.47 MB
alpine 3.2 8697b6cc1f48 2 days ago 5.242 MB
debian latest df2a0347c9d0 11 days ago 125.2 MB
Run Code Online (Sandbox Code Playgroud)
感谢 Andy Shinn:https : //github.com/gliderlabs/docker-alpine/issues/40#issuecomment-107122371
克里斯托弗·霍雷尔:https : //github.com/docker-library/official-images/issues/771#issuecomment-107101595
busybox date支持一个-D选项(类似于zsh'sstrftime但与 GNU 相反date)为您提供了一个界面strptime(),您可以在其中解析任何格式的日期(尽管不是相对日期)(ast-opendate和ksh93'sprintf也有自定义输入格式的方法)。
Wherebusybox是针对 C 库实现构建的,strptime()并且strftime()支持非标准%s标志,您可以执行以下操作:
date -D %s -d "$(($(date +%s) - 86400))"
Run Code Online (Sandbox Code Playgroud)
然而,strftime()在alpine Linux 使用的 musl C 库中,支持%s但strptime()不支持。没关系,因为 busybox date,就像 GNUdate支持@epochtime日期规范一样,所以你可以这样做:
date -d "@$(($(date +%s) - 86400))"
Run Code Online (Sandbox Code Playgroud)
它会给你的日期86400秒前,没有GNU的模糊性date的yesterday(是86400年前,或者是昨天在一天的同一时间?如果还有昨天是一天中没有这样的时间,因为时钟改变夏令时?)。
该语法也适用于 GNU date。
使用 ast-open date(或 ksh93 printf '%(%c)T'),您可以使用:
date -d "#$(($(date +%s) - 86400))"
Run Code Online (Sandbox Code Playgroud)
反而。
请注意,如果您要报案UTC(祖鲁语)的时候,你应该使用TZ=UTC0 date或date -u否则,你会得到系统/用户的时区的时间。
我想您date可以在该容器中运行的不是Linux 主机上常见的GNU coreutils date,而是Busybox小程序之一。尝试从他们俩那里获取帮助消息。
# date --help
BusyBox v1.22.1 (Ubuntu 1:1.22.0-9ubuntu1) multi-call binary.
Usage: date [OPTIONS] [+FMT] [TIME]
Display time (using +FMT), or set time
[-s,--set] TIME Set time to TIME
-u,--utc Work in UTC (don't convert to local time)
-R,--rfc-2822 Output RFC-2822 compliant date string
-I[SPEC] Output ISO-8601 compliant date string
SPEC='date' (default) for date only,
'hours', 'minutes', or 'seconds' for date and
time to the indicated precision
-r,--reference FILE Display last modification time of FILE
-d,--date TIME Display TIME, not 'now'
-D FMT Use FMT for -d TIME conversion
Recognized TIME formats:
hh:mm[:ss]
[YYYY.]MM.DD-hh:mm[:ss]
YYYY-MM-DD hh:mm[:ss]
[[[[[YY]YY]MM]DD]hh]mm[.ss]
'date TIME' form accepts MMDDhhmm[[YY]YY][.ss] instead
Run Code Online (Sandbox Code Playgroud)
BusyBox 为具有单个压缩二进制文件的 Posix 标准命令提供了许多小程序,但大多数程序都具有非常有限的功能,以换取其节省的大小(例如,比较两个环境find --help或两个环境中的输出)。tar --help经常发生这样的情况:在开发/主机环境中成功运行的脚本在 BusyBox 的容器/目标环境中根本不起作用。