首页 > 解决方案 > Dockerfile 不会安装 cron

问题描述

我正在尝试通过我的 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我的 crontabexample-crontab位于我的应用程序文件夹中。Dockerfiledocker-compose.yml

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

标签: dockerdocker-composecrondockerfiledebian

解决方案


在多阶段构建中,只有最后一个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 也需要更新如下:

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

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


推荐阅读