Docker不能同时运行cron和Rails

我已经安装了whenever使用gem的cron任务,所有的crontab工作正常。 但是,当docker启动时,它不启动cron服务。 我需要进入容器,并运行cron来运行该服务。 我跟着其他问题和他们的指示,但他们似乎并没有为我工作。

Dockerfile

 FROM ruby:2.3.3 # setup /app as our working directory ENV RAILS_ROOT /app RUN mkdir -p $RAILS_ROOT # Set working directory, where the commands will be ran: WORKDIR $RAILS_ROOT # Replace shell with bash so we can source files RUN rm /bin/sh && ln -s /bin/bash /bin/sh # Set debconf to run non-interactively RUN echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections # Install base dependencies RUN apt-get update && apt-get install -y -q --no-install-recommends \ apt-transport-https \ build-essential \ ca-certificates \ curl \ git \ libssl-dev \ python \ rsync \ software-properties-common \ wget \ wget ca-certificates \ cron \ && rm -rf /var/lib/apt/lists/* # Installing the Postgres - This specific version so it doesn't throw the version mismatch when we do `pg_dump` RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" | tee /etc/apt/sources.list.d/postgres.list RUN wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - RUN apt-get update RUN apt-get install --assume-yes postgresql-9.6 # Install node and npm with nvm RUN curl https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | bash ENV NVM_DIR=/root/.nvm ENV NODE_VERSION v7.2.1 ENV NODE_PATH $NVM_DIR/versions/node/$NODE_VERSION ENV PATH $NODE_PATH/bin:./node_modules/.bin:$PATH RUN source $NVM_DIR/nvm.sh \ && nvm install $NODE_VERSION \ && nvm alias default $NODE_VERSION \ && nvm use default # Install our ruby dependencies ADD Gemfile Gemfile.lock $RAILS_ROOT/ RUN bundle install # Install the cronjob logger RUN touch /var/log/swscd-cron.log # copy the rest of our code over ADD . $RAILS_ROOT ENV SECRET_KEY_BASE a6bdc5f788624f00b68ff82456d94bf81bb50c2e114b2be19af2e6a9b76f9307b11d05af4093395b0471c4141b3cd638356f888e90080f8ae60710f992beba8f RUN bundle exec whenever --update-crontab RUN bundle exec rake assets:precompile # Expose port 3000 to the Docker host, so we can access it from the outside. EXPOSE 3000 # Set the default command to run our server on port 3000 CMD cron && rails s -p 3000 -b 0.0.0.0 

我无法使用以下configuration重现您的问题:

Dockerfile:

 FROM ruby:2.3.3 RUN apt-get update && apt-get install -y cron CMD cron && ruby -run -e httpd . -p 9090 

然后: docker build -t test . build立形象

然后: docker run -it test来启动镜像,我看到:

 [2017-11-27 19:27:03] INFO WEBrick 1.3.1 [2017-11-27 19:27:03] INFO ruby 2.3.3 (2016-11-21) [x86_64-linux] [2017-11-27 19:27:03] INFO WEBrick::HTTPServer#start: pid=9 port=9090 

这意味着简单的http服务器正在运行然后我做: docker exec -it <containerId> bash进入该容器

然后我input: ps aux查看容器内正在运行的进程,在那里我看到:

 root 1 0.0 0.0 4332 752 pts/0 Ss+ 19:27 0:00 /bin/sh -c cron && ruby -run -e httpd . -p 9090 root 8 0.0 0.0 25900 2172 ? Ss 19:27 0:00 cron root 9 0.5 0.0 72156 14608 pts/0 Sl+ 19:27 0:00 ruby -run -e httpd . -p 9090 root 11 1.0 0.0 20240 3284 pts/1 Ss 19:27 0:00 bash root 17 0.0 0.0 17496 2120 pts/1 R+ 19:27 0:00 ps aux 

我也input了命令apt-get update && apt-get install vim ,然后我做了crontab -e并进入: * * * * * echo "foo" >> /root/bar.txt

一分钟后我看到:

 root@5ae6284ae2e8:~# cat bar.txt foo 

所以一切看起来都很好,在Docker的Cron

同样在容器curl localhost:9090正在返回HTML所以它的工作原理。