'Scheduling a docker Container with a docker-compose file

I have a docker container which is brought up with the help of a docker-compose file along with a database container. I want to do this:

  • Keep the database container running
  • Schedule the container with my python program to run daily, generate results and stop again

This is my configuration file:

version: '3.7'

services:
   database:
      container_name: sql_database
      image: mysql:latest
      command: --init-file /docker-entrypoint-initdb.d/init.sql
      ports:
       - 13306:3306
      environment:
         MYSQL_ROOT_PASSWORD: root
      volumes:
       - ./backup.sql:/docker-entrypoint-initdb.d/init.sql

   python-container:
      container_name: python-container
      build: ./python_project
      command: python main.py
      depends_on: 
       - database
      volumes: 
       - myvol:/python_project/data

volumes: 
   myvol:

Can someone please help me with this? Thanks!



Solution 1:[1]

I was just about to ask the same thing. Seems silly to keep a container going 24/7 just to run one job a day (in my case, certbot renew).

I think there may be a way to fake this using the RESTART_POLICY option with a long delay and no maximum retries, but I haven't tried it yet.

EDIT: Apparently restart_policy only works for swarms. Pity.

Solution 2:[2]

If the underlying container has a bash shell, you set the command to run a loop with a delay, like this:

while true; do python main.py; sleep 1; done

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Phillip Ngan