Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python import error with cronjob in a docker container

I have a python script that runs every minute with cronjob in a docker container Here is my Dockerfile

FROM python:3.8.3 AS base

RUN apt-get update
RUN apt-get -y install software-properties-common cron vim
RUN apt-get update
RUN apt-get -y install python3-pip
RUN pip3 install pandas
RUN pip3 install sklearn
RUN pip3 install SQLAlchemy
RUN pip3 install ConfigParser
RUN pip3 install psycopg2
RUN pip3 install numpy
RUN pip3 install xgboost
RUN pip3 install xlrd
RUN pip3 install matplotlib

FROM base AS publish

WORKDIR /app
COPY . /app
COPY crontab /etc/cron.d/crontab
COPY main.py /app/main.py
RUN chmod 0644 /etc/cron.d/crontab
RUN /usr/bin/crontab /etc/cron.d/crontab

CMD ["cron", "-f"]

Here is my crontab file

#run python script every minutes
* * * * * python /app/main.py > /proc/1/fd/1 2>/proc/1/fd/2

This is the beginning of my code:

import os
print("Code is running")
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pandas import DataFrame
from pandas import concat
from calendar import monthrange
from datetime import date
import pickle
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy import Column, String, Integer, LargeBinary
from my_ml_library.regressor import Regressor
.......

When i build an image from this dockerfile and run it, the script prints in every minute:

Code is running Traceback (most recent call last): File "/app/main.py", line 3, in import numpy as np ImportError: No module named numpy

On the other hand, if i run my code in a docker container without using cron, it works correctly. How could i run my code correctly?

like image 707
Fatih Enes Avatar asked Sep 26 '26 01:09

Fatih Enes


1 Answers

You need to speicfy the absoulte path for Python installation in the crontab file.

#run python script every minutes
* * * * * /usr/local/bin/python /app/main.py > /proc/1/fd/1 2>/proc/1/fd/2

You can find Dockerfile here.

enter image description here

main.py

import numpy as np
a = np.arange(15).reshape(3, 5)
print(a)
like image 162
Adiii Avatar answered Sep 28 '26 15:09

Adiii



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!