Search code examples
pythondockerflaskdockerfileconda

Micromamba and Dockerfile error: /bin/bash: activate: No such file or directory


Used to have a Dockerfile with conda and flask that worked locally but after installing pytorch had to switch to micromamba, the old CMD no longer works after switching to image mambaorg/micromamba:1-focal-cuda-11.7.1, this is how my Dockerfile looks like right now:

FROM mambaorg/micromamba:1-focal-cuda-11.7.1

WORKDIR /app

COPY environment.yml .

RUN micromamba env create --file environment.yml

EXPOSE 5001

COPY app.py .

CMD ["/bin/bash", "-c", "source activate env-app && flask run --host=0.0.0.0 --port=5001"]

Now I get error:

/bin/bash: activate: No such file or directory

but before when I was using conda it was working, I think the CMD command needs to be updated.


Solution

  • Assuming these 2 files:

    environment.yml

    name: testenv
    channels:
      - conda-forge
    dependencies:
      - python >= 3.9
      - flask
    

    app.py

    from flask import Flask
    
    # Create a Flask application
    app = Flask(__name__)
    
    
    # Define a route for the root URL
    @app.route("/")
    def hello_world():
        return "Hello, World!"
    
    
    if __name__ == "__main__":
        # Run the Flask app on the local development server
        app.run()
    

    Then in your Dockerfile you don't activate the environmnent but use the path to environment, e.g. /opt/conda/envs/testenv/bin/flask (note the testenv in the path and in the environment.yaml):

    Dockerfile

    FROM mambaorg/micromamba:1-focal-cuda-11.7.1
    
    WORKDIR /app
    
    COPY environment.yml .
    
    RUN micromamba env create --file environment.yml
    
    EXPOSE 5001
    
    COPY app.py .
    
    CMD micromamba run -n testenv flask run --host=0.0.0.0 --port=5001
    

    To build:

    docker build . -f Dockerfile -t my/image:name
    

    To run:

    docker run --rm -i -t -p 5001:5001 my/image:name
    

    Then navigating the browser to localhost:5001 you should see Hello World.