ドッカーコンテナのcrontab

ドッカーコンテナのcrontab

こんにちは。私はDockerコンテナでcronジョブを実行しようとしています。だから私は私の中にいます。Dockerfile

私のものDockerfile

FROM nginx:stable

RUN apt-get update -y && apt-get upgrade -y && apt-get install -y \
    vim \
    git \
    curl \
    wget \
    certbot \
    cron

COPY cron/crontab /etc/crontab
RUN chmod 0644 /etc/cron.d/crontab
RUN /etc/init.d/cron start 

私のcrontabプロフィール

# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user command
*/1 *   *   *   * root echo "test" >>~/readme

しかし、これはうまくいきません。

機能するには、/etc/init.d/cron startnginxコンテナでコマンドを手動で実行する必要があります。

Dockerfileそのため、コンテナの起動時にこのコマンドを実行できるように、myにエントリポイントを追加しました。

# ENTRYPOINT
ADD entrypoint.sh /entrypoint.sh
RUN chmod 777 /entrypoint.sh

私のものentrypoint.sh

#!/usr/bin/env bash

/etc/init.d/cron start

私のものdocker-compose

entrypoint: /entrypoint.sh

しかし、次のエラーが発生しました。

OCIランタイム実行に失敗しました:実行に失敗しました:container_linux.go:296:コンテナプロセスを起動すると、「process_linux.go:86:setnsプロセスを実行すると「終了ステータス21」が発生します。」:不明

私が逃したものは何ですか?

PS:私はこれに従った。地図時間

ベストアンサー1

私は数日前に同様の問題で苦しんでいました。

  • サービスなどのコンテナを実行するには、どこか(cmdまたはエントリポイント)に実行中のフォアグラウンドプログラムが必要です。あなたの場合は(nginx画像から)ですnginx -g daemon off;

  • また、エントリポイントとcmdがある場合は、エントリポイントの引数としてcmdが渡されて開始されます(と同様に./entrypoint.sh [cmd])。

  • RUN /etc/init.d/cron startイメージがビルドされた後はとにかく終了するため、意味がありません。

解決策:使用中です。エントリポイント.sh次のようなもの(私の場合はdjango/gunicorn/cron):

#!/bin/bash

set -e # exit on any error

if [ "$1" = './gunicorn.sh' ]; then  # './gunicorn.sh' is default command
  cron                               # this runs cron as daemon
  crontab -u django /app/crontab     # this applies a crontab from a file
  exec su django -c "$@"             # this actually runs the default command as different user (you can just use exec "$@" if you dont need to run it as different user)
fi

exec "$@" # this runs any other cmd without starting cron (e.g. docker run -ti myimage /bin/bash)

おすすめ記事