Last active
February 7, 2024 18:02
-
-
Save frankyaorenjie/fc60dba18642d96e1b1a6f90fa955ad6 to your computer and use it in GitHub Desktop.
[Pass parameters from docker run to python] #docker #dockerfile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
docker run foo:bar --app_id='abc' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM python:3.6 | |
ENV LC_ALL C.UTF-8 | |
ENV LANG C.UTF-8 | |
ENV GOOGLE_APPLICATION_CREDENTIALS /service/scripts/application_default_credentials.json | |
COPY . /service/scripts | |
WORKDIR /service/scripts | |
RUN pip install pipenv | |
RUN pipenv install --deploy --system | |
ENTRYPOINT ["python", "/service/scripts/index_image_analysis.py"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
parser = argparse.ArgumentParser(description='For index images analysis') | |
parser.add_argument('--app_id', dest='app_id', type=str, help='app_id', metavar='app_id', default='2760') | |
parser.add_argument('--sample_n', dest='sample_n', type=int, help='number to sample', default=10000) | |
parser.add_argument('--top_n', dest='top_n', type=int, help='top n distribution', default=10) | |
args = parser.parse_args() |
bravo
thanks for this! it works with sys.argv as well
@RheingoldRiver , how did you use it with sys.argv ?
@parikshit14
Pretty much exactly as shown in the original. Here is my dockerfile:
FROM python:3.8.16-bullseye
COPY /requirements.txt /requirements.txt
RUN pip install -U pip
RUN pip install -r requirements.txt
WORKDIR historic-employee-count-tool
COPY . .
ENTRYPOINT [ "python", "historic-employee-count-tool/main.py"]
And in the python code here is how to access an arg:
key_arg = sys.argv[1]
Remember with argv, 0 is reserved as the name of the script.
@RheingoldRiver how did you executed it docker run ....what....
@parikshit14
You run it with:
docker run -it container-name-here param1 param2 param3
Here's a link to the full repo including a README in case that's helpful too.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice