DockerFi内部的高级脚本

2024-10-02 06:29:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个Docker映像/容器,它将在windows10/Linux上运行并测试restapi。是否可以将函数(从我的.bashrc文件)嵌入DockerFile中?函数pytest在运行.py文件之前调用pylint。如果评级不是10/10,则会提示用户修复代码并退出。这在Linux上运行良好。你知道吗

基本上这里是DockerFile中的伪代码,我正试图构建一个映像。你知道吗

------------------------------------------
From: Ubuntu x.xx
install python
Install pytest
install pylint
copy test_file to the respective folder
Execute pytest test_file_name.py
if the rating is not 10\10:
    prompt the user to resolve the rating issue and exit
------------here is the partial code snippet from the func------------------------
function pytest () {
    argument1="$1"
    # Extract the path and file name for pylint when method name is passed
    pathfilename=`echo ${argument1} | sed 's/::.*//'`
    clear && printf '\e[3J'
    output=$(docker exec -t orch-$USER pylint -r n ${pathfilename})
    if (echo "$output" | grep 'warning.*error' o&>/dev/null or
        echo "${output}" | egrep 'warning|convention' &>/dev/null)
    then
            echo echo "${output}" | sed 's/\(warning\)/\o033[33m\1\o033[39m/;s/\(errors\|error\)/\o033[31m\1\o033[39m/'
            YEL='\033[0;1;33m'
            NC='\033[0m'
            echo -e "\n  ${YEL}Fix module as per pylint/PEP8 messages to achieve 10/10 rating before pusing to github\n${NC}"`
fi

我能想到的另一个选择是:
步骤1]使用所有必需的软件构建映像(使用DockerFile)
步骤2]在.py文件中,使用函数中的逻辑添加执行pytest的调用。你知道吗

你的想法?你知道吗


Tags: 文件theto函数namepyechodockerfile
1条回答
网友
1楼 · 发布于 2024-10-02 06:29:08

可以将该函数转换为独立的shell脚本。(基本上只需移除function包装器,并取出工具调用的docker exec部分)完成后,就可以COPY将shell脚本放入映像中,完成后,就可以RUN执行它。你知道吗

...
COPY pylint-enforcer.sh .
RUN chmod +x ./pylint-enforcer.sh \
 && ./pylint-enforcer.sh
...

它看起来像pylint will produce a non-zero exit code if it emits any messages。对于Dockerfile来说,只要RUN pylint -r -n .就足够了;如果它打印任何内容,它看起来将返回一个非零的退出代码,而docker build将解释为“失败”而不继续。你知道吗

您可能会考虑是否需要构建和推送并非绝对完美的代码映像的能力(可能是在生产停止事件期间),以及是否需要根级别的权限来运行简单的代码有效性工具(如果您可以docker任何您可以作为根用户编辑主机上的任意文件的权限)。我建议在CI过程中在非Docker虚拟环境中运行这些工具,不要将它们放在Dockerfile中,也不要依赖docker exec来运行它们。你知道吗

相关问题 更多 >

    热门问题