如何在后台安排工作并在Docker容器中启动另一个过程?

发布于 2025-01-23 23:13:43 字数 609 浏览 4 评论 0原文

我的问题非常接近其他问题,例如 and 这个使用cron无限循环在Docker容器中安排单个作业/过程。

两种方法都对我有用,但是我的问题有些不同:我想

  1. 在后台安排一个工作/过程
  2. ,然后开始另一个过程。

在我的现实世界中,问题

  1. etl流程
  2. django实例(WebSever)。

我该如何以干净的方式做到这一点?

欢迎任何提示!

My question is quite close to other questions such as this one and this one which use cron or an infinite loop to schedule single a job/process inside a docker container.

Both approaches work for me but my problem is a bit different: I would like to

  1. schedule a job/process in the background
  2. and subsequently start another process.

In my real world problem:

  1. is an ETL process and
  2. is a Django instance (websever).

How can I do this in a clean way?

Any hints are welcome!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最终幸福 2025-01-30 23:13:43

我基于以下文章

基本上,它基本上覆盖了另一个服务中的入口点,如docker-compose.yml文件:

version: "3"

services:
  app:
    image: demo-image:latest
    volumes:
      - data:/app-data
  cron:
    image: demo-image:latest
    command: [ "cron -f" ]
    tty: true
    volumes:
      - data:/app-data

volumes:
  data:

我的示例dockerfile

# syntax=docker/dockerfile:experimental
FROM python:3.9

RUN apt-get update
RUN apt-get -y install cron
COPY my-crontab /etc/cron.d/my-crontab
RUN chmod 0744 /etc/cron.d/my-crontab
RUN crontab -l | { cat; cat /etc/cron.d/my-crontab } | crontab -
RUN touch /var/log/cron.log
WORKDIR /code
COPY . /code
ENTRYPOINT ["/bin/bash", "/docker-entrypoint.sh"]

我的示例cronjob cronjob 带有一个带有一个重要的提示花了我数小时的错误跟踪:

* * * * * echo "Hello world" >> /var/log/cron.log 2>&1
# must be ended with a new line "LF" (Unix) and not "CRLF" (Windows)

我发现该解决方案更加清洁,因为它仅使用一个每个容器的过程。

I found a solution using docker-compose based on the following article.

It basically overrides the entrypoint in another service as follows in the docker-compose.yml file:

version: "3"

services:
  app:
    image: demo-image:latest
    volumes:
      - data:/app-data
  cron:
    image: demo-image:latest
    command: [ "cron -f" ]
    tty: true
    volumes:
      - data:/app-data

volumes:
  data:

My example Dockerfile:

# syntax=docker/dockerfile:experimental
FROM python:3.9

RUN apt-get update
RUN apt-get -y install cron
COPY my-crontab /etc/cron.d/my-crontab
RUN chmod 0744 /etc/cron.d/my-crontab
RUN crontab -l | { cat; cat /etc/cron.d/my-crontab } | crontab -
RUN touch /var/log/cron.log
WORKDIR /code
COPY . /code
ENTRYPOINT ["/bin/bash", "/docker-entrypoint.sh"]

My example cronjob file with an important hint that took me hours of bugtracking:

* * * * * echo "Hello world" >> /var/log/cron.log 2>&1
# must be ended with a new line "LF" (Unix) and not "CRLF" (Windows)

I found this solution much cleaner because it only uses one process per container.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文