Mateusz Gebroski
Mateusz Gebroski

Reputation: 1334

Docker Flask Conda - Not a conda environment

I am playing with flask app which uses conda environment. App works fine in local development so I wanted to dockerize it. Building via Dockerfile is successful but when I run app via:

docker run -p 5000:5000 3d-photo

I got:

Not a conda environment: /opt/conda/envs/myenv
  File "app.py", line 18
    def render(filter_name: str):
                          ^
SyntaxError: invalid syntax
ERROR conda.cli.main_run:execute(39): Subprocess for 'conda run [u'python', u'app.py']' command failed.  Stderr was:
Not a conda environment: /opt/conda/envs/myenv
  File "app.py", line 18
    def render(filter_name: str):
                          ^
SyntaxError: invalid syntax

Code which throws error looks like:

@app.route("/render/<filter_name>", methods=["POST"])
def render(filter_name: str):
    if request.method == "POST":
        f = request.files["file"]

        tempdir = tempfile.mkdtemp()
        in_dir = tempfile.mkdtemp(prefix="image_", dir=tempdir)
        out_dir = tempfile.mkdtemp(prefix="image_", dir=tempdir)

        image = Image.open(BytesIO(f.read()))
        image.save(in_dir.name + "/image.jpg", "JPEG")

        render_mp4(in_dir.name, out_dir.name, filter_name)

        filename = "image_" + filter_name + ".mp4"
        fout = open(os.path.join(out_dir.name, filename), "rb")

        response = make_response(fout.read())
        response.headers.set("Content-Type", "video/mp4")
        response.headers.set("Content-Disposition", "attachment", filename=filename)
        shutil.rmtree(tempdir)

        return response

and the Dockerfile:

FROM ubuntu:latest
FROM continuumio/miniconda:latest
RUN apt-get update && apt-get install -y \
    python3-pip \
    python3-dev \
    build-essential
RUN pip3 install auxlib
COPY . /app
WORKDIR /app
RUN conda env create --file exported_conda_env.yml
SHELL ["conda", "run", "-n", "3DPhotoCreator", "/bin/bash", "-c"]
RUN pip install -r requirements.txt
RUN conda install pytorch==1.4.0 torchvision==0.5.0 cudatoolkit==10.1.243 -c pytorch
EXPOSE 5000
CMD ["conda", "run", "-n", "myenv", "python", "app.py"]

Any hints here?

Upvotes: 0

Views: 1219

Answers (2)

Evg
Evg

Reputation: 98

You create environment from file. We don't know which name you've set inside environment.yml.Assuming it's 3DPhotoCreator. You use it to install packages but at the end in CMD you use not this environment which even doesn't exist.Change to

CMD ["conda", "run", "-n", "3DPhotoCreator", "python", "app.py"]

To make more clear which environment is used you can name it during creation from environment.yml

conda env create --name myenv --file exported_conda_env.yml

So now you have myenv which is just renamed environment created from environment.yml (it ignores first string 'name: 3DPhotoCreator' of environment.yml)

Upvotes: 0

Stuart Berg
Stuart Berg

Reputation: 18162

You never created an environment named myenv. It looks like you created an environment named 3DPhotoCreator, but not myenv. It isn't clear what you're trying to do.

By the way, I think there is probably no need to use conda run, unless your dependencies rely on special environment variables being set. (I don't think they do.) You can probably just call the python interpreter without explicitly activating the environment:

CMD /opt/conda/envs/3DPhotoCreator/bin/python app.py

Upvotes: 1

Related Questions