正确的方法是使用Miniconda在Docker上运行Python Flask API,并在本地访问它。

huangapple go评论71阅读模式
英文:

What is the correct way to run a Python Flask API on Docker with Miniconda and access it on localhost?

问题

要访问在本地 Docker 容器上运行的 Flask API,可以尝试使用 http://localhost:5000/api。确保你的 Flask API 在容器中正确运行,而且端口号为 5000。

英文:

How to access Flask API running on localhost docker container?

I created miniconda docker image with Flask API on it.

Dockerfile is:

FROM continuumio/miniconda3

# Install base utilities
RUN apt-get update \
&& apt-get install -y wget \
&& rm -rf /var/lib/apt/lists/*

COPY api /root/api
RUN echo "Running $(conda --version)" 
RUN conda update conda 
RUN conda create -n api python=3.9 
RUN echo "conda activate api" >> ~/.bashrc
SHELL ["/bin/bash", "--login", "-c"]
RUN conda activate api
RUN conda install flask requests 
ENTRYPOINT ["conda", "run", "-n", "api", "python", "/root/api/main.py"]

Flask API uses port 5000. I've tried to add EXPOSE 5000 to dockerfile, but I didn't find any difference.

It builds without error, but I'm still not sure everything is correct. So I run it locally on my PC to test. But I can't access it. I've tested http://172.17.0.2/api and http://localhost/api, but it didn't respond. Also I've tried to run main.py in container terminal, but it says "Port 5000 is in use by another program".

So here is what I would like to ask:

  1. How to properly run Flask API on Linux? Am I doing it right?
  2. How to properly run Flask API in Docker? Am I doing it right?
  3. How to access Flask API running on localhost Docker container (same PC)? I just can't understand what ip/address my API gets.

答案1

得分: 0

这是我找到的最佳解决方案。

Dockerfile:

FROM tensorflow/tensorflow:2.12.0-gpu

# 安装依赖项
RUN pip install tensorflow-hub <whatever else you need>

# 复制应用程序代码
COPY /my-app-code /app
WORKDIR /app

# 为Flask暴露端口5000
EXPOSE 5000

# 设置入口点
ENTRYPOINT ["python", "main.py"]

在你的Flask应用的.py中,你应该以这种方式运行你的应用程序:

app.run(host="0.0.0.0", port="5000")

我使用tensorflow镜像,因为否则我会缺少NVIDIA驱动程序,容器将无法看到GPU。我不使用nvidia容器,因为这可能与我使用的tensorflow版本存在版本冲突。

我不得不放弃miniconda,因为它带来了很多复杂性。然而,可以在其上运行Flask API。以下是有效的Dockerfile:

FROM continuumio/miniconda3

# 安装基本工具
RUN apt-get update \
&& apt-get install -y wget \
&& rm -rf /var/lib/apt/lists/*

# flask应用程序代码在与dockerfile相邻的“my-app-code”文件夹中
COPY /my-app-code /app
RUN echo "Running $(conda --version)" 
RUN conda update conda 
RUN conda create -n app-env python=3.9 
RUN echo "conda activate app-env" >> ~/.bashrc
SHELL ["/bin/bash", "--login", "-c"]
RUN conda activate app-env
RUN conda install flask requests 
EXPOSE 5000
ENTRYPOINT ["conda", "run", "-n", "app-env", "python", "/app/main.py"]

不要忘记像上面展示的那样在你的app.run中添加主机和端口。

英文:

Here is a best solution I found.

Dockerfile:

FROM tensorflow/tensorflow:2.12.0-gpu

# Install dependencies
RUN pip install tensorflow-hub <whatever else you need>

# Copy application code
COPY /my-app-code /app
WORKDIR /app

# Expose port 5000 for Flask
EXPOSE 5000

# Set entrypoint
ENTRYPOINT ["python", "main.py"]

In your .py with Flask app you should run your application this way:

app.run(host="0.0.0.0", port="5000")

I use tensorflow image, because otherwise I'll miss nvidia drivers and containter won't see GPU. I don't use nvidia container because this could have version conflict with tensorflow version I'm using.

I had to move from miniconda as it brings lot of complexity. However it's possible to run Flask API on it. Here is working dockerfile:

FROM continuumio/miniconda3

# Install base utilities
RUN apt-get update \
&& apt-get install -y wget \
&& rm -rf /var/lib/apt/lists/*

# flask app code is in "my-app-code" folder near with dockerfile
COPY /my-app-code /app
RUN echo "Running $(conda --version)" 
RUN conda update conda 
RUN conda create -n app-env python=3.9 
RUN echo "conda activate app-env" >> ~/.bashrc
SHELL ["/bin/bash", "--login", "-c"]
RUN conda activate app-env
RUN conda install flask requests 
EXPOSE 5000
ENTRYPOINT ["conda", "run", "-n", "app-env", "python", "/app/main.py"]

Don't forget to add host and port to your app.run as showed above.

huangapple
  • 本文由 发表于 2023年5月21日 16:17:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76298916.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定