当前位置:   article > 正文

【docker】Dockerfile制作基础镜像之:包含chromium-driv的python3环境 | docker python3+selenium+chromedriver的基础镜像_chromedriver镜像

chromedriver镜像

一、背景

我们通过python实现了一个需要依赖selenium的程序脚本
但是这个插件需要依赖linux中有安装chromium-chromedriverweb驱动
因此我们需要制作一个包含:python3+selenium+chromedriver的基础镜像

二、Dockerfile

这里我们要保证在创建的环境下是通过docker官方的dockerhub下载的镜像源

docker login
  • 1

但是最终可能我们要将镜像推送到我们个人的镜像仓库,如:harbor阿里云-acr
当把镜像制作好
我们再登录自己的平台

docker login [你的容器管理平台]
  • 1

将镜像推送上去

1、直接创建

# 使用 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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

问题:直接在python的linux环境中安装chromium-driver最后整个镜像大小为1.3GB每次编译的市场和镜像都会大很多

2、二阶段创建

# 第一阶段:构建阶段
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 应用程序所需的依赖项

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

3、检查驱动

2个路径:
/usr/lib/chromium
/usr/bin/chromium-browser

3.1、/usr/lib/chromium如下:

在这里插入图片描述

3.2、 /usr/bin/chromium-browser 如下:

执行脚本:

cat /usr/bin/chromium-browser
  • 1

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} "$@"

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

三、应用基础镜像

下面我们通过 python3+selenium+chromedriver这个基础镜像来制作我们python脚本的镜像

1、Dockerfile

我们将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"]

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

2、彩蛋 - 极简打包 requirement

# 针对某个文件夹下进行 requirement 打包
pipreqs . --encoding=utf8 --force
  • 1
  • 2

参考
[1] https://blog.csdn.net/qq_41202483/article/details/121880256

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/861833
推荐阅读
相关标签
  

闽ICP备14008679号