I am trying to take user input as build argument for Dockerfile and trying to pass the same to python program. Here is my use case :
Dockerfile :
FROM centos:7
USER root
RUN yum install -y python36-devel python36-pip
ARG number
COPY . /app
RUN ls -l /app
RUN echo ${number}. <=================This displays output in Docker build console
CMD python3 /app/app.py -t ${number}
app.py
import argparse
import time
def input():
parser = argparse.ArgumentParser()
parser.add_argument('number', help='take program number as input')
args = parser.parse_args()
return args
def root(args):
for i in range(10):
print("Hello World from : " + str(args.number))
time.sleep(2)
if __name__ == "__main__":
args = input()
root(args)
Docker build command :
docker build -t passarg:1 –build-arg number=1 .
Docker run command :
docker run passarg:1
Error :
usage: app.py [-h] number
app.py: error: the following arguments are required: number
2
Answers
Misread, ENV and ARG parameter, was able to solve using run time parameter :
Docker Run command :
$ docker run -it passarg:1 1
Build arguments go out of scope once the build is complete. As such, when the command is run (when you try to start the container), the argument has no value. Instead use an environment variable which maintains its value.
You can do something like this if you still want to use a build argument :