#容器化python 3 的开发的无界面(Headless)程序
此镜像可以跑chrome 和 selenium开放的程序
使用alpine 镜像,可以减小image尺寸
- #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= 2 e c h o " − − − − − − − 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}