简体   繁体   English

如何在Rails docker容器中以非root用户身份执行cron任务?

[英]How can I execute cron task as a non-root user within the rails docker container?

I'm trying to use cron as a non-root user in the docker container. 我正在尝试使用cron作为docker容器中的非root用户。 The container already has rails application by the non-root user and I want to execute cron tasks as the same user while getting rails server running at the same time. 容器已经由非root用户拥有Rails应用程序,我想以同一用户身份执行cron任务,同时使Rails服务器同时运行。 However, when I run docker-compose up -d I get an error saying cron: can't open or create /var/run/crond.pid: Permission denied 但是,当我运行docker-compose up -d ,出现错误消息cron:无法打开或创建/var/run/crond.pid:权限被拒绝

So my question is if there is any way to execute cron task as non-root user in the container that has rails application. 所以我的问题是,在具有Rails应用程序的容器中,是否可以以非root用户身份执行cron任务。 Just so you know I tried not to use root account for a security reason. 请注意,出于安全原因,我尝试不使用root帐户。 Thank you in advance! 先感谢您!

Here is my dockerfile bedow ... 这是我的泊坞窗文件...

FROM ruby:2.5

RUN apt-get update -qq && apt-get install -y --no-install-recommends \
    build-essential \
    libpq-dev \
    nodejs \
    cron \
    && rm -rf /var/lib/apt/lists/* \
    && apt-get clean

ENV \
 USER=new_user \
 GROUP=new_user \
 APP_ROOT=/var/www/new_user \
 HOME=/home/new_user \
 RAILS_PATH=./apps/server/new_user

RUN groupadd $GROUP && \
 useradd -g $GROUP $USER

RUN mkdir -p $APP_ROOT && \
 mkdir -p $HOME

WORKDIR $APP_ROOT

COPY $RAILS_PATH/Gemfile .
COPY $RAILS_PATH/Gemfile.lock .
COPY $RAILS_PATH/config/puma.rb config/puma.rb
COPY $RAILS_PATH/config/schedule.rb config/schedule.rb

RUN \
 gem update --system && \
 gem install bundler && \
 bundle config --global build.nokogiri --use-system-libraries && \
 bundle config --global jobs 2 && \
 bundle install --path=vendor/bundler

RUN \
 chown -R $USER $HOME && \
 chown -R $USER $APP_ROOT && \
 chgrp -R $GROUP $APP_ROOT && \
 chgrp -R $GROUP $APP_ROOT

USER $USER

RUN bundle exec whenever --update-crontab

CMD cron -f && bundle exec puma -C config/puma.rb

... ...

You could use the gem arask instead of whenever. 您可以随时使用gem arask It runs tasks within Rails, so the user is the same. 它在Rails中运行任务,因此用户是相同的。 Just reference your tasks from config/initializers/arask.rb instead and forget about running cron. 只需从config/initializers/arask.rb引用您的任务,而不必运行cron。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在非root用户上下文中运行Rails应用程序? - How can I run my Rails application in a non-root context? 在 Gitlab CI 中以非 root 用户身份运行 Elasticsearch - Running Elasticsearch within Gitlab CI as non-root user 无法使用cron执行Rails rake任务 - Can't execute Rails rake task with cron 如何在没有root用户的情况下在rails上运行ruby的docker容器? - How to run a docker container for ruby on rails without root user? 以非root用户身份运行Rails应用,但要求输入root密码才能访问文件 - Run Rails app as non-root user, but ask for root password to access files Phusion 乘客以 root 身份运行,非 root 用户可以更改乘客根路径 () 的一部分 - Phusion Passenger is running as root, and part(s) of the Passenger root path () can be changed by non-root user(s) Docker,Rails和cron任务:如何设置Docker容器以运行cron Rails rake任务 - Docker, Rails and a cron tasks: how to set up a Docker container to run cron Rails rake tasks 如何通过Rails上的用户交互在heroku上运行“ cron脚本”? - How can I run a “cron script” on heroku by user interaction on Rails? 非特权、非 root 用户启动或重启 web 服务器服务器,例如 nginx,无需 root 或 sudo - Non-privileged, non-root, user to start or restart webserver server such as nginx without root or sudo 将rails应用程序部署到非root上下文 - restful_auth无法正常工作 - Deploy rails app to non-root context - restful_auth not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM