0

File: Dockerfile

From ubuntu:focal-20211006
RUN apt-get update
RUN apt-get -y install python3 python3-pip

RUN pip install asyncio
RUN pip install apscheduler==3.7.0

COPY test.py /home/testing/test.py
WORKDIR /home/testing

CMD python3 -u ./test.py

File:test.py

import asyncio
async def main():
    print('Comes to main')

if __name__ == '__main__':
    try:
        print('Comes to Main 1')
        asyncio.ensure_future(main())
        print('Comes to Main 2')
        asyncio.get_event_loop().run_forever()
    except (KeyboardInterrupt, SystemExit):
        print('Comes to interrupt')
        raise

Commands:

sudo docker build -t test:test
sudo docker run test:test

Unable to exit the process with ctrl+c on ubuntu:20.04

Any help would be appreciated

Note: If i use the CMD in Dockerfile to CMD python3 ./test.py (without -u option) then there are no outputs. And using docker with docker-compose fails to attach

pravin
  • 1,106
  • 1
  • 18
  • 27
  • 1
    run your docker as `sudo docker run -it test:test`. – Pavel Hamerník Dec 24 '21 at 09:34
  • @PavelHamerník wow!!.. naice.. can you please post this as answer.. So that I can accept it – pravin Dec 24 '21 at 09:39
  • You could try to setup it like in this answer https://stackoverflow.com/a/39150040/17726897 . I don't have experience with TTY stuff and compose. If I need to somehow change a running docker I use `docker-compose exec -it service_name /bin/bash` and modify the docker from the inside. – Pavel Hamerník Dec 24 '21 at 09:44
  • But often better is to just recreate the container with new code. – Pavel Hamerník Dec 24 '21 at 09:46

1 Answers1

1

You need to run your docker in interactive tty mode so the shell connects to the docker.

sudo docker run -it test:test