0

I feel like I tried everything including using shell/exec forms, but I still can't figure out why the ENV variables aren't being recognized. You can see below the different variations of commands I tried using. The ENV variables are there for default values, but I plan to be able to change them through command-line arguments when running the container as well (which I'm not sure how to neither at the moment).

# syntax=docker/dockerfile:1 (IS THIS LINE NECESSARY AT ALL NOWADAYS?)

FROM python:3.8-slim-buster

WORKDIR /app

COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt

COPY . .

ENV INPUT="--input ./histogram_input"
ENV OUTPUT="--output ./TEST.tsv"
ENV BUCKET_COUNT="--bucket-count 9999"

ENTRYPOINT ["python", "CreateWeatherHistogram.py", "${INPUT}", "${OUTPUT}", "${BUCKET_COUNT}"]


# ENTRYPOINT ["python", "CreateWeatherHistogram.py", "${input}", "${output}", "${bucket_count}"] # DOESNT WORK
# ENTRYPOINT ["python", "CreateWeatherHistogram.py", "${input} ${output} ${bucket_count}"] # DOESNT WORK
# ENTRYPOINT python CreateWeatherHistogram.py ${input} ${output} ${bucket_count} # DOESNT WORK
# ENTRYPOINT python CreateWeatherHistogram.py "$input" "$output" "$bucket_count" # DOESNT WORK

My python app is called with:

CreateWeatherHistogram --input ./histogram_input --output ./histogram.tsv --bucket-count 5

I want to be able to run this container with:

docker run --mount type=bind,source=/,target=/app [IMAGE-NAME] --input input.file --output output.file --bucket-count x
chadlei
  • 179
  • 1
  • 11
  • i think what you need is not env variables, but **arguments processing** (check the argparse module). you can just handle them in python code without dockerfile. And by convention, env variable should better be all UPPERCASE. – Lei Yang Jul 16 '21 at 00:15
  • @LeiYang I actually do have arg parsing in my python file. i just want it with docker so that I'll be able to change the input args from docker run. good tip on the all uppercase thanks – chadlei Jul 16 '21 at 00:17
  • if just keep `ENTRYPOINT ["python", "CreateWeatherHistogram.py"]` and remove all env, why cann't you `be able to change the input args from docker run`? – Lei Yang Jul 16 '21 at 00:20
  • @LeiYang i didnt know you could just throw arguments in there and it'll add it on to the entrypoint command LOL thanks – chadlei Jul 16 '21 at 00:30
  • i would still like to know how to get env variables to show in entrypoint though – chadlei Jul 16 '21 at 00:31
  • 1
    then please remove all python related things, simplify the question. and change to UPPERCASE first. – Lei Yang Jul 16 '21 at 00:32
  • @LeiYang have a feeling the python part might have a part in it - various related posts has "sh", "c" as the answer but my scenario is specific to python – chadlei Jul 16 '21 at 00:35
  • did you tried this one: ENTRYPOINT python CreateWeatherHistogram.py $INPUT $OUTPUT $BUCKET_COUNT – matiasm77 Jul 16 '21 at 01:12
  • Yes no luck with that one neither – chadlei Jul 16 '21 at 15:35

1 Answers1

1

If it's your own Python code, it seems the easiest solution is just to write your code to read the values directly from the environment, rather than setting environment variables and then passing them in as arguments.

For example, consider the following code that uses the click option processing library (you could do the same thing with argparse (see for example this question), but I like working with click:

#!/usr/bin/python

import click
import os


@click.command()
@click.option('--input', envvar='INPUT')
@click.option('--output', envvar='OUTPUT')
@click.option('--bucket-count', envvar='BUCKET_COUNT')
def main(input, output, bucket_count):
    print('value of input:', input)
    print('value of output:', output)
    print('value of bucket_count:', bucket_count)


if __name__ == '__main__':
    main()

You can pass in arguments on the command line, like this:

$ python CreateWeatherHistogram.py --input histogram_input
value of input: histogram_input
value of output: None
value of bucket_count: None

But you can also just set the corresponding environment variable:

$ export INPUT=histogram_input
$ python CreateWeatherHistogram.py
value of input: histogram_input
value of output: None
value of bucket_count: None

With this sort of code, your Dockerfile looks like this:

FROM python:3.8-slim-buster

WORKDIR /app

COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt

COPY . .

ENV INPUT="./histogram_input"
ENV OUTPUT="./TEST.tsv"
ENV BUCKET_COUNT="9999"

ENTRYPOINT ["python", "CreateWeatherHistogram.py"]
larsks
  • 277,717
  • 41
  • 399
  • 399
  • Very interesting! I think I’m using arg for the very same purpose at the moment but thank you for showing me alternatives. – chadlei Jul 16 '21 at 15:46