Compose: Error during database backup via cronjob

Created on 1 Mar 2018  ·  3Comments  ·  Source: docker/compose

I have following docker-compose:

  db:
    image: mariadb
    environment:
        MYSQL_ROOT_PASSWORD: mysecretpass
    restart: always

I can backup my databases using this command:
cd /opt/docker_data/ && /usr/local/bin/docker-compose exec db sh -c 'exec mysqldump --all-databases -uroot -p"$MYSQL_ROOT_PASSWORD"' > /opt/backup.sql
This works fine.

I added the same line of code to my crontable, but then I get to following error:

Traceback (most recent call last):
  File "bin/docker-compose", line 6, in <module>
  File "compose/cli/main.py", line 71, in main
  File "compose/cli/main.py", line 124, in perform_command
  File "compose/cli/main.py", line 467, in exec_command
  File "site-packages/dockerpty/pty.py", line 338, in start
  File "site-packages/dockerpty/io.py", line 32, in set_blocking
ValueError: file descriptor cannot be a negative integer (-1)
Failed to execute script docker-compose

What is my mistake?
Thank you!

kinquestion

Most helpful comment

Your script is failing because cron does not allocate a TTY, which docker-compose exec expects by default. Adding the -T flag to your command should remedy this:
cd /opt/docker_data/ && /usr/local/bin/docker-compose exec -T db sh -c 'exec mysqldump --all-databases -uroot -p"$MYSQL_ROOT_PASSWORD"' > /opt/backup.sql

All 3 comments

Your script is failing because cron does not allocate a TTY, which docker-compose exec expects by default. Adding the -T flag to your command should remedy this:
cd /opt/docker_data/ && /usr/local/bin/docker-compose exec -T db sh -c 'exec mysqldump --all-databases -uroot -p"$MYSQL_ROOT_PASSWORD"' > /opt/backup.sql

Thank you, that fixed it! =)

It fixes it sure, but I would argue that compose could handle this error slightly better, ending on an exception with "ValueError: file descriptor cannot be a negative integer (-1)" isn't exactly descriptive, I would have caught the exception and print a more meaningful error.

Was this page helpful?
0 / 5 - 0 ratings