【发布时间】: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 位于我的应用程序文件夹中,其中 Dockerfile 和 docker-compose.yml 存在。
为什么我的 cron 容器无法启动?
【问题讨论】:
标签: docker docker-compose cron dockerfile debian