Using docker compose I've created a container for my MySQL DB and one for my Python script. When I use the command docker compose up, my images are built for my Python app container and my MySQL DB container, and the python script is run. After execution, the shell just hangs since MySQL server is still running and my script has completed execution. How can I either stop the MySQL server after my script runs, or re-run my script while the MySQL server continues to run?
Add a comment
|
1 Answer
When running docker-compose up, your current shell will display all the logs from all the containers defined in the docker-compose.yaml file. Also if you terminate the command with cmd + c in MacOS, all the containers will stop running.
As a result, this gives you the impression that the shell just hangs while everything is still running as normal.
What you want in this case is to let the containers continue to run in the background (detached mode)
docker-compose up --detach
The MySQL server will now continue to run until you stop it with docker-compose down.
2 Comments
InternationalCodingGuru
Thank you. IN that case, how do I re-run my script when I want to (eg every 24hours)? Do I need to do
docker-compose up each time or is there a better alternative?Nguyen Lam Phuc
Your best bet is to 1) set the container with the script to
restart: always so it will always be up while docker is up. 2) set the entrypoint and command to tail -f /dev/null so that the container will not be terminated half-way. 3) Install cronjob inside the docker container and configure it to run every 24 hours.