容器化python3.7 开发headless程序

#容器化python 3 的开发的*面(Headless)程序
此镜像可以跑chrome 和 selenium开放的程序
使用alpine 镜像,可以减小image尺寸

  1. #Dockfile文件

ROM python:3.7-alpine3.8
ARG Node=1
#启动命令脚本的第一个参数
ENV env_Node=$Node

#update apk repo

RUN echo “http://dl-4.alpinelinux.org/alpine/v3.8/main” >> /etc/apk/repositories &&
echo “http://dl-4.alpinelinux.org/alpine/v3.8/community” >> /etc/apk/repositories

#install chromedriver

RUN apk update
RUN apk add --no-cache --virtual .pynacl_deps build-base python3-dev libffi-dev libxml2-dev libxslt-dev

RUN apk add chromium chromium-chromedriver rng-tools libressl-dev

#upgrade pip
RUN pip install --upgrade pip

RUN pip install webdriver-manager

#install selenium

RUN pip install selenium

RUN rm -rf /var/lib/apt/lists/*
/var/cache/apk/*
/usr/share/man
/tmp/*

RUN mkdir -p /code/xxx_vipdlt
WORKDIR /code
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

RUN rm -rf /var/lib/apt/lists/*
/var/cache/apk/*
/usr/share/man
/tmp/*

WORKDIR /code/xxx_vipdlt/

CMD python /code/xxx_vipdlt/main.py ${env_Node}

2.#Docker build
docker build -t python37:alpine .

3: #Docker 容器启动脚本 redeploy.sh

#!/bin/sh

contrainer_name=$1
echo “docker name ${contrainer_name}”

number=2echo"2 echo "-------2echo"−−−−−−−{number}"

run_a_contrainer(){
docker run -d
-e “TZ=Asia/Shanghai”
-e env_Node=$number
-v $PWD/code:/code
-v $PWD/code/Logs:/code/xxx_vipdlt/Logs
-v $PWD/code/xxx_vipdlt:/code/xxx_vipdlt
-v /dev/shm:/dev/shm
–name ${contrainer_name} c
}

echo “------”

if sudo docker ps -a --format ‘{{.Names}}’ | grep -Eq “^${contrainer_name}$”; then
echo ‘contrainer exist’
echo ${container_name}

else
echo ‘does not exist’
echo ${contrainer_name}
run_a_contrainer
fi

3: #容器启动命令脚本
./redeploy.sh test 1
test: 容器名字
1: 程序需要的参数 ${env_Node}

容器化python3.7 开发headless程序容器化python3.7 开发headless程序 50岁的老运维 发布了2 篇原创文章 · 获赞 0 · 访问量 10 私信 关注
上一篇:5.Service资源


下一篇:Spire.XLS:一款Excel处理神器