pythondockercondaquarthypercorn

Cleaner Dockerfile for continuumio/miniconda3 environment?


I have a Python3.9 / Quart / Hypercorn microservice which runs in a conda environment configured with an environment.yml file. The base image is continuumio/miniconda3.

It took a lot of hacks to get this launching because of conda init issues etc.

Is there a cleaner way to get a conda environment installed and activated within Docker without having to resort to conda run commands and override the default SHELL commands?

FROM continuumio/miniconda3

COPY . /api/

WORKDIR /api/src

# See this tutorial for details https://pythonspeed.com/articles/activate-conda-dockerfile/
RUN conda env create -f /api/conda_environment_production.yml
SHELL ["conda", "run", "-n", "ms-amazing-environment", "/bin/bash", "-c"]
ENTRYPOINT ["conda", "run", "--no-capture-output", "-n", "ms-amazing-environment", "hypercorn", "--bind", "0.0.0.0:5000", "QuartAPI:app"]

EXPOSE 5000

Solution

  • An alternative approach is described here.

    Basically you can activate conda environment within bash script and run you commands there.

    entrypoint.sh:

    #!/bin/bash --login
    # The --login ensures the bash configuration is loaded,
    
    # Temporarily disable strict mode and activate conda:
    set +euo pipefail
    conda activate ms-amazing-environment
    # enable strict mode:
    set -euo pipefail
    
    # exec the final command:
    exec hypercorn --bind 0.0.0.0:5000 QuartAPI:app
    

    Dockerfile:

    FROM continuumio/miniconda3
    
    COPY . /api/
    
    WORKDIR /api/src
    
    # See this tutorial for details https://pythonspeed.com/articles/activate-conda-dockerfile/
    RUN conda env create -f /api/conda_environment_production.yml
    
    # The code to run when container is started:
    COPY entrypoint.sh ./
    ENTRYPOINT ["./entrypoint.sh"]
    
    EXPOSE 5000