0

I created a custom command in django and I want to use a docker-compose command in it. I use a subprocess as follow:

class Command(BaseCommand):

def handle(self, *args, **options):
    data = open_file()
    os.environ['DATA'] = data
    command_name = ["docker-compose ", "-f", "docker-compose.admin.yml", "up"]
    popen = subprocess.Popen(command_name, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                             universal_newlines=True)
    return popen

when I do it I get a FileNotFoundError:

FileNotFoundError: [Errno 2] No such file or directory: 'docker-compose ': 'docker-compose

Is it even possible to use docker-compose inside of a command ? It feels like I am missing something.

Thank you !

Kimor
  • 532
  • 4
  • 17
  • Probably need `shell=true`? – SiHa Aug 10 '20 at 10:45
  • 1
    There's an extra space after `docker-compose `. (Do _not_ use `shell=True`: it's fairly easy to introduce security issues that would make it possible to root the whole host.) – David Maze Aug 10 '20 at 11:21

1 Answers1

1

I see 2 possible things.

  1. Your docker compose should run in background, so, you should add -d option at the end of the command: docker-compose -f docker-compose.admin.yml up -d

  2. Best practice is start docker compose in background and you can take output with popen executing docker-compose -f docker-compose.admin.yml logs

You can also run docker-compose services and get interactive output, defining stdin_open: true in your yml file.

  1. You could check if your current directory is where docker-compose.admin.yml exists, printing os.getcwd() and comparing it to docker-compose.admin.yml path.
Alejandro Galera
  • 3,445
  • 3
  • 24
  • 42