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
There are two points you need to consider:
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
).sys.argv >=2
.
There are two ways to use CMD command to run your instruction:CMD executable parameter1 parameter2
CMD ["executable", "parameter1", "parameter2"]
in your case:
CMD ["./run.sh", "./project/main/userapi.py", "5556"]
References: