1

I have a project and ı need to run 2 script when ı run mu Docker Image. Two of them this script has infinite loop. So none of them will stop.

I tried the use:

CMD ["python3", "main.py", "btcusdt"] 
CMD ["python3", "Flask_API/api_main.py"]

But ı discovered only last line of CDM is working.

Is there any way to do this ? Or should ı split my code and create 2 Docker Image ?

Thank you for time.

Here is my current not working Dockerfile:

FROM python:3.8

COPY requirements.txt requirements.txt


RUN pip install -r requirements.txt
COPY . .


CMD ["python3", "main.py", "btcusdt"]
CMD ["python3", "Flask_API/api_main.py"]
Selman
  • 274
  • 2
  • 4
  • 17
  • try creating a script of your commands and run using `CMD`, or creating two separate dockerfile is better option – Abhishek Jul 16 '22 at 11:47
  • Can ı do this work with python:3.8 ? Or should ı make this docker image with ubuntu ? – Selman Jul 16 '22 at 11:50
  • python3.8 will do – Abhishek Jul 16 '22 at 11:51
  • Sorry, I'm new in this field. I understand what you're saying and it makes a lot of sense. What can I search for this in google? I understand what you said, but what is the specific name of this event? I'd better take a look at the sample files. – Selman Jul 16 '22 at 11:53
  • do you want to create script or do you want to create two separate dockerfile, for first one try :https://stackoverflow.com/questions/57169267/how-to-run-two-commands-on-dockerfile and for separate docker file i guess you know how to create docker files – Abhishek Jul 16 '22 at 11:57
  • You don't necessarily need to create two images, but you should definitely run two containers with one process each. You can easily override the `CMD` when you run the container (Compose `command:`, or anything after the `docker run image-name`). – David Maze Jul 16 '22 at 12:52
  • [Why can't I use Docker CMD multiple times to run multiple services?](https://stackoverflow.com/questions/23692470/why-cant-i-use-docker-cmd-multiple-times-to-run-multiple-services) also might be informative (though I would avoid its workarounds with multiple-command scripts or supervisord). – David Maze Jul 16 '22 at 12:54

1 Answers1

1

There can be only one CMD instruction in the dockerfile if there is more than one, the last one will override the previous CMD instructions.

The better way I think is to create separate docker containers and talk to each other. here is a tutorial to do that : how to communicate between docker containers

But if you really need to run both scripts in a single container,

Here is the way

  • Create a bash script inside your project and include both bash commands in the script, here is an example.
#!/bin/bash

exec python3  main.py btcusdt &
exec python3 Flask_API/api_main.py

  • Add COPY instruction to Dockerfile to copy our bash script to the container and allow executable permission via RUN instruction.
COPY script.sh ./
RUN chmod a+x script.sh
  • Now replace the previous two CMD instructions with one to execute our bash script.
CMD ["./script.sh"]

Here is the complete Dockerfile for you

FROM python:3.8

COPY requirements.txt requirements.txt


RUN pip install -r requirements.txt
COPY . .

RUN chmod a+x script.sh

CMD ["./script.sh"]

S.Sachith
  • 536
  • 1
  • 9
  • 21