RSSregex
RSSregex

Reputation: 169

Advance Scripting inside a DockerFile

I am trying to create a Docker image/container that will run on Windows 10/Linux and test a REST API. Is it possible to embed the function (from my .bashrc file) inside the DockerFile? The function pytest calls pylint before running the .py file. If the rating is not 10/10, then it prompts the user to fix the code and exits. This works fine on Linux.

Basically here is the pseudo-code inside the DockerFile I am attempting to build an image.

------------------------------------------
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

Another option I can think of:
Step 1] Build the image (using DockerFile) with all the required software
Step 2] In a .py file, add the call for execution of pytest with the logic from the function.

Your thoughts?

Upvotes: 0

Views: 170

Answers (1)

David Maze
David Maze

Reputation: 159771

You can turn that function into a standalone shell script. (Pretty much by just removing the function wrapper, and taking out the docker exec part of the tool invocation.) Once you've done that, you can COPY the shell script into your image, and once you've done that, you can RUN it.

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

It looks like pylint will produce a non-zero exit code if it emits any messages. For purposes of a Dockerfile, it may be enough to just RUN pylint -r -n .; if it prints anything, it looks like it will return a non-zero exit code, which docker build will interpret as "failure" and not proceed.

You might consider whether you'll ever want the ability to build and push an image of code that isn't absolutely perfect (during a production-down event, perhaps), and whether you want to require root-level permissions to run simple code-validity tools (if you can docker anything you can edit arbitrary files on the host as root). I'd suggest running these tools out of a non-Docker virtual environment during your CI process, and neither place them in your Dockerfile nor depend on docker exec to run them.

Upvotes: 1

Related Questions