Python 脚本的 DockerFile,带有基于 Firefox 的 Selenium 网络驱动程序、Flask 和 BS4 依赖项
DockerFile for a Python script with Firefox-based Selenium web driver, Flask & BS4 dependencies
python 的超级新手,之前从未使用过 docker。我想在 Google 云 运行 上托管我的 python 脚本,但需要打包到 Docker 容器中以提交到 google.
这个 Docker 上传到 google 的文件究竟需要什么?
当前信息:
- Python: v3.9.1
- 烧瓶:v1.1.2
- Selenium Web 驱动程序:v3.141.0
- Firefox Geckodriver:v0.28.0
- Beautifulsoup4: v4.9.3
- Pandas: v1.2.0
如果需要有关脚本的更多信息,请告诉我。
我发现以下代码片段可用作 here 的起点。我只是不知道如何调整以适应我的规格,也不知道 '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
Gunicorn 是一个用于 运行 宁你的 python 应用程序实例的应用程序服务器,它是一个用于 WSGI 的纯 Python HTTP 服务器应用程序。它允许您 运行 任何 Python 应用程序并发通过 运行 在单个 dyno 中设置多个 Python 进程。
请查看以下教程,其中详细解释了有关 gunicorn。
关于云 运行,要部署到云 运行,请按照后续步骤或 Cloud Run Official Documentation:
1) 创建文件夹
2) 在该文件夹中,创建一个名为 main.py 的文件并编写您的 Flask 代码
简单 Flask 代码示例
import os
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello_world():
name = os.environ.get("NAME", "World")
return "Hello {}!".format(name)
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))
3) 现在您的应用已完成,可以进行容器化并上传到 Container Registry
3.1) 因此,要将您的应用程序容器化,您需要在与源文件相同的目录中有一个 Dockerfile (main.py)
3.2) 现在使用 Cloud Build 构建您的容器镜像,运行 从包含 Dockerfile 的目录中执行以下命令:
gcloud builds submit --tag gcr.io/PROJECT-ID/FOLDER_NAME
其中 PROJECT-ID 是您的 GCP 项目 ID。 运行宁gcloud config get-value project
即可获得
4) 最后,您可以通过执行以下命令部署到云端 运行:
gcloud run deploy --image gcr.io/PROJECT-ID/FOLDER_NAME --platform managed
您还可以查看 Google Cloud Run Official GitHub Repository 云 运行 Hello World 示例。
python 的超级新手,之前从未使用过 docker。我想在 Google 云 运行 上托管我的 python 脚本,但需要打包到 Docker 容器中以提交到 google.
这个 Docker 上传到 google 的文件究竟需要什么?
当前信息:
- Python: v3.9.1
- 烧瓶:v1.1.2
- Selenium Web 驱动程序:v3.141.0
- Firefox Geckodriver:v0.28.0
- Beautifulsoup4: v4.9.3
- Pandas: v1.2.0
如果需要有关脚本的更多信息,请告诉我。
我发现以下代码片段可用作 here 的起点。我只是不知道如何调整以适应我的规格,也不知道 '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
Gunicorn 是一个用于 运行 宁你的 python 应用程序实例的应用程序服务器,它是一个用于 WSGI 的纯 Python HTTP 服务器应用程序。它允许您 运行 任何 Python 应用程序并发通过 运行 在单个 dyno 中设置多个 Python 进程。
请查看以下教程,其中详细解释了有关 gunicorn。
关于云 运行,要部署到云 运行,请按照后续步骤或 Cloud Run Official Documentation:
1) 创建文件夹
2) 在该文件夹中,创建一个名为 main.py 的文件并编写您的 Flask 代码
简单 Flask 代码示例
import os
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello_world():
name = os.environ.get("NAME", "World")
return "Hello {}!".format(name)
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))
3) 现在您的应用已完成,可以进行容器化并上传到 Container Registry
3.1) 因此,要将您的应用程序容器化,您需要在与源文件相同的目录中有一个 Dockerfile (main.py)
3.2) 现在使用 Cloud Build 构建您的容器镜像,运行 从包含 Dockerfile 的目录中执行以下命令:
gcloud builds submit --tag gcr.io/PROJECT-ID/FOLDER_NAME
其中 PROJECT-ID 是您的 GCP 项目 ID。 运行宁gcloud config get-value project
4) 最后,您可以通过执行以下命令部署到云端 运行:
gcloud run deploy --image gcr.io/PROJECT-ID/FOLDER_NAME --platform managed
您还可以查看 Google Cloud Run Official GitHub Repository 云 运行 Hello World 示例。