skip to Main Content

my userapi

if __name__ == "__main__":
    if len(sys.argv) >= 2:
        try:
            app_port = int(sys.argv[1])
        except ValueError as ve:
            app_port = 5558

        app.run(debug=True, host='0.0.0.0', port=app_port)
    else:
        raise ValueError('No starting port for the application')

my startdocker.sh file

#!/bin/bash
docker container stop userapi_cnt_ad
docker container rm userapi_cnt_ad
docker image rm userapi_img_ad
docker volume rm ad_vol
docker volume create --name ad_vol --opt device=/mnt/share/ad-infra-tp-2-h-2024 --opt o=bind --opt type=none
docker build -t userapi_img_ad -f ./project/docker/Dockerfile .
docker run -p 25556:5556 --mount source=ad_vol,target=/mnt/app --name userapi_cnt_ad userapi_img_ad

and my dockerfile

dont want to share my FROM file
COPY ./requirements.txt /
RUN pip3 install -r /requirements.txt
WORKDIR /mnt/app/
CMD ["./run.sh", "./project/main/userapi.py"]

when i try ./startdocker.sh

it always gives me

File "./project/main/userapi.py", line 46, in <module>
raise ValueError('No starting port for the application')
ValueError: No starting port for the application

and i tried directly giving the app_port a port number, yes it works, but its not what i want because i am trying to test it later with another port

2

Answers


  1. You need in the Dockerfile:

    CMD ["./run.sh", "./project/main/userapi.py", "5555"]
    

    where 5555 is the port you want to bind.

    Your if statement test the numbers of arguments: as far as you don’t pass any, you always throw the ValueError.

    Login or Signup to reply.
  2. There are two points you need to consider:

    1. sys.argv: In python, sys.argv is the list of command line arguments passed to a python script. The first element sys.argv[0] is always the script name. So, if you are just running a command like python userapi.py (or in your case like ./run.sh ~/userapi.py) you are not passing any argument and the length of the list would be always equal to one (len(sys.argv) == 1).
    2. CMD in docker: specifies what command to run within the container. In other words, it specifies the instruction need to be executed when starting a container. This is where you can pass parameters and make sure the length of your system arguments is sys.argv >=2.
      There are two ways to use CMD command to run your instruction:
    • Method 1: shell syntax
    CMD executable parameter1 parameter2
    
    • Method 2: use the JSON array format (recommended)
    CMD ["executable", "parameter1", "parameter2"]
    

    in your case:

    CMD ["./run.sh", "./project/main/userapi.py", "5556"]
    

    References:

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search