gpt4 book ai didi

docker - 如何将 docker cron 日志写入 stdout/stderr

转载 作者:行者123 更新时间:2023-12-02 18:09:25 24 4
gpt4 key购买 nike

我有运行 cron 的 dockerCMD ["cron","-f"]命令。这会将输出写入 cron 日志文件。
有什么办法可以将这些日志重定向到控制台。
我的基本图像看起来像

FROM ubuntu:latest
RUN ls

RUN apt-get update
RUN apt install -y software-properties-common
RUN add-apt-repository -y ppa:openjdk-r/ppa
RUN apt-get update
RUN apt-get install -y openjdk-12-jre

RUN apt-get -y install cron wget unzip



RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
&& echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list \
&& apt-get update -qqy \
&& apt-get -qqy install google-chrome-stable \
&& rm /etc/apt/sources.list.d/google-chrome.list \
&& rm -rf /var/lib/apt/lists/* /var/cache/apt/* \
&& sed -i 's/"$HERE\/chrome"/"$HERE\/chrome" --no-sandbox/g' /opt/google/chrome/google-chrome


ARG CHROME_DRIVER_VERSION=76.0.3809.68
RUN wget --no-verbose -O /tmp/chromedriver_linux64.zip https://chromedriver.storage.googleapis.com/$CHROME_DRIVER_VERSION/chromedriver_linux64.zip \
&& rm -rf /opt/chromedriver \
&& unzip /tmp/chromedriver_linux64.zip -d /opt \
&& rm /tmp/chromedriver_linux64.zip \
&& mv /opt/chromedriver /opt/chromedriver-$CHROME_DRIVER_VERSION \
&& chmod 755 /opt/chromedriver-$CHROME_DRIVER_VERSION \
&& ln -fs /opt/chromedriver-$CHROME_DRIVER_VERSION /usr/bin/chromedriver

COPY tests-cron /etc/cron.d/tests-cron
RUN chmod 0644 /etc/cron.d/tests-cron
WORKDIR /etc/cron.d
RUN ls
RUN crontab /etc/cron.d/tests-cron

RUN touch /var/log/cron.log



WORKDIR /app
RUN mkdir pricetest

COPY --from=maven-container /app/ pricetest/
RUN cp /usr/bin/chromedriver ./pricetest

WORKDIR /app/pricetest
ENV SHELL=/bin/bash

CMD ["cron", "-f"]
PS:这个项目我还在进行中。所以我还没有优化 docker build。
和 cronfile ( tests-cron ) 我包含
* * * * * root echo "Hello world"
使用的基本图像: ubuntu:latest

最佳答案

是的,有一种方法可以将日志重定向到控制台,您只需要更改 Dockerfile 的入口点即可。这是基于 alpine 的工作示例,它每分钟运行一次带有输出的 cronjob。
Alpine 形象
CMD 标志

-f  Foreground
-L FILE Log to FILE
文件

FROM alpine:latest
RUN echo "* * * * * echo hello" | crontab -
CMD ["crond","-f", "-L", "/dev/stdout"]
enter image description here
另一件事,请始终共享您的基本图像,以便很容易理解问题的上下文。
Ubuntu 镜像
ubuntu 中的解决方法是替换 CMD有点,但我不会推荐这种方法,因为 cron 不会是容器的父进程。您可以阅读有关此方法的更多信息 herehere
如果要优化,请使用 alpine docker 容器的 future 镜像,这是这两个镜像的大小差异。

Alpine is just 5MB where Ubuntu is 91MB


enter image description here
这是使用基本镜像 Ubuntu 输出的 cron 的工作示例

FROM ubuntu:latest
RUN apt-get update && apt-get -y install cron
RUN touch /var/log/cron.log
RUN (crontab -l ; echo "* * * * * echo "Hello world" >> /var/log/cron.log") | crontab
CMD cron && tail -f /var/log/cron.log

关于docker - 如何将 docker cron 日志写入 stdout/stderr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57335289/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com