도커와 크론탭(crontab)

hyuckhoon.ko·2021년 7월 8일
1

What I learned in first year

목록 보기
90/146

❶ 디렉토리 구조

  • Dockerfile
  • app
  • crontab
  • my_script.py
  • requirements.txt
  • README.md

❷ Dockerfile

FROM python:3.9.2-buster

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
RUN apt-get update && apt-get install -y --no-install-recommends \
    vim \
    cron \
    && rm -rf /var/lib/apt/lists/*
WORKDIR /app
COPY requirements.txt .
RUN pip install --upgrade pip && pip install --no-cache-dir -r requirements.txt
COPY app/ .
COPY crontab /etc/cron.d/crontab
COPY rebonz.py /app/rebonz.py
RUN chmod 0744 /etc/cron.d/crontab
RUN /usr/bin/crontab /etc/cron.d/crontab

# run crond as main process of container
CMD ["cron", "-f"]

❸ crontab

#run python script every 4 hours
0 */4 * * * /usr/local/bin/python /app/my_script.py > /proc/1/fd/1 2>/proc/1/fd/2

❹ 에러 발생

Python import error with cronjob in a docker container

기존 코드

# crontab 스크립트
#run python script every 4 hours
0 */4 * * * python /app/my_script.py > /proc/1/fd/1 2>/proc/1/fd/2

변경 코드

# crontab 스크립트
#run python script every 4 hours
0 */4 * * * /usr/local/bin/python /app/my_script.py > /proc/1/fd/1 2>/proc/1/fd/2

❺ 참고자료

1개의 댓글

감사합니다!

답글 달기