0

I tried to start 3 docker containers from single repository. My file tree is like so.

.
├── docker
│   ├── inf1
│   │   └── Dockerfile
│   ├── inf2
│   │   └── Dockerfile
│   └── proc1
│       └── Dockerfile
├── docker-compose.yaml
├── example
│   ├── __init__.py
│   ├── infinite_process1.py
│   ├── infinite_process2.py
│   └── stop_process.py
├── exe_inf1.py
├── exe_inf2.py
└── exe_proc.py

infinite_process1.py and infinite_process2.py contains infinite loop process using while True like so

# ./example/infinite_process1.py
# ./example/infinite_process2.py

import time


class InfiniteProcess1:  # InfiniteProcess2 respectively
    def __init__(self):
        pass

    def infinite_loop(self):
        while True:
            print("InfProcess 1")
            time.sleep(2)

./example/stop_process.py is like so. It does not run infinitely

# ./example/stop_process.py
class SimpleProcess:
    def __init__(self, name_: str):
        self.my_name = name_

    def run(self):
        print(f"my name is {self.my_name}")

exe_inf1.py exe_inf2.py exe_proc.py runs classes in infinite_process1.py infinite_process2.py stop_process.py respectively.

For example, ./docker/inf1/Dockerfile looks like this

FROM python:3.10.7

WORKDIR /app

COPY . .

ENTRYPOINT [ "python" ]

CMD [ "exe_inf1.py" ]

I wanted to create all 3 exe_*.py files start independently so, I've created a .yaml file like so

version: '2'
services:
  py1:
    build:
      context: .
      dockerfile: ./docker/inf1/Dockerfile
  
  py2:
    build:
      context: .
      dockerfile: ./docker/inf2/Dockerfile

  py3:
    build:
      context: .
      dockerfile: ./docker/proc1/Dockerfile

However, while docker containers were build successfully, docker container doesn't seem to be running ./exe_inf1.py and ./exe_inf2.py correctly. It should be printing "InfProcess 1" every 2 seconds.

[+] Running 4/1
 ⠿ Network docker-python_default  Created                                                         0.0s
 ⠿ Container docker-python-py2-1  Created                                                         0.0s
 ⠿ Container docker-python-py3-1  Created                                                         0.0s
 ⠿ Container docker-python-py1-1  Created                                                         0.0s
Attaching to docker-python-py1-1, docker-python-py2-1, docker-python-py3-1
docker-python-py3-1  | my name is foo
docker-python-py3-1 exited with code 0

So my question is:

What can I do to make infinite looping docker container to run independently? Or am I doing something wrong here

The whole code can be found here: https://github.com/SKKUGoon/sof_question

배상일
  • 87
  • 1
  • 9
  • 1
    Did you try flushing STDOUT? Maybe everything is working but you just don't see the output. See this question https://stackoverflow.com/questions/230751/how-can-i-flush-the-output-of-the-print-function – SebDieBln Dec 15 '22 at 12:49
  • Thank you! I wasn't sure what question to ask. After adding `flush=True` It works like a charm! – 배상일 Dec 15 '22 at 12:54

1 Answers1

1

It was just printout problem like comment made by @SebDieBlnsaid.

import time

class InfiniteProcess2:
    def __init__(self):
        pass

    def infinite_loop(self):
        while True:
            print("InfProcess 2", flush=True)
            time.sleep(2)

Adding flush=True made everything work. Thanks.

배상일
  • 87
  • 1
  • 9