Ahmed Akhtar
Ahmed Akhtar

Reputation: 1463

Error in running Pyomo with CBC solver on Docker

I am trying to run a python application using pyomo with cbc solver.

When I run it in a docker container it is giving the following error:

AttributeError: 'numpy.float64' object has no attribute 'polynomial_degree'

I am inclined to believe that it is linked with Python not being able to find the cbc executable.

Here is my Dockerfile file:

FROM python:3.9-slim

WORKDIR /app

COPY . .

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update && apt-get install -y \
    build-essential \
    gcc \
    libglpk-dev \
    glpk-utils \
    curl \
    git \
    software-properties-common \
    && rm -rf /var/lib/apt/lists/*

RUN python3 -m venv venv \
    && . venv/bin/activate \
    && python3 -m pip install --upgrade pip \
    && pip3 install -r requirements.txt

RUN /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" \
    && (echo; echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"') >> /root/.profile \
    && eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)" \
    && brew install glpk \
    && brew install ipopt \
    && brew install cbc

ENV PATH="/opt/homebrew/opt/cbc/bin:$PATH"
ENV LDFLAGS="-L/opt/homebrew/opt/cbc/lib"
ENV CPPFLAGS="-I/opt/homebrew/opt/cbc/include"
ENV PKG_CONFIG_PATH="/opt/homebrew/opt/cbc/lib/pkgconfig"

ENV PATH="${PATH}:/app/venv/bin:/home/linuxbrew/.linuxbrew/bin:/usr/local/opt/cbc/bin"

EXPOSE 8501

HEALTHCHECK CMD curl --fail http://localhost:8501/_stcore/health

ENTRYPOINT ["streamlit", "run", "app.py", "--server.port=8501", "--server.address=0.0.0.0"]

And here is the requirements.txt file:

numpy==1.24.3
pandas==2.0.2
pyomo==6.6.1
streamlit==1.23.1
streamlit-aggrid==0.3.4.post3

Please let me know how to resolve this.

I have tried executing which cbc from the python code and it finds the executable to cbc

Upvotes: 0

Views: 474

Answers (1)

Ahmed Akhtar
Ahmed Akhtar

Reputation: 1463

I ended up solving this myself.

Notably, there are two things that fixed it, the first being that the virtual environment directory needs to be added at the start of the PATH environment variable instead of at the end of it:

ENV PATH="/app/venv/bin:${PATH}"

Secondly, the altair package was needed in the requirements.txt file along with some changes to the versions of the existing packages.

Here is the fixed requirements.txt file:

Pyomo==6.5.0
streamlit==1.18.1
streamlit-aggrid==0.3.4.post3
altair==4.2.2

The LDFLAGS, CPPFLAGS and PKG_CONFIG_PATH environment variables don't need to be set in the Dockerfile.

Also, symbolic links to cbc, ipopt and glpsol are created by homebrew inside the directory /home/linuxbrew/.linuxbrew/bin, so we need to add that directory to the path.

ENV PATH="$PATH:/home/linuxbrew/.linuxbrew/bin"

Here is the fixed Dockerfile:

FROM python:3.9-slim

WORKDIR /app

COPY . .

RUN python -m venv venv

ENV PATH="/app/venv/bin:${PATH}"

RUN . venv/bin/activate \
    && python -m pip install --upgrade pip \
    && pip install -r requirements.txt

RUN apt-get update \
    && apt-get -y install curl git gcc \
    && /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" \
    && (echo; echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"') >> /root/.profile \
    && eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)" \
    && brew install glpk \
    && brew install ipopt \
    && brew install cbc

ENV PATH="$PATH:/home/linuxbrew/.linuxbrew/bin"

EXPOSE 8501

HEALTHCHECK CMD curl --fail http://localhost:8501/_stcore/health

ENTRYPOINT ["streamlit", "run", "app.py", "--server.port=8501", "--server.address=0.0.0.0"]

Upvotes: 1

Related Questions