Home > Enterprise >  accept default values in python while creating a docker image
accept default values in python while creating a docker image

Time:07-16

I am creating a docker image of a python project like this:

FROM python:3.7
RUN git clone https://github.com/a/abc.git
WORKDIR abc

RUN pip install -r requirements.txt
CMD [ "python3", "./cascade.py --setup" ]
CMD [ "python3", "./cascade.py" ]

when the first CMD runs that is CMD [ "python3", "./cascade.py --setup" ], it needs some arguments to set by the user that is as follows:

arguments

My Question

How do I set the default values that are just pressing enter or ask the user to input the values when I run sudo docker build . -t pythonimg as without this confirmation, the second command will not work and will throw errors.

CodePudding user response:

you can pipe in yes for the command. This seems to be the preferred way to do this

RUN yes | pip install -r requirements.txt

CodePudding user response:

Update: You can create a file with your configuration values one per line:

True
False
127.0.0.1
...

You can then save this file and copy it in your build in the Dockerfile:

...
COPY my-config.txt .
...

You can then feed this to your program as:

...
CMD python3 ./cascade.py --setup < my-config.txt
...

This should work.

Note that, this is not the right way of automating image builds inside a container. You can rewrite your application to accept parameter values, in a way that it accepts CLI parameters instead of STDIN input.

Additionally, you can create build arguments for your docker build using the ARG instruction. (Read more here).

This way, it can run like:

...
# When not specified in `docker build` command, these values will be used
ARG arg1=<default value>
ARG arg2=<default value>
ENV arg1=$arg1
ENV arg2=$arg2
...
CMD python3 ./cascade.py --setup --arg1=${arg1} --arg2=${arg2} ...
...

You will then use these arguments in your docker build command with --build-arg switch as follows:

docker build -t pythonimg --build-arg arg1=value1 --build-arg arg2=value2 ... .

This you can provide through your automated system easily and pass onto your image build.

  • Related