如何把python脚本打成docker镜像并批量部署?

549 阅读3分钟

找一个或者创建一个路径,用来放所有需要的文件和代码,并cd到该路径。

  1. 创建Dockerfile

    Dockerfile 是一个用来构建镜像的文本文件,文本内容包含了一条条构建镜像所需的指令和说明。

    看一下我的Dockerfile以及每一块的解释

    # 基础镜像设置为python:3.7.8,并声明维护人
    FROM python:3.7.8
    MAINTAINER Lucille
    ​
    # 这里设置了下载包的源为阿里云,如果不设置会从默认源下载
    RUN echo \
        'deb http://mirrors.aliyun.com/debian/ bullseye main non-free contrib\n\
    deb http://mirrors.aliyun.com/debian-security/ bullseye-security main\n\
    deb http://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib\n\
    deb http://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib' \
        > /etc/apt/sources.list
    ​
    # Chrome 依赖下载
    RUN apt-get update && apt-get install -y \
        fonts-liberation libasound2 libatk-bridge2.0-0 libatk1.0-0 libatspi2.0-0 libcups2 libdbus-1-3 libdrm2 libgbm1 libgtk-3-0 libnspr4 \
        libnss3 libwayland-client0 libxcomposite1 libxdamage1 libxfixes3 libxkbcommon0 libxrandr2 xdg-utils libu2f-udev libvulkan1
    ​
    # 下载Chrome浏览器
    RUN apt-get install -y wget
    RUN wget -q https://dl.google.com/linux/chrome/deb/pool/main/g/google-chrome-stable/google-chrome-stable_97.0.4692.71-1_amd64.deb
    RUN apt-get install ./google-chrome-stable_97.0.4692.71-1_amd64.deb
    ​
    # 打印谷歌浏览器版本
    RUN echo "Chrome: " && google-chrome --version
    ​
    # 安装谷歌驱动
    RUN wget -q -O /tmp/chromedriver.zip https://chromedriver.storage.googleapis.com/97.0.4692.71/chromedriver_linux64.zip \
        && unzip /tmp/chromedriver.zip -d /usr/local/bin/ \
        && rm /tmp/chromedriver.zip \
        && ls -al /usr/local/bin/
        
    # 声明环境变量
    ENV Selenium_UseHeadlessDriver=true
    ENV PYTHONUNBUFFERED=1
    ​
    # 将工作目录设置为 /app
    WORKDIR /app
    ​
    # 创建虚拟环境并安装pip
    RUN python -m venv /venv
    ENV PATH="/venv/bin:$PATH"
    RUN python -m pip install --no-cache-dir --upgrade pip
    ​
    # 此命令将 requirements.txt 文件复制到当前目录 (/app)
    COPY requirements.txt .
    ​
    # 安装requirements.txt中的依赖
    RUN pip install --no-cache-dir -r requirements.txt
    ​
    # 将其余应用程序文件复制到当前目录 (/app)
    COPY . .
    ​
    # 设置 chromedriver 的可执行权限
    RUN chmod +x /usr/local/bin/chromedriver
    ​
    # Start the application
    CMD [ "python", "get_website_linux.py" ]
    

    Dockerfile的常用语法:

命令说明
FROM基于哪个镜像来实现
MAINTAINER镜像的创建者
ENV声明环境变量
RUN执行的命令
ADD添加宿主机文件到容器里,有需要解压的文件会自动解压
COPY添加宿主机文件到容器里
WORKDIR工作目录
EXPOSE容器内应用可使用的端口
CMD为启动的容器指定默认要运行的程序,程序运行结束,容器也就结束。CMD 指令指定的程序可被 docker run 命令行参数中指定要运行的程序所覆盖;如果 Dockerfile 中如果存在多个 CMD 指令,仅最后一个生效。
ENTRYPOINT与CMD功能相同,但执行docker run 不会覆盖,如果需要覆盖可加参数 -entrypoint
VOLUME将宿主机的目录挂载到容器里(定义匿名数据卷。在启动容器时忘记挂载数据卷,会自动挂载到匿名卷。)
  1. docker build 构建成为一个镜像

    docker build -t your_image_name .
    

    镜像的名字可以直接替换成想要push的远程路径,就省去了tag这一步骤,例如:

    docker build -t registry.aliyuncs.com/plan:1.0 .
    

    如果考虑到默认源很多是国外的,想挂着代理build,语法为:

    docker build -t registry.aliyuncs.com/plan:1.0 --build-arg http_proxy=http://127.0.0.1:8000 --build-arg https_proxy=http://127.0.0.1:8000 .
    
  1. 本地docker run运行镜像

    docker run --name your_container_name your_image_name
    

    (your_container_name 自己起;后面跟镜像名+tag 或者镜像ID都可以;-d是让容器在后台运行并打印容器ID)

    docker run --name spider  -d 917c4f2602c1
    

    跑起来之后查看日志,运行没问题就可以push了:

    docker logs -f 4c60b061aec9
    
  2. docker push 发布镜像(可以发布到 DockerHub,也可以发布到阿里云上面)

    docker push registry.aliyuncs.com/plan:1.0
    

到这为止,镜像就打好了,如果想在其他机器上运行该镜像,需要先安装docker,登录远程仓库并拉代码运行,我一般会直接运行,之前没拉过的会直接pull后运行:

sudo docker run -d registry.aliyuncs.com/product:1.2

参考:zhuanlan.zhihu.com/p/79949030

www.runoob.com/docker/dock…