2

I'm new to Docker. I have a Python program that I run in the following way.

python main.py --s=aws --d=scylla --p=4 --b=15 --e=local -w

Please note the double hyphen -- for the first four parameters and single hyhpen '-' for the last one.

I'm trying to run this inside a Docker container. Here's my Dockerfile:

FROM python:3.6
COPY . /app
WORKDIR /app
RUN pip install --upgrade pip
RUN pip install --no-cache-dir -r requirements.txt
CMD ["python","app.py","--s","(source)", "--d","(database)","--w", "(workers)", "--b", "(bucket)", "--e", "(env)", "-w"]

I'm not sure if this is will work as I don't know exactly how to test and run this. I want to run the Docker image with the following port mappings.

docker run --name=something -p 9042:9042 -p 7000:7000 -p 7001:7001 -p 7199:7199 -p 9160:9160 -p 9180:9180 -p 10000:10000 -d user/something

How can I correct the Docker file? Once I build an image how to run it?

2
  • What is exactly your problem ? I don't know exactly how to test and run this. ? It's your program and you have the docker run command.. just execute it and see if your program is running correctly. Commented Aug 16, 2019 at 17:47
  • Do the arguments actually need to be baked into the image or could the be added at docker run time on the command line? Commented Aug 17, 2019 at 1:59

1 Answer 1

3

First, fix the dockerfile:

FROM python:3.6
COPY . /app
WORKDIR /app
# optional: it is better to chain commands to reduce the number of created layers
RUN pip install --upgrade pip \
  && pip install --no-cache-dir -r requirements.txt
# mandatory: "--s=smth" is one argument
# optional: it's better to use environment variables for source, database etc
CMD ["python","app.py","--s=(source)", "--d=(database)","--w=(workers)", "--b=(bucket)", "--e=(env)", "-w"]

then, build it:

docker build -f "<dockerfile path>" -t "<tag to assign>" "<build dir (eg .)>"

Then, you can just use the assigned tag as an image name:

docker run ... <tag assigned>

UPD: I got it wrong the first time, tag should be used instead of the image name, not the instance name

UPD2: With the first response, I assumed you're going to hardcode parameters and only mentioned it is better to use environment variables. Here is an example how to do it:

First, better, option is to use check environment variables directly in your Python script, instead of command line arguments.

First, make your Python script to read environment variables. The quickest dirty way to do so is to replace CMD with something like:

CMD ["sh", "-c", "python app.py --s=$SOURCE --d=$DATABASE --w=$WORKERS ... -w"]

(it is common to use CAPS names for environment variables) It will be better, however, to read environment variables directly in your Python script instead of command line arguments, or use them as defaults:

# somewere in app.py
import os
...
DATABASE = os.environ.get('DATABASE', default_value)  # can default ot args.d
SOURCE = os.environ.get('SOURCE')  # None by default
# etc

Don't forget to update dockerfile as well in this case

# Dockerfile:
...
CMD ["python","app.py"]

Finally, pass environment variables to your run command:

docker run --name=something ... -e DATABASE=<dbname> -e SOURCE=<source> ... <tag assigned at build>

There are more ways to pass environment variables, I'll just refer to the official documentation here: https://docs.docker.com/compose/environment-variables/

Sign up to request clarification or add additional context in comments.

1 Comment

in this syntax how do I pass the values to the parameters, like --s=<something>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.