【发布时间】:2021-04-11 01:14:05
【问题描述】:
python 超级新手,之前从未使用过 docker。我想在 Google Cloud Run 上托管我的 python 脚本,但需要打包到 Docker 容器中以提交给 google。
这个 DockerFile 中究竟需要做什么才能上传到 google?
当前信息:
- Python:v3.9.1
- 烧瓶:v1.1.2
- Selenium Web 驱动程序:v3.141.0
- Firefox Geckodriver:v0.28.0
- Beautifulsoup4:v4.9.3
- 熊猫:v1.2.0
如果需要有关脚本的更多信息,请告诉我。
我从here 中找到了以下sn-ps 代码作为起点。我只是不知道如何调整以适应我的规格,也不知道“gunicorn”的用途。
# Use the official Python image.
# https://hub.docker.com/_/python
FROM python:3.7
# Install manually all the missing libraries
RUN apt-get update
RUN apt-get install -y gconf-service libasound2 libatk1.0-0 libcairo2 libcups2 libfontconfig1 libgdk-pixbuf2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libxss1 fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils
# Install Chrome
RUN wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
RUN dpkg -i google-chrome-stable_current_amd64.deb; apt-get -fy install
# Install Python dependencies.
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
# Copy local code to the container image.
ENV APP_HOME /app
WORKDIR $APP_HOME
COPY . .
# Run the web service on container startup. Here we use the gunicorn
# webserver, with one worker process and 8 threads.
# For environments with multiple CPU cores, increase the number of workers
# to be equal to the cores available.
CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 main:app
# requirements.txt
Flask==1.0.2
gunicorn==19.9.0
selenium==3.141.0
chromedriver-binary==77.0.3865.40.0
【问题讨论】:
-
你如何运行你的烧瓶应用程序? docker 文件对我来说似乎还不错,您只需传递所有需要特定版本控制的要求。在文件的末尾,您将添加运行烧瓶应用程序所需的命令,在此 docker 文件中,gunicorn 用于提供服务,如果您有 main.py,您还需要将
export FLASK_APP=main添加到 dockerfile 中其中包含app.run() -
感谢您的回复。我什至还没有将 main.py 设置为包含 Flask,因为我仍在学习如何......只知道我需要它。但它将包含
app.run(),所以我将添加该命令。所以我理解正确,在这个例子中,gunicorn 被用作 Flask 的替代品? -
我现在读的越多,我就越不确定我是否应该使用 Flask 并且应该使用 Nginx 或 Gunicorn。
-
Flask 应用程序可以使用许多工具,例如
flask run本身不推荐用于生产用途,我们有 gunicorn、waitress、wsgi 等,它们都服务于 Flask 应用程序,你需要告诉他们你在哪个文件中启动了你的烧瓶应用程序。
标签: python docker selenium dockerfile google-cloud-run