0

I have multiple Python scripts from which I want to run a docker container. From a related question How to run multiple Python scripts and an executable files using Docker? , I found that the best way to do that is to have run.sh a shell file as follows:

#!/bin/bash
python3 producer.py &
python3 consumer.py &
python3 test_conn.py

and then call this file from a Dockerfile as:

FROM python:3.9

RUN mkdir -p /usr/src/app

WORKDIR /usr/src/app

COPY requirements.txt /usr/src/app

RUN pip install --no-cache-dir -r requirements.txt

COPY . /usr/src/app

CMD ["./run.sh"]

However, in the container logs the following error is prompting exec ./run.sh: no such file or directory, which makes no sense to me since I copied everything on the current directory, run.sh included, to /usr/src/app on my container via COPY . /usr/src/app

Please, clone my repo and on the root directory call docker-compose up -d and check myapp container logs to help me.

https://github.com/Quilograma/IES_Project

Thank you!

Can't run multiple python scripts in a single container.

4 Answers4

0

You should explicitly specify what shell interpreter be used for running your script.

Changing the last line to CMD ["bash", "-c", "./run.sh"] might solve your issue.

Jib
  • 1,334
  • 1
  • 2
  • 12
0

You need to chmod run.sh to be excecuteable:

FROM python:3.9

RUN mkdir -p /usr/src/app

WORKDIR /usr/src/app

COPY requirements.txt /usr/src/app

RUN pip install --no-cache-dir -r requirements.txt

COPY . /usr/src/app

RUN chmod +x run.sh

CMD ["./run.sh"]
zsolt
  • 1,233
  • 8
  • 18
  • Didn't work @zsolt – Martim Sousa Nov 20 '22 at 00:22
  • @MartimSousa okay, I see that your line endings were messed up, this is why questions should always include the OS (at least win/linux/mac) and all the relevant software versions. This Dockerfile I gave is still a better approach to deal with file execution permissions, to set it explicitly in image build time through Dockerfile instead of relying on that the file is add-ed, commited to the git repo with `--chmod=+x` flag or to rely on the host filesystem of the builder machine. – zsolt Nov 20 '22 at 22:32
-1

If you need to run three separate long-running processes, do not try to orchestrate them from a shell script. Instead, launch three separate containers. If you're running this via Compose, this is straightforward: have three containers all running the same image, but override the command: to run different main processes.

version: '3.8'
services:
  producer:
    build: .
    command: ./producer.py
  consumer:
    build: .
    command: ./consumer.py
  test_conn:
    build: .
    command: ./test_conn.py

Make sure the scripts are executable (run chmod +x producer.py on your host system, and commit that to source control) and begin with a "shebang" line #!/usr/bin/env python3 as the very first line.

David Maze
  • 130,717
  • 29
  • 175
  • 215
  • yeah that's another way of calling the python scripts, but the error persists. Didn't really understand what you meant by chmod +x producer.py and #!/usr/bin/env python3. I'm a begginer.... – Martim Sousa Nov 18 '22 at 23:47
-2

For those encountering the same problem adding RUN sed -i -e 's/\r$//' run.sh on the Dokcerfile before CMD ["bash", "-c", "./run.sh"] is what worked for me. See Bash script – "/bin/bash^M: bad interpreter: No such file or directory" for further details.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 22 '22 at 05:05