【问题标题】:Dockerfile won't install cronDockerfile 不会安装 cron
【发布时间】:2021-09-16 17:23:21
【问题描述】:

我正在尝试通过我的 Dockerfile 安装 cron,以便 docker-compose 可以在构建时使用不同的入口点创建专用的 cron 容器,这将定期创建另一个运行脚本的容器,然后将其删除。我正在尝试遵循本指南的从您的应用程序服务中分离 Cron 部分:https://www.cloudsavvyit.com/9033/how-to-use-cron-with-your-docker-containers/

我知道操作顺序很重要,但我想知道我的 Dockerfile 中是否配置错误:

FROM swift:5.3-focal as build

RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true \
    && apt-get -q update \
    && apt-get -q dist-upgrade -y \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /build

RUN apt-get update && apt-get install -y cron
COPY example-crontab /etc/cron.d/example-crontab
RUN chmod 0644 /etc/cron.d/example-crontab &&\
    crontab /etc/cron.d/example-crontab

COPY ./Package.* ./
RUN swift package resolve

COPY . .

RUN swift build --enable-test-discovery -c release

WORKDIR /staging

RUN cp "$(swift build --package-path /build -c release --show-bin-path)/Run" ./
RUN [ -d /build/Public ] && { mv /build/Public ./Public && chmod -R a-w ./Public; } || true
RUN [ -d /build/Resources ] && { mv /build/Resources ./Resources && chmod -R a-w ./Resources; } || true

# ================================
# Run image
# ================================
FROM swift:5.3-focal-slim

RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true && \
    apt-get -q update && apt-get -q dist-upgrade -y && rm -r /var/lib/apt/lists/*

RUN useradd --user-group --create-home --system --skel /dev/null --home-dir /app vapor

WORKDIR /app

COPY --from=build --chown=vapor:vapor /staging /app

USER vapor:vapor

EXPOSE 8080

ENTRYPOINT ["./Run"]
CMD ["serve", "--env", "production", "--hostname", "0.0.0.0", "--port", "8080"]

这是我的 docker-compose 文件的相关部分:

services:
  app:
    image: prizmserver:latest
    build:
      context: .
    environment:
      <<: *shared_environment
    volumes:
      - $PWD/.env:/app/.env
    links:
      - db:db
    ports:
      - '8080:8080'
    # user: '0' # uncomment to run as root for testing purposes even though Dockerfile defines 'vapor' user.
    command: ["serve", "--env", "production", "--hostname", "0.0.0.0", "--port", "8080"]
  cron:
    image: prizmserver:latest
    entrypoint: /bin/bash
    command: ["cron", "-f"]

这是我的example-scheduled-task.sh

#!/bin/bash
timestamp=`date +%Y/%m/%d-%H:%M:%S`
echo "System path is $PATH at $timestamp"

这是我的 crontab 文件:

*/5 * * * * /usr/bin/sh /example-scheduled-task.sh

我的脚本 example-scheduled-task.sh 和我的 crontab example-crontab 位于我的应用程序文件夹中,其中 Dockerfiledocker-compose.yml 存在。

为什么我的 cron 容器无法启动?

【问题讨论】:

    标签: docker docker-compose cron dockerfile debian


    【解决方案1】:

    在多阶段构建中,只有最后一个 FROM 将用于生成最终图像。

    例如,下一个例子,a.txt 只能在第一阶段看到,在最终图像中看不到。

    Dockerfile:

    FROM python:3.9-slim-buster
    
    WORKDIR /tmp
    RUN touch a.txt
    RUN ls /tmp
    
    FROM ubuntu:16.04
    RUN ls /tmp
    

    执行:

    # docker build -t abc:1 . --no-cache
    Sending build context to Docker daemon  2.048kB
    Step 1/6 : FROM python:3.9-slim-buster
     ---> c2f204720fdd
    Step 2/6 : WORKDIR /tmp
     ---> Running in 1e6ed4ef521d
    Removing intermediate container 1e6ed4ef521d
     ---> 25282e6f7ed6
    Step 3/6 : RUN touch a.txt
     ---> Running in b639fcecff7e
    Removing intermediate container b639fcecff7e
     ---> 04985d00ed4c
    Step 4/6 : RUN ls /tmp
     ---> Running in bfc2429d6570
    a.txt
    tmp6_uo5lcocacert.pem
    Removing intermediate container bfc2429d6570
     ---> 3356850a7653
    Step 5/6 : FROM ubuntu:16.04
     ---> 065cf14a189c
    Step 6/6 : RUN ls /tmp
     ---> Running in 19755da110b8
    Removing intermediate container 19755da110b8
     ---> 890f13e709dd
    Successfully built 890f13e709dd
    Successfully tagged abc:1
    

    回到您的示例,您将 crontab 复制到 swift:5.3-focal 的阶段,但最后阶段是 swift:5.3-focal-slim,它不会有任何 crontab。

    编辑:

    对你来说,cron 的 compose 也需要更新为 next:

    cron:
        image: prizmserver:latest
        entrypoint: cron
        command: ["-f"]
    

    cron 不需要使用/bash 启动,直接使用cron 覆盖entrypoint 即可。

    【讨论】:

    • 将三个 cron 命令移动到 FROM swift:5.3-focal-slim 之后具有相同的结果,没有 cron 容器。我还有什么遗漏的吗?
    • docker-compose logs cron 的输出是什么?
    • 在我的应用程序基础文件夹中运行它只会显示attaching to
    • 请使用docker-compose up -d --build 重建 Dockerfile。 docker镜像还是旧的,也就是说第一阶段安装的cron,别忘了最后阶段的apt-get install -y cron
    • 做到了!清理构建是关键部分。谢谢。
    猜你喜欢
    • 2022-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-07
    相关资源
    最近更新 更多