赞
踩
我们通过python实现了一个需要依赖selenium
的程序脚本
但是这个插件需要依赖linux中有安装chromium-chromedriver
web驱动
因此我们需要制作一个包含:python3
+selenium
+chromedriver
的基础镜像
这里我们要保证在创建的环境下是通过docker官方的dockerhub下载的镜像源
docker login
但是最终可能我们要将镜像推送到我们个人的镜像仓库,如:harbor
、阿里云-acr
当把镜像制作好
我们再登录自己的平台
docker login [你的容器管理平台]
将镜像推送上去
# 使用 Python 3.9 作为基础镜像
FROM python:3.9
# 更新apt并安装 Chromium 和 Chromium Driver
RUN apt-get update && apt-get install -y \
chromium \
chromium-driver
# 设置环境变量,指定 Chromium 可执行文件的位置
ENV CHROME_BIN=/usr/bin/chromium
问题
:直接在python的linux环境中安装chromium-driver
最后整个镜像大小为1.3GB每次编译的市场和镜像都会大很多
# 第一阶段:构建阶段 FROM python:3.9-alpine AS builder # 安装 Chromium 和 Chromium Driver 的构建依赖项 RUN apk update && apk add --no-cache \ chromium \ chromium-chromedriver # 第二阶段:运行阶段 FROM python:3.9-alpine # 从构建阶段复制已安装好的 Chromium 和 Chromium Driver COPY --from=builder /usr/bin/chromium-browser /usr/bin/chromium-browser COPY --from=builder /usr/lib/chromium /usr/lib/chromium # 更新 Alpine 的软件源,并安装运行 Chromium 所需的依赖项 RUN apk update && apk add --no-cache \ libstdc++ \ harfbuzz \ nss \ freetype \ ttf-freefont # 设置环境变量,指定 Chromium 可执行文件的位置 ENV CHROME_BIN=/usr/bin/chromium-browser # 添加你的其他操作,比如安装 Python 应用程序所需的依赖项
2个路径:
/usr/lib/chromium
/usr/bin/chromium-browser
执行脚本:
cat /usr/bin/chromium-browser
chromium-browser的脚本如下:
#!/bin/sh for f in /etc/chromium/*.conf; do [ -f "$f" ] && . "$f" done # Append CHROMIUM_USER_FLAGS (from env) on top of system # default CHROMIUM_FLAGS (from /etc/chromium/chromium.conf). CHROMIUM_FLAGS="$CHROMIUM_FLAGS ${CHROMIUM_USER_FLAGS:+"$CHROMIUM_USER_FLAGS"}" # Let the wrapped binary know that it has been run through the wrapper export CHROME_WRAPPER="$(readlink -f "$0")" PROGDIR=${CHROME_WRAPPER%/*} case ":$PATH:" in *:$PROGDIR:*) # $PATH already contains $PROGDIR ;; *) # Append $PROGDIR to $PATH export PATH="$PATH:$PROGDIR" ;; esac if [ $(id -u) -eq 0 ] && [ $(stat -c %u -L ${XDG_CONFIG_HOME:-${HOME}}) -eq 0 ]; then # Running as root with HOME owned by root. # Pass --user-data-dir to work around upstream failsafe. CHROMIUM_FLAGS="--user-data-dir=${XDG_CONFIG_HOME:-"$HOME"/.config}/chromium $CHROMIUM_FLAGS" fi # Set the .desktop file name export CHROME_DESKTOP="chromium.desktop" export CHROME_VERSION_EXTRA="Alpine Linux" exec "$PROGDIR/chromium" ${CHROMIUM_FLAGS} "$@"
下面我们通过
python3
+selenium
+chromedriver
这个基础镜像来制作我们python脚本的镜像
我们将
python3
+selenium
+chromedriver
打包成私有镜像
,地址:registry.cn-beijing.aliyuncs.com/ctra_test/python3.9-with-chromium-driver-alpine
# Use an official Python runtime as a parent image FROM registry.cn-beijing.aliyuncs.com/ctra_test/python3.9-with-chromium-driver-alpine # Set the working directory in the container WORKDIR /app # Copy the current directory contents into the container at /app COPY . /app # Install any needed packages specified in requirements.txt # 更换 pip 源为阿里云镜像 1 RUN pip install --no-cache-dir -i https://mirrors.aliyun.com/pypi/simple/ -r requirements.txt # Run the Python script when the container launches CMD ["python", "./get_itra_cookie_with_cra.py"]
# 针对某个文件夹下进行 requirement 打包
pipreqs . --encoding=utf8 --force
参考
[1] https://blog.csdn.net/qq_41202483/article/details/121880256
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。