我想在docker(aipine)和每当宝石的帮助下做一个cron-乔布

发布于 2025-01-26 11:25:24 字数 2552 浏览 2 评论 0原文

使用docker-compose a Asumption

i m,并想在gem 进行进行cron过程中使用rails,但是在研究中,我发现我必须在docker中安装和运行cron。因此,我研究了它,但是我找不到有关Alpine有关Cron处理的任何信息。可以 Yone告诉我该怎么做?

我们已经实现的目标

是我每天执行一次特定的过程。

代码

是我的dockerfile:


FROM ruby:2.7.1-alpine

ARG WORKDIR

ENV RUNTIME_PACKAGES="linux-headers libxml2-dev make gcc libc-dev nodejs tzdata postgresql-dev postgresql git" \
    DEV_PACKAGES="build-base curl-dev" \
    HOME=/${WORKDIR} \
    LANG=C.UTF-8 \
    TZ=Asia/Tokyo

RUN echo ${HOME}

WORKDIR ${HOME}

COPY Gemfile* ./

RUN apk update && \ 
    apk upgrade && \
    apk add --no-cache ${RUNTIME_PACKAGES} && \
    apk add --virtual build-dependencies --no-cache ${DEV_PACKAGES} && \
    bundle install -j4 && \
    apk del build-dependencies

COPY . .

CMD ["rails", "server", "-b", "0.0.0.0"]

这是我的docker撰写文件:

version: '3.8'

services:
  db:
    image: postgres:12.3-alpine
    environment:
      TZ: UTC
      PGTZ: UTC
      POSTGRES_PASSWORD: $POSTGRES_PASSWORD
    volumes:
      - ./api/tmp/db:/var/lib/postgresql/data
  

  api:
    build:
      context: ./api
      args:
        WORKDIR: $WORKDIR
    command: /bin/sh -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    environment:
      POSTGRES_PASSWORD: $POSTGRES_PASSWORD
      API_DOMAIN: "localhost:$FRONT_PORT"
      APP_URL: "http://localhost:$API_PORT"
    volumes:
      - ./api:/$WORKDIR
    depends_on:
      - db
    ports:
      - "$API_PORT:$CONTAINER_PORT"
  mailcatcher:
    image: schickling/mailcatcher
    ports:
      - "1080:1080"
      - "1025:1025"

  front:
    build:
      context: ./front
      args:
        WORKDIR: $WORKDIR
        CONTAINER_PORT: $CONTAINER_PORT
        API_URL: "http://localhost:$API_PORT"
    command: yarn run dev
    volumes:
      - ./front:/$WORKDIR
    ports:
      - "$FRONT_PORT:$CONTAINER_PORT"
    depends_on:
      - api

实际处理

/config/schedule.rb


require File.expand_path(File.dirname(__FILE__) + "/environment")
ENV.each { |k, v| env(k, v) } 

set :output, "#{Rails.root}/log/cron.log"
set :environment, :development

every 1.days do
  runner "User.guest_reset"
end

我们尝试了

很多研究,并找到了有关使用cron with 的大量信息。 APT,但找不到有关使用apk的任何信息。

Aasumption

I m using docker-compose and wanted to use the whenever gem to do a cron process that deletes at a certain time in Rails, but upon research I found that I have to install and run cron in docker. So I looked into it, but I can't find any information about Alpine regarding cron processing in Rails. Can an
yone tell me how to do this?

What we have achieved

I want to execute a specific process once a day.

Code

Here is my Dockerfile:


FROM ruby:2.7.1-alpine

ARG WORKDIR

ENV RUNTIME_PACKAGES="linux-headers libxml2-dev make gcc libc-dev nodejs tzdata postgresql-dev postgresql git" \
    DEV_PACKAGES="build-base curl-dev" \
    HOME=/${WORKDIR} \
    LANG=C.UTF-8 \
    TZ=Asia/Tokyo

RUN echo ${HOME}

WORKDIR ${HOME}

COPY Gemfile* ./

RUN apk update && \ 
    apk upgrade && \
    apk add --no-cache ${RUNTIME_PACKAGES} && \
    apk add --virtual build-dependencies --no-cache ${DEV_PACKAGES} && \
    bundle install -j4 && \
    apk del build-dependencies

