How to setup and attach to container running python program waiting for input?

Hey ya’ll,

I have a python app with a shell-like interface that accepts commands via stdin. I’m trying to figure out how to setup docker-compose / balena-engine such that I can attach to the container and use the shell.

My python app uses input('> ') to wait for stdin. It’s working when run directly, outside of balena.
In my docker-compose.yml config I’ve added stdin_open: true and tty: true.
Then I ssh into the host container and attach with balena-engine attach core. This appears to attach correctly to the app but typing commands into the prompt (actually the > prompt doesn’t appear) has no effect… i.e. the input never reaches the program.

I can make it work by running balena-engine run -ti local_image_core but then it’s running independently of docker-compose and I don’t think that’s what I want.

How can I connect to an interactive shell like this?

Any help would be much appreciated!

Upon further research it appears that docker-compose run might be required versus docker-compose up (which I assume balena is using) according to this issue. OR passing the -d (detach) to docker-compose up to as suggested by this SO answer.

However, I still don’t know how to achieve this with balena-cli / balenaEngine

Hello! Can you share what is the ENTRYPOINT or CMD instruction you are using in your dockerfile, I am saying this because the attach command attaches to the entrypoint process.

Best Regards,
Marios

Hey Marios,

Thanks for the help. It’s CMD python main.py. In main.py, I have something like while line := input('> ') to repeatedly accept commands via stdin.

Can you try using ENTRYPOINT instead of CMD and see if it makes a difference?

@mbalamat I tried ENTRYPOINT and it doesn’t make a difference when attaching. I also tried balena-engine start -ai core_1_1 unsuccessfully. However, balena-engine run -ti --privileged local_image_core works fine.

Hi, I would expect that balena-engine exec -ti core_1_1 would work just like the example using run. Can you confirm that this is something you tried?