Skip to content

Instantly share code, notes, and snippets.

@frankyaorenjie
Last active February 7, 2024 18:02
Show Gist options
  • Save frankyaorenjie/fc60dba18642d96e1b1a6f90fa955ad6 to your computer and use it in GitHub Desktop.
Save frankyaorenjie/fc60dba18642d96e1b1a6f90fa955ad6 to your computer and use it in GitHub Desktop.
[Pass parameters from docker run to python] #docker #dockerfile
docker run foo:bar --app_id='abc'
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"]
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()
@HoiDam
Copy link

HoiDam commented Feb 23, 2022

nice

@xiwen1995
Copy link

bravo

@RheingoldRiver
Copy link

thanks for this! it works with sys.argv as well

@parikshit14
Copy link

@RheingoldRiver , how did you use it with sys.argv ?

@RheingoldRiver
Copy link

RheingoldRiver commented Apr 5, 2023

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

@parikshit14
Copy link

@RheingoldRiver how did you executed it docker run ....what....

@RheingoldRiver
Copy link

@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