COPY . .

CMD ["rails", "server", "-b", "0.0.0.0"]

Here is my Docker Compose file:

version: '3.8'

services:
  db:
    image: postgres:12.3-alpine
    environment:
      TZ: UTC
      PGTZ: UTC
      POSTGRES_PASSWORD: $POSTGRES_PASSWORD
    volumes:
      - ./api/tmp/db:/var/lib/postgresql/data
  

  api:
    build:
      context: ./api
      args:
        WORKDIR: $WORKDIR
    command: /bin/sh -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    environment:
      POSTGRES_PASSWORD: $POSTGRES_PASSWORD
      API_DOMAIN: "localhost:$FRONT_PORT"
      APP_URL: "http://localhost:$API_PORT"
    volumes:
      - ./api:/$WORKDIR
    depends_on:
      - db
    ports:
      - "$API_PORT:$CONTAINER_PORT"
  mailcatcher:
    image: schickling/mailcatcher
    ports:
      - "1080:1080"
      - "1025:1025"

  front:
    build:
      context: ./front
      args:
        WORKDIR: $WORKDIR
        CONTAINER_PORT: $CONTAINER_PORT
        API_URL: "http://localhost:$API_PORT"
    command: yarn run dev
    volumes:
      - ./front:/$WORKDIR
    ports:
      - "$FRONT_PORT:$CONTAINER_PORT"
    depends_on:
      - api

Actual processing

/config/schedule.rb


require File.expand_path(File.dirname(__FILE__) + "/environment")
ENV.each { |k, v| env(k, v) } 

set :output, "#{Rails.root}/log/cron.log"
set :environment, :development

every 1.days do
  runner "User.guest_reset"
end

What we tried

I did a lot of research and found a lot of information on using cron with apt, but could not find any information on using apk.

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

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

发布评论

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

评论(1

雪花飘飘的天空 2025-02-02 11:25:24

通过使用与您的Rails App Image(由DockerFile构建的),将cron分为docker-compose.yml中的另一个服务。然后在命令中以 cron 和在命令中。

docker-compose.yml

version: '3'
services:
  app:
    image: myapp
    depends_on:
      - 'db'
    build:
      context: .
    command: bash -c "rm -f tmp/pids/server.pid && 
      bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - ".:/myapp"

  cron:
    image: myapp
    command: bash -c "touch log/cron.log && cron && whenever --update-crontab &&
      crontab -l && tail -f log/cron.log"
    volumes:
      - '.:/myapp'

  db:
    image: postgres:13
    ports: # 127.0.0.1 to only expose the port to loopback
      - '127.0.0.1:5432:5432'
    volumes:
      - 'postgres_dev:/var/lib/postgresql/data'

dockerfile

FROM ruby:3.0.1

RUN apt-get update -qq && apt-get install -y postgresql-client cron vim \
  && mkdir /myapp
WORKDIR /myapp

ENV BUNDLE_WITHOUT=development:test

COPY Gemfile Gemfile.lock ./
RUN bundle install --jobs 20 --retry 5

COPY package.json ./
RUN npm install --check-files --production

Separate cron into another service in your docker-compose.yml by using the same image as your rails app image (which built by Dockerfile). Then run cron and whenever --update-crontab in the command.

docker-compose.yml

version: '3'
services:
  app:
    image: myapp
    depends_on:
      - 'db'
    build:
      context: .
    command: bash -c "rm -f tmp/pids/server.pid && 
      bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - ".:/myapp"

  cron:
    image: myapp
    command: bash -c "touch log/cron.log && cron && whenever --update-crontab &&
      crontab -l && tail -f log/cron.log"
    volumes:
      - '.:/myapp'

  db:
    image: postgres:13
    ports: # 127.0.0.1 to only expose the port to loopback
      - '127.0.0.1:5432:5432'
    volumes:
      - 'postgres_dev:/var/lib/postgresql/data'

Dockerfile

FROM ruby:3.0.1

RUN apt-get update -qq && apt-get install -y postgresql-client cron vim \
  && mkdir /myapp
WORKDIR /myapp

ENV BUNDLE_WITHOUT=development:test

COPY Gemfile Gemfile.lock ./
RUN bundle install --jobs 20 --retry 5

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