pythondockerconda

pip dependencies from env yaml file missed during building docker container


I have the following conda environment YAML file :

name: someapp
channels:
  - conda-forge
  - defaults
dependencies:
  - flask=2.2.2
  - pandas=1.5.3
  - pip=23.0.1
  - python=3.11.3
  - pip:
      - psycopg2-binary==2.9.9
      - requests==2.32.3
prefix: /usr/local/anaconda3/envs/someapp

And I would like this env to be created when building my docker container (for a Flask app) but I have a ModuleNotFoundError: No module named 'requests' error when launching the server.py file where I import requests with the following DockerFile :

FROM continuumio/miniconda3

RUN apt-get update && \
    apt-get install --yes --no-install-recommends \
    && apt-get clean

WORKDIR /app/services/back_end/

COPY ./environment.yml ./environment_server.yml
RUN conda env create -f ./environment_server.yml

# added line to try to install but doesn't work
RUN /bin/bash -c "source activate someapp && pip install requests" 

RUN echo "source activate someapp" > ~/.bashrc
ENV PATH /opt/conda/envs/someapp/bin:$PATH

EXPOSE 6969

ENTRYPOINT ["python", "server.py"]

Solution

  • To run conda with your environment you should use conda command, but in your case you should change entrypoint for the container.

    It should looks like

    ENTRYPOINT ["conda", "run", "--no-capture-output", "-n", "someapp", "python", "server.py"]
    

    Check the command spec here