pythondockerfacebook-prophet

How to build docker image with Facebook prophet library


I am trying to build a docker image with Prophet 1.0.1

Here is my requirements.txt file

google-cloud-bigquery
google-cloud-storage
numpy==1.21.0
pandas==1.2.0
db-dtypes
plotly==5.10.0
hampel==0.0.5
prophet==1.0.1
click
joblib
scikit-learn

and here is my docker file -

FROM python:3.8-slim-buster as base
RUN mkdir my_dir
COPY requirements.txt /my_dir/requirements.txt
COPY src /my_dir/src

WORKDIR /my_dir

RUN apt update
RUN apt-get install -y --no-install-recommends \
    ca-certificates \
    cmake \
    build-essential \
    gcc \
    g++ \
    git \
    curl \
    apt-transport-https \
    ca-certificates \
    gnupg

RUN python -m pip install --no-cache-dir --upgrade setuptools pip wheel pipenv
RUN pip install -r requirements.txt
WORKDIR /my_dir/src
RUN python setup.py bdist_wheel
RUN pip install dist/*

# kubectl
RUN curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
RUN chmod +x ./kubectl
RUN mv ./kubectl /usr/local/bin

ENTRYPOINT [ "sh", "run.sh" ]

When I try to run docker-compose up -d command, the build fails locally with below error -

13 25.10 Building wheels for collected packages: prophet, pymeeus
#13 25.10   Building wheel for prophet (setup.py): started
#13 25.40   Building wheel for prophet (setup.py): finished with status 'error'
#13 25.41   error: subprocess-exited-with-error
#13 25.41   
#13 25.41   × python setup.py bdist_wheel did not run successfully.
#13 25.41   │ exit code: 1
#13 25.41   ╰─> [49 lines of output]
#13 25.41       running bdist_wheel
#13 25.41       running build
#13 25.41       running build_py
#13 25.41       creating build
#13 25.41       creating build/lib
#13 25.41       creating build/lib/prophet
#13 25.41       creating build/lib/prophet/stan_model
#13 25.41       Traceback (most recent call last):
#13 25.41         File "<string>", line 36, in <module>
#13 25.41         File "<pip-setuptools-caller>", line 34, in <module>
#13 25.41         File "/tmp/pip-install-l_l1ynvx/prophet_2f0894fc2ee74edfa49399ff3fb20863/setup.py", line 150, in <module>
#13 25.41           long_description_content_type='text/markdown',
#13 25.41         File "/usr/local/lib/python3.7/site-packages/setuptools/__init__.py", line 108, in setup
#13 25.41           return distutils.core.setup(**attrs)
#13 25.41         File "/usr/local/lib/python3.7/site-packages/setuptools/_distutils/core.py", line 185, in setup
#13 25.41           return run_commands(dist)
#13 25.41         File "/usr/local/lib/python3.7/site-packages/setuptools/_distutils/core.py", line 201, in run_commands
#13 25.41           dist.run_commands()
#13 25.41         File "/usr/local/lib/python3.7/site-packages/setuptools/_distutils/dist.py", line 969, in run_commands
#13 25.41           self.run_command(cmd)
#13 25.41         File "/usr/local/lib/python3.7/site-packages/setuptools/dist.py", line 1221, in run_command
#13 25.41           super().run_command(command)
#13 25.41         File "/usr/local/lib/python3.7/site-packages/setuptools/_distutils/dist.py", line 988, in run_command
#13 25.41           cmd_obj.run()
#13 25.41         File "/usr/local/lib/python3.7/site-packages/wheel/bdist_wheel.py", line 343, in run
#13 25.41           self.run_command("build")
#13 25.41         File "/usr/local/lib/python3.7/site-packages/setuptools/_distutils/cmd.py", line 318, in run_command
#13 25.41           self.distribution.run_command(command)
#13 25.41         File "/usr/local/lib/python3.7/site-packages/setuptools/dist.py", line 1221, in run_command
#13 25.41           super().run_command(command)
#13 25.41         File "/usr/local/lib/python3.7/site-packages/setuptools/_distutils/dist.py", line 988, in run_command
#13 25.41           cmd_obj.run()
#13 25.41         File "/usr/local/lib/python3.7/site-packages/setuptools/_distutils/command/build.py", line 131, in run
#13 25.41           self.run_command(cmd_name)
#13 25.41         File "/usr/local/lib/python3.7/site-packages/setuptools/_distutils/cmd.py", line 318, in run_command
#13 25.41           self.distribution.run_command(command)
#13 25.41         File "/usr/local/lib/python3.7/site-packages/setuptools/dist.py", line 1221, in run_command
#13 25.41           super().run_command(command)
#13 25.41         File "/usr/local/lib/python3.7/site-packages/setuptools/_distutils/dist.py", line 988, in run_command
#13 25.41           cmd_obj.run()
#13 25.41         File "/tmp/pip-install-l_l1ynvx/prophet_2f0894fc2ee74edfa49399ff3fb20863/setup.py", line 48, in run
#13 25.41           build_models(target_dir)
#13 25.41         File "/tmp/pip-install-l_l1ynvx/prophet_2f0894fc2ee74edfa49399ff3fb20863/setup.py", line 36, in build_models
#13 25.41           from prophet.models import StanBackendEnum
#13 25.41         File "/tmp/pip-install-l_l1ynvx/prophet_2f0894fc2ee74edfa49399ff3fb20863/prophet/__init__.py", line 8, in <module>
#13 25.41           from prophet.forecaster import Prophet
#13 25.41         File "/tmp/pip-install-l_l1ynvx/prophet_2f0894fc2ee74edfa49399ff3fb20863/prophet/forecaster.py", line 14, in <module>
#13 25.41           import numpy as np
#13 25.41       ModuleNotFoundError: No module named 'numpy'
#13 25.41       [end of output]

I do have the numpy package included in the requirements.txt but it does not seem to work for me. How can I successfully install the prophet 1.0.1 using docker?


Solution

  • prophet 1.0.1 seems to need some of its dependencies before the package can been built, so you'll need to install those first. You can change your requirements.txt to

    google-cloud-bigquery
    google-cloud-storage
    numpy==1.21.0
    pandas==1.2.0
    db-dtypes
    plotly==5.10.0
    hampel==0.0.5
    click
    joblib
    scikit-learn
    pystan==2.19.1.1
    Cython
    cmdstanpy==0.9.68
    matplotlib
    LunarCalendar
    convertdate
    holidays
    setuptools-git
    python-dateutil
    tqdm
    

    (Note that prophet==1.0.1 has been removed here.) And then change your Dockerfile to

    FROM python:3.8-slim-buster as base
    RUN mkdir my_dir
    COPY src /my_dir/src
    
    WORKDIR /my_dir
    
    RUN apt update
    RUN apt-get install -y --no-install-recommends \
        ca-certificates \
        cmake \
        build-essential \
        gcc \
        g++ \
        git \
        curl \
        apt-transport-https \
        ca-certificates \
        gnupg
    
    COPY requirements.txt /my_dir/requirements.txt
    
    RUN python -m pip install --no-cache-dir --upgrade setuptools pip wheel pipenv
    RUN pip install -r requirements.txt
    RUN pip install prophet==1.0.1
    WORKDIR /my_dir/src
    RUN python setup.py bdist_wheel
    RUN pip install dist/*
    
    # kubectl
    RUN curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
    RUN chmod +x ./kubectl
    RUN mv ./kubectl /usr/local/bin
    
    ENTRYPOINT [ "sh", "run.sh" ]
    

    Alternatively, you could switch to a newer version of prophet that doesn't have this problem.