r/pythonhelp • u/denunciadolince • Jul 07 '21
Python Invoke tasks for Docker using Git Bash not working on a Windows system
Hello dear people of r/pythonhelp, I wish you a very good day. I'm currently trying to make some simple docker and docker-compose tasks with the help of Python's invoke library. One of my teammates only has access to a Windows laptop and has to inevitably work like this. I originally wrote and ran them on Linux without any issues but the commands do not work for him. Here's how my script looks like after trying to set up the Windows option:
from invoke import task
import os
if os.name == 'nt':
SYSTEM_DOCKER_PREFIX = 'winpty docker exec -it'
pty = False
else:
pty = True
SYSTEM_DOCKER_PREFIX = 'docker exec -it'
def manage(c, command, restart_docker=False):
"""Run django manage.py commands"""
if restart_docker:
up(c, True)
container_name = get_full_container_name(c, 'webapp')
c.run(f'{SYSTEM_DOCKER_PREFIX} {container_name} python3 manage.py {command}', pty=pty)
@task
def docker_exec(c, command):
"""Run arbitrary commands in the web app container"""
container_name = get_full_container_name(c, 'webapp')
c.run(f'{SYSTEM_DOCKER_PREFIX} {container_name} {command}', pty=pty)These tasks (Though I have others more complex) would just be used to shorten some of the lengthy docker-compose commands to build my containers, a DJANGO app, and simple django stuff like createsuperuser, migrate, shell, etc. If I run one of these commands in Git Bash in Windows I always get this error:
$ inv task.manage createsuperuser
webapp_1
stdin is not a tty
$ inv task.manage shell
webapp_1
stdin is not a ttyTrying to research this problem, it seems there's some issues with pty=True and winpty. I already followed the suggestions of including winpty, changing pty to false, and still nothing. Furthermore, looking for this error online doesn't really pop up anything of help.
If anyone has ever faced a similar issue and could give some help, I'd be really grateful. Thank you guys and this community as always for helping so many people.
1
u/ace6807 Jul 08 '21
As much as I'd love python to be a nice solution for this, I think npm run scripts works better here. Install npm, create a package.json and define your commands in the script block and you are good to go. I just found this to help you get started if you are interested: https://gist.github.com/duluca/d13e501e870215586271b0f9ce1781ce