2

I have a python script that i am trying to get running correctly in a docker container. I am struggling with the CMD section of the DOCKERFILE.

I can get the script to run correctly from my terminal that shows the live updates continuously as per the code in the script by opening an interactive python console and then running the following commands

exec(open(“coin_flip_demo_v1.0.py").read())
test = coin_flip_demo()
test._run_()

My DOCKERFILE looks like

FROM python:3.6

RUN mkdir src
ADD . /src/
RUN pip install pandas
WORKDIR /src/
EXPOSE 32768 32769 32770
CMD [ "python", "./coin_flip_demo_v1.0.py" ]

When i try to run the container it doesn't give any errors however when i run docker ps there are no containers showing

Just for reference

coin_flip_demo_v1.0.py is the script i am trying to run

coin_flip_demo is my main class in that script

_run_ is a module within the main class.

Let me know if you need any more info to solve this issue.

Thanks

Al Hennessey
  • 2,395
  • 8
  • 39
  • 63

1 Answers1

2

To run interactive processes in a container, you should start the container with -i -t option.

This allocates a tty for the container.

docker run -it <img>[:tag]

Also refer to the answers in this SO question.

Apart from the -it option.. you should also make your python script to directly run (as you have mentioned in the Dockerfile so)

Can you run the script directly from the terminal? like this

python ./coin_flip_demo_v1.0.py

If not add the following lines to the end of the python file

if __name__ == '__main__':
    test = coin_flip_demo()
    test._run_()
Sam Daniel
  • 1,800
  • 12
  • 22
  • Hi, yes i ran it with those commands and it gives the same result no errors, but no containers showing when running docker ps. If i run the command docker ps -a then it shows the container as being started but obviously not continuing to run. – Al Hennessey Aug 31 '19 at 05:25
  • I am wondering if i need to create a bash file with the commands i listed above to run instead of just trying to run the single 'CMD [ "python", "./coin_flip_demo_v1.0.py" ]' in the dockerfile, however i am not sure if this is the proper way of doing it. – Al Hennessey Aug 31 '19 at 05:32
  • @AlHennessey Yes, your python script should be executable.. I have edited my answer to address this. You can use a bash script as you say, or preferable is to make the python script itself executable properly. – Sam Daniel Aug 31 '19 at 08:36