pythondockerpython-clickdocker-run

Passing Python click options to an ENTRYPOINT using docker run gives error: "executable file not found in $PATH"


I have a simple python script that I want to run inside of a docker container. It prints a one-line message "Hello {name}". The python script uses the click CLI interface to define the recipient name and if I were to run it directly (not using the docker run command) it would look like:

python hello.py -n Smith

Docker build command:

docker build . -t hello:1.0.0

Docker run command:

docker run --rm -v $(pwd):/app hello -n Smith

When using this docker run command, I get an error saying exec: "-n": executable file not found in $PATH:. Full error message is as follows:

docker: Error response from daemon: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "-n": executable file not found in $PATH: unknown.

Dockerfile:

FROM python:3.11.0-slim
RUN mkdir -p /app
WORKDIR /app
COPY . .
RUN python -m pip install --upgrade pip
RUN python -m pip install click
RUN chmod +x ./* 
ENTRYPOINT [ "python", "hello.py" ]

My simple hello.py script using click:

#!/usr/bin/env python3

import click

@click.command()
@click.option('-n', '--name', help='Name of message recipient')
def hello(name):

    # Print to screen and to file
    msg = f'Hello {name}'
    print(msg)
    with open("hello.txt", "w") as f:
        f.write(msg)

if __name__ == '__main__':
    hello() 

Expected output:

Hello Smith

I have tried changing the click option name to something else, such as -r or --recipient, but I get the same error.

When I remove the -n Smith from the docker run command I get the following expected output:

Hello None

Solution

  • Issue is with the docker run command. It needs the full <name>:<tag> when specifying the image. In this case, hello:1.0.0 matches the build command, rather than using the hello:latest tag (by default when no tag is provided), which was pointing to some other previous and broken build. The following run command fixes the issue by pointing to the correct Image ID:

    docker run --rm -v $(pwd):/app hello:1.0.0 -n Smith
    

    With output as expected:

    Hello Smith