pkoasne Posted August 11, 2023 Share Posted August 11, 2023 Hi, Is there a way that I can trigger a script to run whenever a specific docker starts? The options in the User Scripts plugin doesn't have this usecase. Thanks Quote Link to comment
Solution JonathanM Posted August 11, 2023 Solution Share Posted August 11, 2023 Not that I can think of, but you can certainly start a container from a script. Disable the auto start for that container on the docker page and script the startup separately. schedule at array start sleep however long you want to wait docker start containername run the rest of your commands. Quote Link to comment
pkoasne Posted August 12, 2023 Author Share Posted August 12, 2023 (edited) 19 hours ago, JonathanM said: Not that I can think of, but you can certainly start a container from a script. Disable the auto start for that container on the docker page and script the startup separately. schedule at array start sleep however long you want to wait docker start containername run the rest of your commands. Thanks, that is basically what I ended up doing. Running this on a cron every few minutes to check if the docker is running and if not it starts it up and executes the commands I need. Sharing the script in case anyone else ever needs this. #!/bin/bash # Replace CONTAINER_NAME with the actual name of your running Docker container CONTAINER_NAME="CHANGEME" # Specify the log file path LOG_FILE="docker_exec_log.txt" # Check if the container is already running if docker inspect -f '{{.State.Running}}' $CONTAINER_NAME > /dev/null 2>&1; then echo "Container $CONTAINER_NAME is already running." exit 0 fi # Start the container if it's not running docker start $CONTAINER_NAME # Wait for the container to start echo "Waiting for container $CONTAINER_NAME to start..." while ! docker inspect -f '{{.State.Running}}' $CONTAINER_NAME > /dev/null 2>&1; do sleep 1 done echo "Container $CONTAINER_NAME is now running." # Run commands inside the Docker container and log errors/output docker exec $CONTAINER_NAME bash -c ' REPLACE_THIS_WITH_THE_COMMANDS_YOU_WANT_TO_RUN ' 2>&1 > $LOG_FILE # Display the log file contents cat $LOG_FILE Edited August 12, 2023 by pkoasne added script 1 Quote Link to comment
BlueBull Posted October 15, 2023 Share Posted October 15, 2023 On 8/12/2023 at 8:19 PM, pkoasne said: Thanks, that is basically what I ended up doing. Running this on a cron every few minutes to check if the docker is running and if not it starts it up and executes the commands I need. Sharing the script in case anyone else ever needs this. #!/bin/bash # Replace CONTAINER_NAME with the actual name of your running Docker container CONTAINER_NAME="CHANGEME" # Specify the log file path LOG_FILE="docker_exec_log.txt" # Check if the container is already running if docker inspect -f '{{.State.Running}}' $CONTAINER_NAME > /dev/null 2>&1; then echo "Container $CONTAINER_NAME is already running." exit 0 fi # Start the container if it's not running docker start $CONTAINER_NAME # Wait for the container to start echo "Waiting for container $CONTAINER_NAME to start..." while ! docker inspect -f '{{.State.Running}}' $CONTAINER_NAME > /dev/null 2>&1; do sleep 1 done echo "Container $CONTAINER_NAME is now running." # Run commands inside the Docker container and log errors/output docker exec $CONTAINER_NAME bash -c ' REPLACE_THIS_WITH_THE_COMMANDS_YOU_WANT_TO_RUN ' 2>&1 > $LOG_FILE # Display the log file contents cat $LOG_FILE You are awesome for sharing the script! I greatly appreciate it, thank you 1 Quote Link to comment
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.